Which GCs minimize stop-the-world pauses?
4. ConcGCThreads = cores / 4 5. Avoid large arrays 6. SoftMaxHeapSize for memory return
Junior Level
Low-latency GC — collectors that minimize application pauses.
Main ones:
- ZGC — pauses < 1 ms (best)
- Shenandoah — pauses < 1 ms
- G1 — pauses 20-200 ms (compromise)
How they do it:
- Do most of the work concurrently with the application
- Stop only for scanning GC Roots
Middle Level
Minimization Strategies
| GC | Strategy | Pause |
|---|---|---|
| ZGC | Colored Pointers + Load Barriers | < 1 ms |
| Shenandoah | Load Reference Barriers | < 1 ms |
| G1 | Regions + Mixed GC | 20-200 ms |
Allocation Stall — the Danger
Application creates garbage faster than concurrent marking can finish.
Application thread blocks and starts evacuating objects itself. Dangerous: if stalls are frequent,
throughput drops even more than with Parallel GC.
Comparison
| GC | Typical Pause | CPU overhead | Throughput |
|---|---|---|---|
| Parallel | 100-1000 ms | Minimal | Maximum |
| G1 | 20-200 ms | Medium | Good |
| ZGC/Shenandoah | < 1 ms | High | -5-15% |
Generational ZGC (Java 21+)
Generational split:
→ -50% CPU overhead
→ Throughput almost like G1
→ Pauses still < 1 ms
When NOT to use low-latency GC
❌ At high CPU utilization, barriers will make it worse — application spends more time on GC than on work.
❌ Batch processing → pauses don't matter
❌ Heap < 4 GB → G1 is more efficient
Senior Level
Concurrent Evacuation
ZGC/Shenandoah move objects WITHOUT STW:
→ Application reads reference → Load Barrier
→ If object moved → Self-healing
→ G1: Evacuation = STW (limitation)
Barrier Overhead
Write Barriers (G1):
→ On every reference write
→ ~1-2 ns overhead
Load Barriers (ZGC):
→ On every reference read
→ ~5-15 ns overhead
→ But Self-healing in Fast Path
Result: ZGC is 5-15% slower
Tuning for Minimal Pauses
# ZGC
-XX:+UseZGC -XX:+ZGenerational
-XX:ConcGCThreads=4
-XX:SoftMaxHeapSize=24g
# G1
-XX:MaxGCPauseMillis=100
-XX:InitiatingHeapOccupancyPercent=45
Best Practices
- ZGC (Java 21+) for SLA < 10 ms
- G1 for SLA 100-200 ms
- Monitor Allocation Stall
- ConcGCThreads = cores / 4
- Avoid large arrays
- SoftMaxHeapSize for memory return
Senior Summary
- ZGC/Shenandoah = < 1 ms, CPU overhead 5-15% (depends on workload and Java version; Generational ZGC has different overhead profile).
- Concurrent Evacuation = key to low latency
- Allocation Stall = hidden danger
- Generational ZGC = best balance
- G1 = compromise for most applications
- Barrier overhead = price for low pauses
Interview Cheat Sheet
Must know:
- ZGC and Shenandoah: pauses < 1 ms through Concurrent Evacuation (objects moved WITHOUT STW)
- ZGC: Colored Pointers + Load Barriers (~5-15 ns overhead on read); Shenandoah: Load Reference Barriers (cheaper)
- G1: Write Barriers (~1-2 ns on write), pauses 20-200 ms through Mixed GC + regions
- Allocation Stall: application creates garbage faster than concurrent marking; throughput drops more than with Parallel GC
- Generational ZGC (Java 21+): -50% CPU overhead, throughput almost like G1, pauses still < 1 ms
- When NOT to use low-latency GC: high CPU utilization (barriers worsen it), batch processing, Heap < 4 GB
- Tuning: ZGC
-XX:ConcGCThreads=4(cores/4), G1-XX:MaxGCPauseMillis=100
Common follow-up questions:
- Why do ZGC/Shenandoah have 5-15% overhead? — Barriers are inserted on every reference read/write; CPU spends time on checks
- What happens if MaxGCPauseMillis=10 ms is set for G1? — G1 cannot fit → To-space exhausted → Full GC (seconds!)
- Why is Generational ZGC better than single-generational? — Young GC more often and faster; -50% overhead; Allocation Stalls almost disappeared
- When is G1 better than ZGC? — SLA 100-200 ms acceptable, throughput more important; CPU-bound applications; Heap < 4 GB
Red flags (DO NOT say):
- “ZGC has no overhead” — 5-15% throughput sacrifice for pauses < 1 ms
- “Allocation Stall is not dangerous” — throughput drops more than with Parallel GC
- “Low-latency GC is always better” — for batch processing or CPU-bound tasks, Parallel/G1 is more efficient
Related topics:
- [[14. What is ZGC]]
- [[15. What is Shenandoah GC]]
- [[13. What is G1 GC]]
- [[16. What is stop-the-world]]
- [[12. What GC algorithms exist]]