Generational ZGC in Java 21
ZGC is a scalable, low-latency garbage collector with pause times typically below a millisecond. Java 21 (JEP 439) added a generational mode that keeps young and old objects in separate generations and collects young objects more often.
This tutorial explains why generations matter, how to turn the mode on in Java 21, and how to check which collector your JVM is actually using.
1. Why Generational ZGC?
Most objects die young: request objects, temporary strings and buffers. A non-generational collector must scan the whole heap to reclaim them. By collecting the young generation frequently and the old generation less often, generational ZGC does less work per cycle, needs less spare memory and uses less CPU, while keeping ZGC's very short pauses.
2. Enabling It
| Java version | How to get generational ZGC |
|---|---|
| Java 21 | -XX:+UseZGC -XX:+ZGenerational |
| Java 23 | -XX:+UseZGC (generational is the default, JEP 474) |
| Java 24 and later | -XX:+UseZGC (non-generational mode removed, JEP 490) |
3. Practical Example
Example: Confirming the active collector
Every collector registers GarbageCollectorMXBeans with recognisable names. The same program is run three times with different JVM options.
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;
public class GcInfo {
public static void main(String[] args) {
// Create some short-lived garbage
List<byte[]> keep = new ArrayList<>();
for (int i = 0; i < 2_000; i++) {
byte[] temp = new byte[10_000];
if (i % 100 == 0) {
keep.add(temp);
}
}
System.out.println("Kept " + keep.size() + " arrays");
// Each collector registers its own management beans
for (GarbageCollectorMXBean gc : ManagementFactory.getGarbageCollectorMXBeans()) {
System.out.println("GC bean: " + gc.getName());
}
}
}
Kept 20 arrays GC bean: ZGC Minor Cycles GC bean: ZGC Minor Pauses GC bean: ZGC Major Cycles GC bean: ZGC Major Pauses
Compile and run: java -XX:+UseZGC -XX:+ZGenerational GcInfo
Command: java -XX:+UseZGC GcInfo
Kept 20 arrays GC bean: ZGC Cycles GC bean: ZGC Pauses
Command: java GcInfo
Kept 20 arrays GC bean: G1 Young Generation GC bean: G1 Concurrent GC GC bean: G1 Old Generation
In Java 21, -XX:+UseZGC alone still selects the older, non-generational mode ("ZGC Cycles"). Only with -XX:+ZGenerational do you see separate minor and major cycles. Without any option, the default G1 collector is used.
4. How Generational ZGC Works
5. Important Points and Best Practices
- Consider ZGC for latency-sensitive services with large heaps; G1 remains the default and a good general choice.
- On Java 21, always add
-XX:+ZGenerationalwhen choosing ZGC. - Set the maximum heap with
-Xmx; ZGC needs some headroom to work concurrently. - Verify the effect with GC logs (
-Xlog:gc) or JFR before and after switching.
6. Summary
- Java 21 (JEP 439) added a generational mode to ZGC.
- Enable it on Java 21 with
-XX:+UseZGC -XX:+ZGenerational. - It reduces CPU and memory overhead while keeping very short pauses.
- It became the default in Java 23 and the only ZGC mode in Java 24.
Related Tutorials
- Garbage Collection in Java
- Generational Shenandoah (Java 25)
- Java Flight Recorder (Java 11)
- Virtual Threads
7. References
Frequently Asked Questions (FAQ)
Q1. How do I enable generational ZGC in Java 21?
Use -XX:+UseZGC -XX:+ZGenerational. In Java 21, -XX:+UseZGC alone selects the non-generational mode.
Q2. Is generational ZGC the default garbage collector?
No. G1 is the default collector. Within ZGC, generational mode became the default in Java 23.
Q3. How can I check which ZGC mode is active?
Look at the garbage collector MXBean names: generational ZGC reports "ZGC Minor Cycles" and "ZGC Major Cycles", the older mode reports "ZGC Cycles".
Q4. What happened to non-generational ZGC?
It was deprecated when generational mode became the default in Java 23 and removed in Java 24 (JEP 490).