Java / Java 17 Garbage Collection Interview Questions
How does G1 GC decide which regions to collect first?
This is the origin of G1's name - "Garbage First". During its concurrent marking cycle, G1 tracks how much live versus garbage data each old region holds, then, for every subsequent collection pause, it picks the set of regions offering the most reclaimable garbage for the amount of pause time it's budgeting toward its -XX:MaxGCPauseMillis goal.
This means G1 doesn't need to collect the entire old generation at once - it prioritizes whichever regions give the best "garbage reclaimed per millisecond of pause" trade-off, which is what lets it hit a soft pause-time target even on very large heaps.
More Related questions...