00:00

Generational Shenandoah in Java 25

Last updated: · Product feature in Java 25 (opt-in)

Shenandoah is a low-pause garbage collector that does most of its work concurrently with the application. Generational Shenandoah splits the heap into young and old generations. It was experimental in Java 24 (JEP 404) and became a product feature in Java 25 (JEP 521), enabled with -XX:ShenandoahGCMode=generational.

This tutorial explains the idea, how to enable the mode and how to verify which mode the JVM is using.

1. Why a Generational Mode?

Most objects die soon after they are created. A single-generation collector must still trace the whole heap to find them, which costs CPU and needs headroom. By collecting the young generation often and the old generation rarely, generational Shenandoah reclaims most garbage cheaply, aiming for lower CPU usage and better throughput with the same short pauses.

2. Enabling It

Command
java -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational -jar app.jar
VersionJEPStatus
Java 24JEP 404experimental: needed -XX:+UnlockExperimentalVMOptions
Java 25JEP 521product mode; the default mode is still single-generation (satb)

3. Practical Example

Example: Checking the active Shenandoah mode

The program reads the JVM options through HotSpotDiagnosticMXBean and then creates short-lived garbage. It is run in both modes on JDK 25.

ShenandoahMode.java
import com.sun.management.HotSpotDiagnosticMXBean;
import java.lang.management.ManagementFactory;

public class ShenandoahMode {
    public static void main(String[] args) {
        HotSpotDiagnosticMXBean vm = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
        System.out.println("UseShenandoahGC  = " + vm.getVMOption("UseShenandoahGC").getValue());
        System.out.println("ShenandoahGCMode = " + vm.getVMOption("ShenandoahGCMode").getValue());

        // Short-lived garbage: the kind of allocation a young generation handles cheaply
        long bytes = 0;
        for (int i = 0; i < 2_000_000; i++) {
            byte[] temp = new byte[256];
            bytes += temp.length;
        }
        System.out.println("Allocated " + bytes / (1024 * 1024) + " MB of temporary arrays");
    }
}
Output
UseShenandoahGC  = true
ShenandoahGCMode = generational
Allocated 488 MB of temporary arrays

Compile and run: java -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational ShenandoahMode

Command: java -XX:+UseShenandoahGC ShenandoahMode

Output
UseShenandoahGC  = true
ShenandoahGCMode = satb
Allocated 488 MB of temporary arrays

Without the mode option, Shenandoah uses its classic single-generation satb mode. Shenandoah is included in OpenJDK builds such as Eclipse Temurin; check your JDK vendor if the option is not recognised.

4. How the Generations Work

Generational Shenandoah cycle New objects are allocated in the young generation, which is collected often and concurrently; objects that survive are promoted to the old generation, which is collected less often. allocate young generation young collection frequent, concurrent promote survivors to old generation old collection less frequent
Figure 1: Concurrent marking and evacuation keep pauses short in both generations.

5. Important Points and Best Practices

  • Try it for latency-sensitive services that already use Shenandoah, and compare CPU usage and pause times.
  • Set -Xmx explicitly and leave headroom for concurrent work.
  • Use GC logging (-Xlog:gc) or JFR to compare modes.
  • G1 remains the default collector, and generational ZGC is the other low-pause option.

6. Summary

  • Java 25 (JEP 521) made generational Shenandoah a product feature.
  • Enable it with -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational.
  • Young and old generations reduce the work needed per collection.
  • It is opt-in; the default Shenandoah mode is still single-generation.

Related Tutorials

7. References