Scrivio Interview library

Garbage collection

33 questions in 10 subtopics.

GC roots and reachability

ConceptFoundations6 min read
What are GC roots in the JVM? Name several kinds and explain how reachability decides what is garbage.
DebuggingFoundations7 min read
This service's heap grows until OutOfMemoryError:
public class SessionRegistry {
private static final Map<String, Session> SESSIONS = new HashMap<>();
public static void register(Session s) {
SESSIONS.put(s.id(), s);
}
}
Sessions are registered on login but nothing calls remove. Explain the leak and fix it.
InternalsIntermediate7 min read
A young collection must find young objects referenced from the old generation without scanning the whole old generation. Explain how card tables and G1 remembered sets solve this, and what they cost.

Serial and Parallel GC

ConceptFoundations6 min read
When is Serial GC the right choice, and when does the JVM select it without being told?
ScenarioIntermediate7 min read
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

InternalsFoundations6 min read
Describe a G1 collection cycle: young collections, concurrent marking, and mixed collections. What does MaxGCPauseMillis influence?
InternalsIntermediate7 min read
What are humongous objects in G1, and why might a service that builds 20 MB byte arrays perform poorly with 4 MB regions?
DebuggingIntermediate7 min read
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?
ScenarioAdvanced7 min read
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

ConceptFoundations6 min read
How do you enable ZGC on JDK 21 compared with JDK 25, and what are typical reasons to choose it over G1?
ConceptIntermediate7 min read
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.
DebuggingIntermediate6 min read
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

ConceptIntermediate6 min read
How does Shenandoah differ from G1 and ZGC, and which JDK builds include it?
ScenarioAdvanced6 min read
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

ConceptFoundations5 min read
What is the default garbage collector in JDK 21 and JDK 25, and how do you check which collector a running JVM is using?
ScenarioIntermediate6 min read
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.
LeadershipAdvanced6 min read
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?
LeadershipPrincipal6 min read
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.
LeadershipPrincipal7 min read
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

DebuggingFoundations6 min read
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.532ms
DebuggingIntermediate6 min read
A 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?
ScenarioIntermediate6 min read
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

ConceptFoundations7 min read
Explain strong, soft, weak and phantom references in Java with one practical use for each.
DebuggingFoundations6 min read
A team uses WeakHashMap<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?
DebuggingIntermediate6 min read
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.
LeadershipPrincipal6 min read
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

ConceptFoundations7 min read
What are the main goals you can tune a garbage collector for, and why can you not optimise all of them at once?
ScenarioIntermediate7 min read
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?
DesignAdvanced6 min read
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.
LeadershipPrincipal6 min read
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

ConceptFoundations6 min read
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?
InternalsIntermediate7 min read
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.
ScenarioAdvanced7 min read
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?