ZGC, the JDK's Newest Garbage Collector - Sip of Java
Billy Korando on May 30, 2022The Z Garbage Collector, ZGC, is the newest garbage collector to be added to the JDK. Originally added as an experimental feature in JDK 11, ZGC became a production feature with JEP 377 in JDK 15. In this article, we will explore the goals of ZGC, how to start using it, and the ongoing development of ZGC.
An Overview of ZGC
ZGC was designed as a low-latency, highly scalable garbage collector. The max pause times for ZGC rarely exceed 250 microseconds, yes microseconds, with average pause times in the tens of microseconds. ZGC is highly scalable, with a minimum heap size of 8 MBs to a maximum of 16 TBs. Crucially, pause times do not increase with heap size. So even with a heap several terabytes in size, pause times would still be measured in microseconds.
While ZGC provides low-latency and high scalability, this does come at the cost of throughput, with a reduction of about 10% compared to G1. The specific reduction in throughput will depend on application design, system architecture, and business needs.
Using ZGC
The G1 garbage collector was designated as the default garbage collector with JDK 9, so to use ZGC requires setting the VM flag -XX:+UseZGC
.
When using ZGC, the most important configuration is setting the maximum heap size, -Xmx
. The heap size should be large enough to handle your application’s live-set with additional headroom to allow GCs to occur. The more heap space available, the less frequent a GC will need to happen, but this needs to be balanced against memory usage.
ZGC Development
Since its release as a production feature in JDK 15, ZGC has continued to see active development. In JDK 16, Concurrent Thread-Stack Processing was implemented (JEP 376), and in JDK 18, StringDeduplication was added as an optional feature. It is also planned for ZGC to become multi-generational, though the specific timeline has not yet been set.
Additional Reading
- JEP 377
- JEP 376
- ZGC - The Future of Low-Latency Garbage Collection Is Here
- Official Documentation on ZGC
Happy coding!