00:00

Generational ZGC in Java 21

Last updated: · Standard (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 versionHow 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.

GcInfo.java
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());
        }
    }
}
Output
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

Output
Kept 20 arrays
GC bean: ZGC Cycles
GC bean: ZGC Pauses

Command: java GcInfo

Output
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

Young and old generations in ZGC New objects are allocated in the young generation. Minor collections run often and reclaim most of them; survivors are promoted to the old generation, which major collections process less often. allocation young generation minor cycle frequent, mostly garbage promotion survivors move to old major cycle rare, whole heap
Figure 1: Most work happens in cheap minor cycles, while pauses stay below a millisecond.

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:+ZGenerational when 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

7. References