Java / Java 17 Garbage Collection Interview Questions
Explain the execution flow of a ZGC collection cycle?
ZGC's cycle alternates brief stop-the-world pauses with much longer concurrent phases, keeping every pause's cost tied to the number of GC roots rather than the size of the live heap:
Pause Mark Start scans thread stacks and other GC roots to seed the mark, in a pause whose length depends only on the number of roots, not the heap size. Concurrent Mark then traces the object graph while the application keeps running; every reference load through a load barrier checks the color bits and "heals" (updates) stale pointers on the fly if the collector has since moved that object.
Pause Mark End is another brief pause to finish marking, after which ZGC concurrently selects a relocation set - the regions worth compacting. Pause Relocate Start again scans just the roots, and then Concurrent Relocate copies live objects out of the chosen regions while the application runs, with load barriers ensuring any thread that touches a not-yet-relocated reference gets redirected transparently to the new location.
More Related questions...