Java / Java 17 Garbage Collection Interview Questions
Explain the lifecycle of an object through the JVM heap under G1 GC?
An object's journey through the heap follows a predictable path shaped by the generational hypothesis, with G1's region-based layout determining exactly where each step physically happens:
Most objects never make it past the first branch - they're allocated in Eden and found unreachable at the very next minor GC, which is exactly the weak generational hypothesis in action. Survivors get copied back and forth between the two survivor regions, aging each time, until either a survivor region runs out of room or the tenuring threshold is reached, at which point G1 relabels the destination region as Old and the object is promoted there.
Once in the old generation, the object's fate depends on G1's concurrent marking: if its region is found to be mostly garbage, it gets swept up in a mixed collection (or, if the whole region turns out to be garbage, reclaimed directly at Cleanup with no copying needed at all).
More Related questions...