Prev Next

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:

flowchart TD A[Allocated in Eden region via TLAB] --> B{Reachable at next minor GC?} B -- No --> Z[Reclaimed - Eden region cleared for reuse] B -- Yes --> C[Copied to a Survivor region, age = 1] C --> D{Reachable at next minor GC?} D -- Yes, age below threshold --> C D -- Yes, age reaches MaxTenuringThreshold --> E[Promoted to an Old region] D -- No --> Z E --> F{Region found garbage-heavy during concurrent marking?} F -- Yes --> G[Reclaimed via Mixed Collection evacuation, or freed outright at Cleanup if 100% garbage] F -- No --> E

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).

According to the weak generational hypothesis, most objects are reclaimed:
A region found to be 100% garbage at the Cleanup phase of concurrent marking is:

More Related questions...

What is garbage collection in Java? What is the purpose of a garbage collector in the JVM? What are the types of garbage collectors available in Java 17? What is the default garbage collector in Java 17? What is the young generation in the JVM heap? What is the old (tenured) generation in the JVM heap? What is Metaspace in Java 17? What is a minor GC? What is a major GC? What is a full GC? Define a stop-the-world pause? What is the Eden space? What are Survivor spaces used for? How do you enable GC logging in Java 17? What is the purpose of the -Xmx and -Xms flags? Why is G1 GC the default collector since Java 9? How does G1 GC divide the heap into regions? What is a humongous object in G1 GC? How does G1 GC decide which regions to collect first? What is a remembered set in G1 GC? What is a card table and how does it help garbage collection? How do you configure G1 GC's pause time goal? What is the difference between G1 GC and Parallel GC? When should you choose ZGC over G1 GC? When should you choose Shenandoah over G1 GC? What happens during a G1 GC mixed collection? Why doesn't Java 17 include the CMS garbage collector? How is Parallel GC different from Serial GC? What is the difference between throughput and low-latency garbage collectors? How do you troubleshoot frequent full GCs in a Java 17 application? What is string deduplication in G1 GC? How does the JVM decide when to trigger a GC cycle? What is promotion in the context of generational garbage collection? Why should you avoid explicit calls to System.gc()? What is the difference between soft, weak, and phantom references? Explain the internal working of G1 GC's concurrent marking cycle? Explain the execution flow of a ZGC collection cycle? How does ZGC achieve sub-millisecond pause times using colored pointers? Explain the lifecycle of an object through the JVM heap under G1 GC? How does Shenandoah's Brooks pointer (forwarding pointer) enable concurrent compaction? What is an evacuation failure in G1 GC and how do you handle it? How does garbage collection behave differently in a containerized environment with cgroup memory limits? How can you optimize G1 GC for large heaps (32GB+)? Explain the tricolor marking algorithm used in concurrent garbage collectors? What are write barriers and load barriers, and how do collectors use them? How do you analyze a GC log to identify a memory leak? What is the difference between G1 GC's young-only phase and space-reclamation phase? How does adaptive sizing (ergonomics) work in the JVM's garbage collectors? Explain how class unloading interacts with garbage collection and Metaspace? Which is better for a low-latency trading application - ZGC or Shenandoah - and why?
Show more question and Answers...


Comments & Discussions