Video in progress

Strong Middle ⚙️ JVM and memory

What is G1's cycle: how do young collection, concurrent marking and mixed collection connect with each other?

Normally only young GCs run. When Old occupancy exceeds the IHOP threshold, concurrent marking starts and works out which Old regions have the most garbage. The next several mixed GCs clean those Old regions with the most garbage along with the young regions.

📖 Answer

The phases in their natural order:

  1. Young-only phase. Every young GC is a stop-the-world evacuation: the live objects in Eden and Survivor are copied to another region. Old slowly fills up.
  2. Marking starts. When the occupied part of Old goes above InitiatingHeapOccupancyPercent (default 45%, though G1 tunes it adaptively), the next young GC happens with the "Concurrent Start" label.
  3. Concurrent marking. Live objects are marked in parallel with the application. At the end come short Remark and Cleanup pauses. Completely empty Old regions are freed right there.
  4. Mixed phase. Over several GCs, the young regions + the Old regions with the most garbage are evacuated. That is where the name G1 comes from: Garbage First.
  5. Then it goes back to the young-only phase.

In the log it looks like this:

GC(120) Pause Young (Concurrent Start) (G1 Evacuation Pause) 1800M->1650M(4096M) 18.2ms
GC(121) Concurrent Mark Cycle
GC(125) Pause Young (Prepare Mixed) ...
GC(126) Pause Young (Mixed) ...

If marking can't keep up with the speed at which the application fills Old, you get to-space exhausted and eventually a Full GC. In that case lowering IHOP or raising ConcGCThreads is worth considering, but first check whether the heap is big enough and why the allocation rate is so high.