NOTE

7.10 3. Garbage Collectors

Generational collection and historical Serial, Parallel, CMS, and G1 collectors, including CMS/G1 details and comparison.

JavaCreated Updated 2 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Garbage-Collection Algorithm Actually Used by the JVM: Generational Collection

Memory is divided into the young generation and old generation according to object lifetime.

  • The young generation is characterized by only a small number of surviving objects after each collection, so an improved copying algorithm is used. The improved copying algorithm divides memory into Eden and Survivor areas (8:1:1), so only 10% of the space is wasted.
  • The old generation is characterized by many surviving objects, so mark-sweep and mark-compact algorithms are used.

2. Garbage-Collector Categories

2.1. Four Categories and Seven Garbage Collectors

  • Serial: garbage collection is performed by a single thread while all user threads are paused.
    • Young generation: Serial collector.
    • Old generation: Serial Old collector.
  • Parallel: garbage collection is performed by multiple threads while all user threads are paused.
    • Young generation: ParNew collector and Parallel Scavenge collector.
    • Old generation: Parallel Old collector.
  • CMS: garbage collection is performed by multiple threads and user threads do not need to be paused for the entire collection.
  • G1:

2.1.1. How to Enable Them

4. Selecting a Specific Garbage Collector with JVM Parameters

2.2. CMS in Detail

  1. Initial mark: single-threaded and STW, but it only marks objects directly reachable from GC Roots. Because the number of directly associated objects is relatively small, this stage is very fast.
  2. Concurrent mark: multi-threaded without STW for the whole phase. It concurrently traces and marks the objects identified by the initial-mark phase while other threads can continue working. This stage takes longer but does not stop all application work for the whole phase.
  3. Remark: STW. Because new garbage or reference changes may occur during concurrent marking, a remark is required. This phase runs without user threads and generally takes longer than initial marking.
  4. Concurrent sweep: multi-threaded without STW for the whole phase. It concurrently clears the garbage previously marked while other user threads can continue working.

2.2.1. Log Analysis

UTOOLS1584166071027.png

2.3. G1 in Detail

  • Memory division: macroscopically it is no longer laid out simply as contiguous young- and old-generation areas. It is divided into N regions, and each region has generational attributes.

  • It uses mark-compact overall to avoid memory fragmentation and copying locally.

  • G1 can specify a desired pause-time target.

  • The collection process includes:

    1. Initial mark: STW, marks objects directly reachable from GC Roots.
    2. Concurrent marking: performs concurrent marking while user threads can still run.
    3. Remark: STW; because reference changes may occur during concurrent marking, mark again.
    4. Live Data Counting and Evacuation: evaluates reclaimable regions and selects what to reclaim based on the pause-time goal.

2.4. CMS vs. G1

CMS G1
Scope Old-generation collector; can be used together with young-generation collectors such as ParNew Covers both young and old generations and does not need another collector for those generations
Goal Minimize pause time Make garbage-collection pause time more predictable
Memory fragmentation Uses mark-sweep and can create memory fragmentation Uses region compaction/evacuation to reduce fragmentation
Collection process See 2.2 CMS in Detail See 2.3 G1 in Detail

3. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub