Generational ZGC - Sip of JavaBilly Korando on October 9, 2023
The Z Garbage Collector, often shortened to ZGC, received a significant update in JDK 21. ZGC is now a multi-generational garbage collector, typically called Generational ZGC or GenZGC. Let’s take a look at this update to ZGC!
Multi-Generational Garbage Collectors
Multi-generational garbage collectors, or just generational garbage collectors, are garbage collectors that logically divide the heap into two regions. A Young Region and an Old Region. This is done to take advantage of the Weak-Generational Hypothesis, which posits that many objects die shortly after their creation.
As new objects are allocated, they are initially placed in the Young Region.
The garbage collector frequently scans the Young Region, looking for dead objects. If an object survives enough GC cycles, it’s promoted to the Old Region, which is scanned less often. This design allows garbage collectors to be more efficient with CPU resources by focusing on a portion of the heap that is more likely to contain dead objects.
Using Generational ZGC
When ZGC was initially released, it was a single-generation garbage collector. Starting with JDK 21, and in addition to single-generation, multi-generation support is now also available in ZGC. To use GenZGC requires passing two VM arguments:
$ java -XX:+UseZGC -XX:+ZGenerational ...
The plan is to make GenZGC the default and eventually deprecate and remove single-generation ZGC, but the timeline for that transition is not yet certain.