Garbage collection
33 questions in 10 subtopics.
GC roots and reachability
What are GC roots in the JVM? Name several kinds and explain how reachability decides what is garbage.This service's heap grows untilOutOfMemoryError:public class SessionRegistry {
private static final Map<String, Session> SESSIONS = new HashMap<>();
public static void register(Session s) {
SESSIONS.put(s.id(), s);
}
}
Serial and Parallel GC
When is Serial GC the right choice, and when does the JVM select it without being told?A nightly batch job processes 200 GB of records over two hours on a 16-core VM. Only total runtime matters; nobody is waiting on individual requests. Which collector would you choose, and how would you configure and validate it?G1
Describe a G1 collection cycle: young collections, concurrent marking, and mixed collections. What does MaxGCPauseMillis influence?What are humongous objects in G1, and why might a service that builds 20 MB byte arrays perform poorly with 4 MB regions?G1 logs for a service show a young pause marked with to-space exhausted or evacuation failure, immediately followed by Pause Full (G1 Compaction Pause) lasting several seconds. What happened and what do you change?A platform team sees that after a move to larger instances, many G1 services have p99 latency regressions although pause times look the same. Investigation shows young collections are rare but long. How do you reason about this and what fleet guidance follows?ZGC and generational ZGC
How do you enable ZGC on JDK 21 compared with JDK 25, and what are typical reasons to choose it over G1?How does ZGC keep application pauses well under a few milliseconds regardless of heap size? Explain colored pointers and load barriers, and what generational ZGC changed.A service on generational ZGC shows GC pauses below 1 ms, yet p99 latency spikes to 300 ms under peak load. JFR shows ZAllocationStall events. Explain and fix.Shenandoah
How does Shenandoah differ from G1 and ZGC, and which JDK builds include it?Your fleet uses JDK builds from two vendors. One team wants Shenandoah for a latency-sensitive service, but Shenandoah is absent from the other vendor's build used by most services. How do you decide?Choosing a collector
What is the default garbage collector in JDK 21 and JDK 25, and how do you check which collector a running JVM is using?Choose a collector for each case and justify it: (a) a REST API with a 2 GB heap and p99 target of 100 ms; (b) an in-memory cache service with a 64 GB heap and p99.9 target of 5 ms; (c) a sidecar with 1 vCPU and 256 MB.You are setting the garbage collector standard for about 400 JVM services. What is the default, what exceptions do you allow, and how do you validate a change across the fleet?A vendor proposes moving your trading platform to a commercial JVM with its own pauseless collector, instead of using OpenJDK with generational ZGC. Frame this as a multi-year decision.HotSpot's collectors evolved from Serial and Parallel to CMS, then G1 as default in JDK 9, then ZGC and Shenandoah, and now generational ZGC. Explain what drove each step and what that history tells you about the collector choices your company should make for the next five years.Reading GC logs
Explain every part of this unified GC log line: [12.345s][info][gc] GC(42) Pause Young (Normal) (G1 Evacuation Pause) 812M->214M(1024M) 18.532msA service's GC log shows this line once an hour, every hour:GC(310) Pause Full (System.gc()) 2900M->2100M(4096M) 2412.771ms. What are the likely causes and how do you fix it?From GC logs you see young collections every 2 seconds on a G1 service; eden is 1,200 MB before each collection; heap after young GC rises slowly from 900 MB to 1,000 MB over an hour. Estimate the allocation rate and promotion rate, and say what these numbers tell you.Reference types: soft, weak, phantom
Explain strong, soft, weak and phantom references in Java with one practical use for each.A team usesWeakHashMap<Key, Meta> to attach metadata to keys owned elsewhere, but entries are never removed even after the keys are dropped. The code is map.put(key, new Meta(key, info)). Why?A service caches computed reports in a ConcurrentHashMap<String, SoftReference<Report>>. Under load, the heap stays close to full, GC pauses grow and GC CPU rises, but there is no OutOfMemoryError. Explain and propose a fix.Finalization is deprecated for removal, and soft references are discouraged for caches. What does the history of finalize and reference-based resource management teach about relying on the garbage collector for non-memory resources, and what standards follow?GC tuning
What are the main goals you can tune a garbage collector for, and why can you not optimise all of them at once?A G1 service with an 8 GB heap and about 1 GB per second of allocation has a p99 latency budget of 50 ms, but young pauses take around 120 ms. How do you approach tuning it?Garbage collection consumes a noticeable share of CPU across your 2,000 JVM pods. Propose a program to reduce GC-related cost without hurting reliability.Cloud providers price CPU per core while memory per core keeps growing, and concurrent collectors trade CPU and memory headroom for shorter pauses. Over the next five years, how should this affect your company's GC strategy, and how do you keep the strategy current?Mark, sweep and compaction algorithms
Explain mark-sweep, mark-compact and copying collection. What are the trade-offs in fragmentation, pause time and memory overhead, and which does HotSpot use for young and old generations?How do concurrent collectors like G1, ZGC and Shenandoah mark objects while the application keeps changing references? Explain tri-color marking, the lost object problem and the barriers used.A service with a 31 GB heap on Parallel GC has full GC pauses of 12 seconds every few hours because the old generation fills with long-lived cache entries. The team wants to move to ZGC with a 64 GB heap. How do you evaluate this?No question matches these choices.