Java / Java 17 Garbage Collection Interview Questions
Explain the internal working of G1 GC's concurrent marking cycle?
G1's concurrent marking cycle identifies which old regions hold the most garbage so later mixed collections know what to reclaim. It runs in several distinct phases, most of them alongside the running application:
Initial Mark marks the direct roots and is piggybacked onto an already-scheduled young collection to avoid a separate pause. Root Region Scanning then concurrently scans survivor regions (as of initial mark) for references into the old generation. Concurrent Marking traces the full object graph from those roots while the application keeps running; to stay correct despite concurrent mutation, G1 uses a Snapshot-At-The-Beginning (SATB) write barrier that logs any reference a mutator thread is about to overwrite, ensuring no object that was reachable at the start of marking is missed.
Remark is a short stop-the-world pause that drains the last SATB logs and finishes reference processing (for soft/weak/phantom references). Cleanup then tallies live data per region and immediately frees any region found to be 100% garbage, without needing to evacuate anything from it - this feeds directly into the mixed-collection phase that follows.
More Related questions...