The Z Garbage Collector (ZGC) is one of the most advanced collectors in the JVM, designed to achieve sub-millisecond pause times, regardless of heap size. It enables Java applications to scale seamlessly into heaps ranging from a few gigabytes to multiple terabytes, all while maintaining consistent performance.
In this tutorial, we’ll explore how ZGC works, its unique design, tuning options, advantages over other collectors, and real-world production scenarios.
Why ZGC Matters
- Targets low-latency applications (trading, gaming, real-time analytics).
- Provides sub-millisecond pause times even with multi-terabyte heaps.
- Scales well with modern hardware (NUMA, large memory).
- Replaces CMS/G1 in ultra-low-latency use cases.
Analogy: If G1 cleans your house room by room, ZGC cleans while you’re walking around, without you noticing.
How ZGC Works
ZGC is a region-based, concurrent, compacting collector. Unlike CMS and G1, it uses colored pointers and load barriers to track object references without long pauses.
Key Concepts
-
Colored Pointers
- Metadata bits embedded into object pointers.
- Tracks GC state without global marking.
-
Load Barriers
- Intercept memory loads to update references.
- Allow concurrent relocation of objects.
-
Concurrent Compaction
- Moves objects in parallel with application threads.
- Eliminates fragmentation without stop-the-world compaction.
ZGC Phases
- Concurrent Mark → Identifies live objects.
- Concurrent Relocate → Moves objects while app threads run.
- Concurrent Remap → Fixes references using load barriers.
Pause times remain under 1ms, independent of heap size.
Example: Enabling ZGC
java -XX:+UseZGC -Xms4g -Xmx4g MyApplication
Tuning ZGC
-XX:+UseZGC
→ Enable ZGC.-XX:ZUncommitDelay=300
→ Release unused memory back to OS after 300s.-XX:+UnlockExperimentalVMOptions
(needed in Java 11).- Minimal tuning required; ZGC is largely self-tuning.
Advantages of ZGC
- Sub-millisecond pause times.
- Eliminates fragmentation via concurrent compaction.
- Supports multi-terabyte heaps.
- Minimal configuration required.
Limitations of ZGC
- Higher CPU overhead compared to G1 in some cases.
- Available in Java 11+, fully stable since Java 15.
- For extremely high-throughput workloads, G1 may still outperform.
Real-World Case Study
A real-time financial risk system adopted ZGC to reduce GC pauses from 200ms (CMS) to <1ms under a 2TB heap. This improved system responsiveness and reduced latency spikes during trading hours.
JVM Version Tracker
- Java 11 → ZGC introduced (experimental).
- Java 15 → ZGC became production-ready.
- Java 17 → Improved NUMA awareness and performance.
- Java 21+ → Project Lilliput reduces object header size for ZGC efficiency.
Best Practices with ZGC
- Use ZGC for latency-sensitive workloads (microservices, trading, analytics).
- Prefer default settings; tune only when necessary.
- Monitor with JDK Flight Recorder (
jfr
) or GC logs. - For smaller heaps, G1 may be more resource-efficient.
Conclusion & Key Takeaways
- ZGC delivers predictable, sub-millisecond pause times.
- It’s the best choice for large heaps and low-latency systems.
- Largely self-tuning, requiring minimal intervention.
- Modern JVMs (Java 15+) include ZGC as stable and production-ready.
FAQs
1. What is the JVM memory model and why does it matter?
It defines how threads interact with memory, ensuring correctness in concurrent programs.
2. How does ZGC differ from G1?
ZGC achieves sub-ms pauses with concurrent compaction, while G1 targets predictable pauses (~10–200ms).
3. When should I use ZGC?
For large heap, latency-sensitive apps where predictable response times are critical.
4. Does ZGC work with small heaps?
Yes, but G1 may offer better throughput efficiency on smaller heaps.
5. How does ZGC handle fragmentation?
By relocating objects concurrently, fragmentation is avoided without STW compaction.
6. What GC flags are useful for ZGC?-XX:+UseZGC
, -XX:ZUncommitDelay
, and default tuning parameters.
7. Can ZGC scale to terabytes of memory?
Yes, ZGC was designed to scale up to 16TB heaps.
8. Is ZGC stable for production?
Yes, fully stable since Java 15.
9. How does ZGC compare to Shenandoah?
Both target low-latency; Shenandoah emphasizes shorter STW phases, while ZGC scales better for huge heaps.
10. What’s next for ZGC in Project Lilliput?
Reduced object headers and improved efficiency for cloud-native, memory-constrained apps.