JVM Garbage Collectors Compared — G1, ZGC, Shenandoah, Parallel
Fast recall
Quick revision
Core answer
Whether you know GC selection criteria beyond 'use the default'
Assuming ZGC is always better than G1 — ZGC uses more CPU and memory for its concurrency work
Explain the G1 region-based heap model and why it enables more predictable pause times than CMS
Interview-ready guide
Interview answer
Java ships with four main garbage collectors, each making a different trade-off between throughput, pause duration, and CPU overhead. There is no universally best choice — the right GC algorithm depends on what your application can tolerate: long pauses for high throughput, or short pauses at higher CPU cost.
Parallel GC:
- Highest throughput — throws all threads at collection during stop-the-world pauses
- Pauses can last seconds — suitable for batch processing and offline analytics, not for user-facing services
G1GC (default since Java 9):
- Region-based heap — collects regions with the highest garbage ratio first
- Soft pause-time target via
-XX:MaxGCPauseMillis=200; adjusts collection scope to meet it - Avoids fragmentation issues that killed CMS; good default for most web services
ZGC:
- Almost all work done concurrently with application threads — sub-millisecond pauses regardless of heap size
- Higher CPU usage for concurrent GC work
- Use when even 200ms pauses are unacceptable: algorithmic trading, real-time pricing, strict p99 SLAs
Common mistake:
- Assuming ZGC is universally better — on small heaps under 4GB, G1 pauses are already short; ZGC's CPU overhead provides no net benefit
I recommend G1 as the default, enabling GC logging, and only switching if p99 latency analysis shows GC pauses as the dominant contributor.
JVM Garbage Collectors Compared — G1, ZGC, Shenandoah, Parallel
Direct Answer
G1GC (default Java 9+) targets ~200ms pauses with region-based collection. ZGC and Shenandoah trade CPU for sub-millisecond pauses on large heaps. Parallel GC maximizes throughput with longer stop-the-world pauses — best for batch jobs. Pick by latency SLA, not brand.
Common Mistakes & Pitfalls
Assuming ZGC is always better than G1 — ZGC uses more CPU and memory for its concurrency work
Check your understanding
Answer these without looking back at the article.
- How does G1GC's region-based heap model differ from the traditional young/old generation model?
- When would you choose Shenandoah over ZGC?
- How do you measure the actual GC pause times in a running Java application?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.