← Back to Java

Types of Garbage Collectors

JavaSeniorjava

The Question

What are the different types of garbage collectors in Java?

What a Strong Answer Covers

  • Serial = single-threaded stop-the-world
  • Parallel = multi-threaded high throughput Java 8 default
  • G1 = region-based Java 9 default large heaps
  • ZGC = ultra-low latency Java 11
  • Shenandoah = concurrent compaction

Senior-Level Answer

Java provides several garbage collector implementations, each representing a different set of tradeoffs between throughput, latency, and resource usage.

Serial GC (-XX:+UseSerialGC) uses a single thread for all GC work and Stop-the-World pauses for both Minor and Major collection. It is appropriate for small heaps (< 100 MB) and client applications or containers with a single CPU.

Parallel GC (-XX:+UseParallelGC, default before Java 9) uses multiple threads for GC work, maximizing throughput. It still uses Stop-the-World pauses but parallelizes the collection work. Best for batch processing jobs where throughput matters more than latency.

CMS (Concurrent Mark Sweep, -XX:+UseConcMarkSweepGC) performed most marking and sweeping concurrently with application threads, reducing pause times. However it did not compact the heap, leading to fragmentation. It was deprecated in Java 9 and removed in Java 14.

G1GC (Garbage First, default since Java 9) divides the heap into equal-sized regions rather than contiguous Young/Old areas. It prioritizes regions with the most garbage (hence 'Garbage First'), performs concurrent marking, and compacts incrementally during mixed collections. It targets a configurable pause time goal (-XX:MaxGCPauseMillis). G1 is the best general-purpose choice for heaps between 6 GB and 32 GB with mixed throughput/latency requirements.

ZGC (-XX:+UseZGC, production-ready since Java 15) performs marking, relocation, and remapping concurrently using load barriers and multi-colored pointers, keeping Stop-the-World pauses under 1ms regardless of heap size. Optimal for latency-sensitive services with large heaps.

Shenandoah (-XX:+UseShenandoahGC, from Red Hat, available in OpenJDK) similarly achieves concurrent compaction using a Brooks forwarding pointer indirection, with pause times competitive with ZGC. Best for applications that cannot afford long GC pauses but are running on commodity JVMs without ZGC availability.

Key Differences

CollectorPause StyleCompactionBest ForDefault Since
SerialStop-the-World (single thread)YesSmall heaps, single-CPU—
ParallelStop-the-World (multi-thread)YesBatch / throughput-focusedJava 8 default
CMSMostly concurrentNo (fragmentation risk)Low-latency (deprecated)Removed Java 14
G1GCConcurrent + short STWIncrementalGeneral-purpose, 6–32 GB heapsJava 9+
ZGCConcurrent (< 1ms STW)ConcurrentLarge heaps, latency-criticalJava 15+
ShenandoahConcurrent (< 1ms STW)ConcurrentLow-latency on standard JVMOpenJDK option

What Separates a 2/3 from a 3/3

2/3 — Passing but Incomplete

Names at least four collectors with correct throughput/latency characterizations and knows G1GC is the Java 9+ default.

3/3 — Strong Answer

Accurately describes the mechanism of each collector (region-based for G1, colored pointers for ZGC, Brooks pointers for Shenandoah), explains when to choose each, and notes CMS deprecation/removal.

Common Mistakes

  • Not knowing that G1GC has been the default since Java 9 — still saying Parallel GC is the default.
  • Describing CMS as a current recommended option — it was removed in Java 14.
  • Unable to explain what makes ZGC/Shenandoah's pauses so short — just saying 'concurrent' without mentioning the load barriers or pointer mechanisms.

Follow-Up Questions

  • How do you tune G1GC's pause time target and what is the tradeoff? — Set -XX:MaxGCPauseMillis to a target in milliseconds. G1 will collect fewer regions per cycle to meet the target, potentially allowing more floating garbage and higher memory overhead.
  • What is a Stop-the-World pause and why does it harm latency-sensitive applications? — All application threads are suspended while GC performs certain phases. For a latency-sensitive service, a 200ms STW pause means all requests during that window experience a 200ms spike.
  • How would you choose between ZGC and G1GC for a new microservice? — If the service has strict p99 latency SLAs (sub-10ms), use ZGC. If throughput matters more or the heap is under 8 GB with moderate latency tolerance, G1GC is simpler to tune and well-understood.

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