"Can you explain how garbage collection works in the JVM?"
JVM garbage collection is a core topic in Java interviews at top tech companies. Understanding GC demonstrates your ability to write performant applications, diagnose memory issues, and tune production systems — skills critical for senior engineering roles.
Start with the generational hypothesis: most objects die young, so the JVM divides the heap into Young Generation (Eden + two Survivor spaces) and Old Generation.
Explain Minor GC: new objects are allocated in Eden. When Eden fills up, a Minor GC copies live objects to a Survivor space and clears Eden. Objects surviving multiple GCs (default threshold 15) get promoted to Old Generation.
Describe Major/Full GC: triggered when Old Generation is full. This is more expensive and causes longer stop-the-world pauses.
Cover GC algorithms: Mark-Sweep, Mark-Compact, and Copying. Each has different tradeoffs for throughput vs. latency.
Discuss modern collectors: G1 (default since Java 9, region-based, predictable pause times), ZGC and Shenandoah (sub-millisecond pauses for large heaps).
The JVM uses automatic garbage collection to reclaim memory from objects that are no longer reachable. The heap is divided into generations based on the observation that most objects are short-lived. The Young Generation contains Eden and two Survivor spaces — new objects go into Eden, and Minor GC uses a copying algorithm to move live objects between Survivor spaces. Objects that survive enough GC cycles (configurable, default 15) are promoted to the Old Generation. When Old Generation fills up, a Major GC is triggered, which is more expensive. The JVM determines object liveness through GC Roots — objects referenced from stack frames, static fields, JNI references, etc. Three main algorithms are used: Mark-Sweep (fast but causes fragmentation), Mark-Compact (defragments but slower), and Copying (used in Young Gen, fast but needs double space). Modern collectors like G1 divide the heap into equal-sized regions and can collect partial regions to meet pause time targets. G1 became the default in Java 9. For ultra-low latency, ZGC (Java 15+) achieves sub-millisecond pauses even with terabyte-scale heaps using colored pointers and load barriers. In production, key tuning parameters include -Xms/-Xmx for heap size, -XX:MaxGCPauseMillis for G1 pause targets, and enabling GC logging with -Xlog:gc for diagnostics.
Know the difference between Minor GC, Major GC, and Full GC — interviewers often test this.
Be prepared to explain GC Roots and how the JVM determines which objects are alive.
Mention real-world tuning experience if you have it — it strongly differentiates candidates.
Use HireMe AI to get real-time reminders of GC collector names and their characteristics during your interview.
A pause where all application threads are stopped so the GC can safely identify and collect garbage. Modern collectors like ZGC minimize these pauses to under 1ms.
G1 is a good default for most applications. ZGC is better for applications requiring ultra-low latency with large heaps (multi-GB to TB).
System.gc() suggests a GC run, but the JVM may ignore it. In production, never rely on manual GC — let the JVM manage it.
HireMe AI listens to your interview in real-time, detects questions, and generates instant answer suggestions — so you never freeze up again.
Try HireMe AI Free