Generational Shenandoah in Java 25
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
java -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational -jar app.jar
| Version | JEP | Status |
|---|---|---|
| Java 24 | JEP 404 | experimental: needed -XX:+UnlockExperimentalVMOptions |
| Java 25 | JEP 521 | product 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.
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");
}
}
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
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
5. Important Points and Best Practices
- Try it for latency-sensitive services that already use Shenandoah, and compare CPU usage and pause times.
- Set
-Xmxexplicitly 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
- Generational ZGC (Java 21)
- Garbage Collection in Java
- Compact Object Headers
- Java Flight Recorder (Java 11)
7. References
Frequently Asked Questions (FAQ)
Q1. How do I enable generational Shenandoah in Java 25?
Use -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational. No experimental unlock option is needed in Java 25.
Q2. Is generational Shenandoah the default?
No. G1 is the default collector, and within Shenandoah the default mode in Java 25 is the single-generation satb mode.
Q3. What is the difference between generational Shenandoah and generational ZGC?
Both are concurrent low-pause collectors with young and old generations, but they are separate implementations with different designs and tuning options.
Q4. Is Shenandoah available in every JDK?
It is part of OpenJDK and included in most builds, such as Eclipse Temurin, but some vendor builds may exclude it.