← Back to Java

Garbage Collection in Java

JavaMidjava

The Question

What is garbage collection in Java?

What a Strong Answer Covers

  • automatic memory management
  • Mark-Sweep phases
  • Young Generation Eden/Survivor
  • Old Generation/tenured
  • Minor GC vs Major GC
  • unreachable objects collected
  • System.gc() suggestion only

Senior-Level Answer

Garbage collection (GC) in Java is the automatic process of identifying objects on the heap that are no longer reachable from any GC root (active threads, static fields, local variables on stack) and reclaiming their memory. The developer is freed from manual memory management, eliminating use-after-free and double-free bugs common in C/C++.

The JVM heap is divided generationally based on the observation that most objects die young (the weak generational hypothesis). The Young Generation holds newly created objects and is collected frequently in Minor GC events, which are fast. Objects that survive enough Minor GCs are promoted to the Old Generation (Tenured), collected less frequently in Major GC or Full GC events. Some collectors also use a Metaspace (formerly PermGen) for class metadata.

GC algorithms in modern JVMs include Serial GC (single-threaded, for small heaps), Parallel GC (throughput-focused, multi-threaded), CMS (Concurrent Mark Sweep — low pause but deprecated in Java 9, removed in 14), G1GC (Garbage First — default since Java 9, balances throughput and latency), ZGC (scalable, near-zero pause times, production-ready since Java 15), and Shenandoah (Red Hat, low-latency concurrent GC).

The GC process generally involves: mark (traverse from GC roots, mark live objects), sweep (reclaim unmarked objects), and optionally compact (slide live objects together to eliminate fragmentation). Compacting collectors avoid fragmentation but require a Stop-the-World pause while references are updated.

GC tuning involves choosing the right collector for your workload, sizing the heap and generation spaces (-Xms, -Xmx, -XX:NewRatio), and monitoring GC logs for pause frequency and duration. For latency-sensitive applications, ZGC or Shenandoah are preferred because they do most work concurrently with the application.

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Explains reachability-based collection, generational heap structure (Young/Old), and names at least two modern GC algorithms.

3/3 — Strong Answer

Adds the weak generational hypothesis rationale, mark-sweep-compact phases, GC roots definition, and distinguishes throughput vs. latency tradeoffs in collector selection (G1 vs. ZGC).

Common Mistakes

  • Saying objects are collected when their reference count reaches zero — Java uses reachability analysis (tracing GC), not reference counting.
  • Not knowing the current default GC (G1GC since Java 9) or being unaware of ZGC and Shenandoah for low-latency workloads.
  • Treating Full GC as equivalent to Minor GC — Full GC is far more expensive and a sign of memory pressure or tuning issues.

Follow-Up Questions

  • What is a GC root in Java and why does it matter? — GC roots are the starting points for reachability tracing: local variables in active stack frames, static fields, JNI references. An object is live if and only if it is reachable from a GC root.
  • What is the difference between a Minor GC and a Major GC? — Minor GC collects the Young Generation — fast, frequent, Stop-the-World but brief. Major GC (or Full GC) collects the Old Generation — more expensive, potentially long pause, indicates objects are being promoted faster than they can be collected.
  • How does ZGC achieve near-zero pause times? — ZGC performs all phases (mark, relocate, remap) concurrently with application threads using load barriers and colored pointers, keeping Stop-the-World pauses under 1ms regardless of heap size.

Related Questions

  • JVM vs JRE vs JDK
  • Java Platform Independence
  • How JVM Works
  • Main Features of Java
  • public static void main

Can You Explain This Cold?

Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.

Get Graded — Free Assessment
GrindQuestionsAITechnical interview assessment
TermsPrivacyAbout