Add gc-basics: Java garbage collection explained and measured on JDK 25, 27, 28 EA

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b44MquEzmd9QMkZCd7
This commit is contained in:
asmhatre
2026-11-24 06:46:39 +05:30
co-authored by Claude Sonnet 5.5
parent f7b6c1efca
commit 876ae1bb5c
36 changed files with 948 additions and 1 deletions
+2
View File
@@ -0,0 +1,2 @@
target/
*.class
+59
View File
@@ -0,0 +1,59 @@
# gc-basics
Companion code for the ankurm.com article **"Java Garbage Collection Explained: How It Works and Which Collector to Choose (G1, ZGC, Shenandoah on JDK 25 to 28)"**
(<https://ankurm.com/a-developers-deep-dive-into-java-memory-management-and-garbage-collection/>).
No `docs/` folder: the explanation lives in the article, in collapsible sections. This module keeps only the sources, one script and the real output of every run.
| Thing | Version |
|---|---|
| JDK 25 (LTS) | Temurin 25.0.5.1+2 |
| JDK 27 | Temurin 27.0.1.1+4 (GA 2026-09-15) |
| JDK 28 | Temurin 28+25 early-access build 2026-11-19 (GA scheduled for March 2027, so anything seen only there may change) |
| Containers | `eclipse-temurin:25-jre`, `eclipse-temurin:27-jre` (step 22 only) |
```bash
JDK25=/path/to/jdk-25 JDK27=/path/to/jdk-27 JDK28=/path/to/jdk-28 ./run.sh # regenerate every file in output/ (about 25 minutes)
./run.sh 07 15 # or just some steps
```
Timings (stalls, requests) depend on the machine; this was a 2 vCPU sandbox. Read them as shape, not as results. Everything else is stable.
## Sources (`src/`)
| File | What it shows |
|---|---|
| `Which.java` | which collector the JVM picked, read from the JVM's own `GarbageCollectorMXBean` names |
| `StackAndHeap.java`, `TryWithResources.java` | the two small snippets from the article |
| `Reach.java` | reachability observed with weak references: roots, a child, an unreferenced object, a two-object cycle |
| `Churn.java` | the workload: a live set (`flat` or `graph`) plus a stream of short-lived garbage, and a ticker thread that measures how late a 1 ms sleep wakes up |
| `SysGc.java` | what one `System.gc()` costs |
| `Leak.java` | a static collection that only grows, ending in `OutOfMemoryError` |
| `Finalize.java` | `finalize()` today: the compiler warning and `--finalization=disabled` |
## Captured output (`output/`)
| File | Step | What it is |
|---|---|---|
| `01` | `./run.sh 01` | default collector on JDK 25, 27, 28 for 1, 2 and 4 CPUs |
| `02` | `02` | `-XX:MaxRAM` on 25, 27, 28 |
| `03` | `03` | Shenandoah mode on 25, 27, 28 |
| `04` | `04` | ZGC: removed mode, flags new on 28 |
| `05` | `05` | reachability |
| `06` | `06` | reading a G1 log |
| `07` | `07` | five collectors, graph workload, 1 GB heap, three runs each |
| `08` | `08` | the default on 25, 27, 28 and Shenandoah on 28 |
| `09` | `09` | `jcmd GC.heap_info` per collector |
| `10` | `10` | `OutOfMemoryError` |
| `11` | `11` | `System.gc()` |
| `12` | `12` | `finalize()` |
| `13` | `13` | the two small snippets run |
| `14` | `14` | the flat live set baseline |
| `15` | `15` | G1 pause types |
| `16` | `16` | Native Memory Tracking |
| `17` | `17` | a few flag defaults |
| `18`, `20`, `21` | `18`, `20`, `21` | Shenandoah by JDK: stalls, pause types, allocation stalls |
| `19` | `19` | the same workload with a 2 GB heap |
| `22` | `22` | default collector under real `docker run --cpus --memory` limits |
MIT licensed, see the repository root.
+4
View File
@@ -0,0 +1,4 @@
src/Finalize.java:13: warning: [removal] finalize() in Object has been deprecated and marked for removal
protected void finalize() { // deliberately not suppressed: javac prints a removal warning
^
1 warning
+1
View File
@@ -0,0 +1 @@
versions: OpenJDK Runtime Environment Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS) | OpenJDK Runtime Environment Temurin-27.0.1.1+4 (build 27.0.1.1+4) | OpenJDK Runtime Environment Temurin-28+25-202611191755 (build 28-beta+25-ea)
+25
View File
@@ -0,0 +1,25 @@
# Which collector does the JVM pick when you name none? (emulating a 1, 2 and 4 CPU machine with -XX:ActiveProcessorCount)
## JDK 25: OpenJDK Runtime Environment Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS)
$ java -XX:ActiveProcessorCount=1 Which
collector MXBeans : Copy | MarkSweepCompact
$ java -XX:ActiveProcessorCount=2 Which
collector MXBeans : G1 Young Generation | G1 Concurrent GC | G1 Old Generation
$ java -XX:ActiveProcessorCount=4 Which
collector MXBeans : G1 Young Generation | G1 Concurrent GC | G1 Old Generation
## JDK 27: OpenJDK Runtime Environment Temurin-27.0.1.1+4 (build 27.0.1.1+4)
$ java -XX:ActiveProcessorCount=1 Which
collector MXBeans : G1 Young Generation | G1 Concurrent GC | G1 Old Generation
$ java -XX:ActiveProcessorCount=2 Which
collector MXBeans : G1 Young Generation | G1 Concurrent GC | G1 Old Generation
$ java -XX:ActiveProcessorCount=4 Which
collector MXBeans : G1 Young Generation | G1 Concurrent GC | G1 Old Generation
## JDK 28: OpenJDK Runtime Environment Temurin-28+25-202611191755 (build 28-beta+25-ea)
$ java -XX:ActiveProcessorCount=1 Which
collector MXBeans : G1 Young Generation | G1 Concurrent GC | G1 Old Generation
$ java -XX:ActiveProcessorCount=2 Which
collector MXBeans : G1 Young Generation | G1 Concurrent GC | G1 Old Generation
$ java -XX:ActiveProcessorCount=4 Which
collector MXBeans : G1 Young Generation | G1 Concurrent GC | G1 Old Generation
+14
View File
@@ -0,0 +1,14 @@
# The memory half of the rule cannot be emulated with -XX:MaxRAM on 27 or later
## JDK 25
$ java -XX:MaxRAM=1g -XX:+PrintFlagsFinal -version | grep MaxRAM
uint64_t MaxRAM = 1073741824 {pd product} {command line}
## JDK 27
$ java -XX:MaxRAM=1g -XX:+PrintFlagsFinal -version | grep MaxRAM
OpenJDK 64-Bit Server VM warning: Ignoring option MaxRAM; support was removed in 27.0
## JDK 28
$ java -XX:MaxRAM=1g -XX:+PrintFlagsFinal -version | grep MaxRAM
Unrecognized VM option 'MaxRAM=1g'
Error: Could not create the Java Virtual Machine.
+25
View File
@@ -0,0 +1,25 @@
# Shenandoah: which mode is the default, and is the generational mode still experimental? (mode read from -Xlog:gc+init)
## JDK 25, no mode flag
$ java -XX:+UseShenandoahGC -Xlog:gc+init -version | grep Mode
[0.008s][info][gc,init] Mode: Snapshot-At-The-Beginning (SATB)
## JDK 27, no mode flag
$ java -XX:+UseShenandoahGC -Xlog:gc+init -version | grep Mode
[0.007s][info][gc,init] Mode: Snapshot-At-The-Beginning (SATB)
## JDK 28, no mode flag
$ java -XX:+UseShenandoahGC -Xlog:gc+init -version | grep Mode
[0.014s][info][gc,init] Mode: generational
## JDK 25: generational mode without -XX:+UnlockExperimentalVMOptions
$ java -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational -Xlog:gc+init -version
[0.011s][info][gc,init] Mode: Generational
## JDK 24-style spelling on 25 (the unlock flag is now unnecessary but harmless)
[0.012s][info][gc,init] Mode: Generational
## JDK 28 early access: asking for the old mode explicitly
$ java -XX:+UseShenandoahGC -XX:ShenandoahGCMode=satb -Xlog:gc+init -version
[0.007s][info][gc,init] Mode: satb
(no deprecation warning is printed by this early-access build)
+20
View File
@@ -0,0 +1,20 @@
# ZGC: the non-generational mode is gone, and what 28 adds
## JDK 25: ask for the old mode
$ java -XX:+UseZGC -XX:-ZGenerational -version
OpenJDK 64-Bit Server VM warning: Ignoring option ZGenerational; support was removed in 24.0
openjdk version "25.0.5.1" 2026-11-17 LTS
## JDK 25: plain ZGC
collector MXBeans : ZGC Minor Cycles | ZGC Minor Pauses | ZGC Major Cycles | ZGC Major Pauses
## ZGC flags present on 28 early access but not on 27 (diff of -XX:+PrintFlagsFinal -XX:+UseZGC, names only)
only on 28:
ZAdaptiveHeapSizing
ZGCIntensity
ZMemoryHeating
only on 27:
## its default on 28
size_t SoftMaxHeapSize = 1449551462 {manageable} {ergonomic}
bool ZAdaptiveHeapSizing = false {product} {default}
+8
View File
@@ -0,0 +1,8 @@
# Reachability observed with weak references (JDK 25)
A (local variable) alive
B (static field) alive
C (child of A) alive
D (no references) collected
X (cycle with Y, no root) collected
Y (cycle with X, no root) collected
A and C are still used here: A -> C
+29
View File
@@ -0,0 +1,29 @@
# Reading a G1 log: the first lines of -Xlog:gc for the Churn workload (JDK 25, -Xms512m -Xmx512m, 150 MB live)
$ java -Xms512m -Xmx512m -Xlog:gc Churn 150 3
[0.004s][info][gc] Using G1
[0.044s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 26M->25M(512M) 12.661ms
[0.209s][info][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 205M->164M(512M) 88.487ms
[0.244s][info][gc] GC(2) Pause Young (Normal) (G1 Evacuation Pause) 283M->175M(512M) 17.136ms
[0.268s][info][gc] GC(3) Pause Young (Normal) (G1 Evacuation Pause) 318M->176M(512M) 1.967ms
[0.298s][info][gc] GC(4) Pause Young (Normal) (G1 Evacuation Pause) 340M->176M(512M) 2.303ms
[0.332s][info][gc] GC(5) Pause Young (Normal) (G1 Evacuation Pause) 356M->176M(512M) 2.059ms
[0.386s][info][gc] GC(6) Pause Young (Normal) (G1 Evacuation Pause) 387M->176M(512M) 10.611ms
[0.439s][info][gc] GC(7) Pause Young (Normal) (G1 Evacuation Pause) 407M->176M(512M) 10.162ms
[0.487s][info][gc] GC(8) Pause Young (Normal) (G1 Evacuation Pause) 424M->177M(512M) 6.242ms
[0.541s][info][gc] GC(9) Pause Young (Normal) (G1 Evacuation Pause) 438M->177M(512M) 8.665ms
[0.591s][info][gc] GC(10) Pause Young (Normal) (G1 Evacuation Pause) 447M->177M(512M) 8.525ms
[0.642s][info][gc] GC(11) Pause Young (Normal) (G1 Evacuation Pause) 455M->178M(512M) 7.669ms
[0.694s][info][gc] GC(12) Pause Young (Normal) (G1 Evacuation Pause) 462M->178M(512M) 7.269ms
[0.748s][info][gc] GC(13) Pause Young (Normal) (G1 Evacuation Pause) 467M->178M(512M) 8.543ms
[0.801s][info][gc] GC(14) Pause Young (Normal) (G1 Evacuation Pause) 471M->178M(512M) 6.383ms
[0.857s][info][gc] GC(15) Pause Young (Normal) (G1 Evacuation Pause) 471M->179M(512M) 5.703ms
[0.910s][info][gc] GC(16) Pause Young (Normal) (G1 Evacuation Pause) 472M->179M(512M) 5.622ms
[0.962s][info][gc] GC(17) Pause Young (Normal) (G1 Evacuation Pause) 471M->179M(512M) 5.201ms
[1.014s][info][gc] GC(18) Pause Young (Normal) (G1 Evacuation Pause) 471M->180M(512M) 5.320ms
[1.065s][info][gc] GC(19) Pause Young (Normal) (G1 Evacuation Pause) 472M->180M(512M) 5.347ms
[1.117s][info][gc] GC(20) Pause Young (Normal) (G1 Evacuation Pause) 472M->180M(512M) 5.279ms
[1.168s][info][gc] GC(21) Pause Young (Normal) (G1 Evacuation Pause) 472M->180M(512M) 4.655ms
[1.217s][info][gc] GC(22) Pause Young (Normal) (G1 Evacuation Pause) 472M->180M(512M) 3.155ms
# the same run, counting what G1 did
88 Pause Young (Normal) (G1 Evacuation Pause)
+42
View File
@@ -0,0 +1,42 @@
# Same workload, five collectors, JDK 25, -Xms1g -Xmx1g, 450 MB live set of small linked objects that are constantly replaced, 8 s measured after a 1.5 s warm-up, three runs each
# (2 vCPU sandbox: shape, not result. 'requests' counts completed loop iterations, a rough throughput signal)
## -XX:+UseSerialGC
run 1 requests : 2077033 (10135 MB allocated)
run 1 ticker max stall: 576.0 ms; wakeups >10 ms: 38; >50 ms: 26
run 2 requests : 2026832 (9889 MB allocated)
run 2 ticker max stall: 595.4 ms; wakeups >10 ms: 37; >50 ms: 24
run 3 requests : 1942997 (9479 MB allocated)
run 3 ticker max stall: 586.2 ms; wakeups >10 ms: 37; >50 ms: 17
## -XX:+UseParallelGC
run 1 requests : 957464 (4672 MB allocated)
run 1 ticker max stall: 538.7 ms; wakeups >10 ms: 19; >50 ms: 19
run 2 requests : 906922 (4425 MB allocated)
run 2 ticker max stall: 553.8 ms; wakeups >10 ms: 18; >50 ms: 18
run 3 requests : 908001 (4429 MB allocated)
run 3 ticker max stall: 536.8 ms; wakeups >10 ms: 18; >50 ms: 18
## -XX:+UseG1GC
run 1 requests : 3227925 (15750 MB allocated)
run 1 ticker max stall: 566.5 ms; wakeups >10 ms: 40; >50 ms: 35
run 2 requests : 3255959 (15885 MB allocated)
run 2 ticker max stall: 575.7 ms; wakeups >10 ms: 40; >50 ms: 40
run 3 requests : 3132846 (15284 MB allocated)
run 3 ticker max stall: 515.3 ms; wakeups >10 ms: 38; >50 ms: 35
## -XX:+UseZGC
run 1 requests : 5195766 (25350 MB allocated)
run 1 ticker max stall: 12.4 ms; wakeups >10 ms: 1; >50 ms: 0
run 2 requests : 5701049 (27813 MB allocated)
run 2 ticker max stall: 8.3 ms; wakeups >10 ms: 0; >50 ms: 0
run 3 requests : 5588330 (27264 MB allocated)
run 3 ticker max stall: 13.0 ms; wakeups >10 ms: 1; >50 ms: 0
## -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
run 1 requests : 1742398 (8501 MB allocated)
run 1 ticker max stall: 11.1 ms; wakeups >10 ms: 1; >50 ms: 0
run 2 requests : 1680239 (8197 MB allocated)
run 2 ticker max stall: 11.2 ms; wakeups >10 ms: 2; >50 ms: 0
run 3 requests : 1684136 (8216 MB allocated)
run 3 ticker max stall: 5.0 ms; wakeups >10 ms: 0; >50 ms: 0
@@ -0,0 +1,33 @@
# The defaults on each release, same workload and heap as 07, two runs each
## JDK 25: OpenJDK Runtime Environment Temurin-25.0.5.1+2 (build 25.0.5.1+2-LTS), no collector flag
run 1 collector beans : G1 Young Generation G1 Concurrent GC G1 Old Generation
run 1 requests : 3243911 (15827 MB allocated)
run 1 ticker max stall: 78.1 ms; wakeups >10 ms: 37; >50 ms: 37
run 2 collector beans : G1 Young Generation G1 Concurrent GC G1 Old Generation
run 2 requests : 3279760 (16002 MB allocated)
run 2 ticker max stall: 534.3 ms; wakeups >10 ms: 39; >50 ms: 36
## JDK 27: OpenJDK Runtime Environment Temurin-27.0.1.1+4 (build 27.0.1.1+4), no collector flag
run 1 collector beans : G1 Young Generation G1 Concurrent GC G1 Old Generation
run 1 requests : 4914119 (23976 MB allocated)
run 1 ticker max stall: 600.6 ms; wakeups >10 ms: 53; >50 ms: 51
run 2 collector beans : G1 Young Generation G1 Concurrent GC G1 Old Generation
run 2 requests : 4690805 (22886 MB allocated)
run 2 ticker max stall: 512.3 ms; wakeups >10 ms: 53; >50 ms: 49
## JDK 28: OpenJDK Runtime Environment Temurin-28+25-202611191755 (build 28-beta+25-ea), no collector flag
run 1 collector beans : G1 Young Generation G1 Concurrent GC G1 Old Generation
run 1 requests : 4456120 (21741 MB allocated)
run 1 ticker max stall: 567.4 ms; wakeups >10 ms: 51; >50 ms: 49
run 2 collector beans : G1 Young Generation G1 Concurrent GC G1 Old Generation
run 2 requests : 4790262 (23369 MB allocated)
run 2 ticker max stall: 503.2 ms; wakeups >10 ms: 52; >50 ms: 49
## JDK 28 early access, -XX:+UseShenandoahGC with no mode flag
run 1 collector beans : Shenandoah Pauses Shenandoah Cycles
run 1 requests : 1758008 (8578 MB allocated)
run 1 ticker max stall: 301.2 ms; wakeups >10 ms: 29; >50 ms: 25
run 2 collector beans : Shenandoah Pauses Shenandoah Cycles
run 2 requests : 1760812 (8592 MB allocated)
run 2 ticker max stall: 289.3 ms; wakeups >10 ms: 27; >50 ms: 22
+30
View File
@@ -0,0 +1,30 @@
# jcmd GC.heap_info on a live process, per collector (JDK 25, 512 MB heap, 150 MB live)
## -XX:+UseParallelGC
PSYoungGen total 163328K, used 67168K [0x00000000f5580000, 0x0000000100000000, 0x0000000100000000)
eden space 152064K, 37% used [0x00000000f5580000,0x00000000f8c783c8,0x00000000fea00000)
from space 11264K, 96% used [0x00000000ff500000,0x00000000fffa0000,0x0000000100000000)
to space 11264K, 0% used [0x00000000fea00000,0x00000000fea00000,0x00000000ff500000)
ParOldGen total 349696K, used 216440K [0x00000000e0000000, 0x00000000f5580000, 0x00000000f5580000)
object space 349696K, 61% used [0x00000000e0000000,0x00000000ed35e2e0,0x00000000f5580000)
## -XX:+UseG1GC
garbage-first heap total reserved 524288K, committed 524288K, used 214460K [0x00000000e0000000, 0x0000000100000000)
region size 1024K, 24 young (24576K), 15 survivors (15360K)
## -XX:+UseZGC
ZHeap used 320M, capacity 512M, max capacity 512M
Cache 192M (23)
size classes 2M (12), 4M (7), 8M (2), 32M (1), 64M (1)
## -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
Shenandoah Heap
512M max, 512M soft max, 512M committed, 261M used
2048 x 256 K regions
Status: not cancelled
Reserved region:
- [0x00000000e0000000, 0x0000000100000000)
Collection set:
- map (vanilla): 0x0000000000004800
- map (biased): 0x0000000000001000
+10
View File
@@ -0,0 +1,10 @@
# Running out of memory: the message depends on how the collector fails (JDK 25, -Xmx64m)
## G1, 1 MB arrays
kept 31 arrays of 1 MB then: java.lang.OutOfMemoryError: Java heap space
## G1, tiny objects
kept 1215487 tiny objects then: java.lang.OutOfMemoryError: Java heap space
## Parallel, tiny objects
kept 1160801 tiny objects then: java.lang.OutOfMemoryError: Java heap space
+31
View File
@@ -0,0 +1,31 @@
# What one System.gc() costs (JDK 25, G1, 150 MB live)
## default
[0.187s][info][gc] GC(7) Pause Full (System.gc()) 156M->154M(267M) 22.122ms
System.gc() #1 took 22.5 ms
[0.224s][info][gc] GC(8) Pause Full (System.gc()) 155M->154M(267M) 18.764ms
System.gc() #2 took 18.9 ms
[0.238s][info][gc] GC(9) Pause Full (System.gc()) 155M->154M(267M) 13.827ms
System.gc() #3 took 14.0 ms
live chunks still referenced: 153600
## -XX:+DisableExplicitGC
System.gc() #1 took 0.1 ms
System.gc() #2 took 0.0 ms
System.gc() #3 took 0.0 ms
live chunks still referenced: 153600
## -XX:+ExplicitGCInvokesConcurrent
[0.203s][info][gc] GC(7) Pause Young (Concurrent Start) (System.gc()) 168M->168M(267M) 8.496ms
System.gc() #1 took 34.4 ms
[0.261s][info][gc] GC(9) Pause Young (Concurrent Start) (System.gc()) 168M->169M(285M) 4.044ms
System.gc() #2 took 31.7 ms
[0.290s][info][gc] GC(11) Pause Young (Concurrent Start) (System.gc()) 169M->168M(287M) 0.919ms
System.gc() #3 took 22.5 ms
live chunks still referenced: 153600
## ZGC
System.gc() #1 took 47.5 ms
System.gc() #2 took 23.6 ms
System.gc() #3 took 19.2 ms
live chunks still referenced: 153600
+28
View File
@@ -0,0 +1,28 @@
# finalize() today
## what javac says (JDK 25, -Xlint:deprecation)
src/Finalize.java:13: warning: [removal] finalize() in Object has been deprecated and marked for removal
protected void finalize() { // deliberately not suppressed: javac prints a removal warning
^
1 warning
## JDK 25, default
finalize() ran
main done
## JDK 25, --finalization=disabled
main done
## JDK 27, default
finalize() ran
main done
## JDK 27, --finalization=disabled
main done
## JDK 28, default
finalize() ran
main done
## JDK 28, --finalization=disabled
main done
+4
View File
@@ -0,0 +1,4 @@
# the other two snippets in the article, run
10 Hello
read byte count: 5
stream closed deterministically at the end of the try block
@@ -0,0 +1,21 @@
# The baseline: a FLAT live set (one list of 1 KB byte arrays, no pointers inside), JDK 25, -Xms512m -Xmx512m, 150 MB live, 8 s, one run each
## -XX:+UseSerialGC
requests : 10735539 (52378 MB allocated)
ticker max stall: 8.6 ms; wakeups >10 ms: 0; >50 ms: 0
## -XX:+UseParallelGC
requests : 13025302 (63552 MB allocated)
ticker max stall: 5.0 ms; wakeups >10 ms: 0; >50 ms: 0
## -XX:+UseG1GC
requests : 8873404 (43294 MB allocated)
ticker max stall: 7.2 ms; wakeups >10 ms: 0; >50 ms: 0
## -XX:+UseZGC
requests : 13262536 (64709 MB allocated)
ticker max stall: 6.3 ms; wakeups >10 ms: 0; >50 ms: 0
## -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
requests : 9827770 (47951 MB allocated)
ticker max stall: 5.3 ms; wakeups >10 ms: 0; >50 ms: 0
+37
View File
@@ -0,0 +1,37 @@
# Why is G1's worst case so uneven? Pause types and the three longest pauses in three G1 runs of the graph workload (JDK 25, -Xms1g -Xmx1g, 450 MB live)
## run 1
pause counts by type:
38 Pause Young (Normal)
5 Pause Young (Mixed)
5 Pause Young (Concurrent Start)
4 Pause Young (Prepare Mixed)
1 Pause Full (G1 Compaction Pause)
three longest pauses:
GC(51) Pause Full (G1 Compaction Pause) 1024M->464M(1024M) 467.261ms
GC(2) Pause Young (Normal) (G1 Evacuation Pause) 409M->409M(1024M) 109.383ms
GC(49) Pause Young (Mixed) (G1 Evacuation Pause) (Evacuation Failure: Allocation) 971M->975M(1024M) 106.895ms
## run 2
pause counts by type:
38 Pause Young (Normal)
5 Pause Young (Mixed)
5 Pause Young (Concurrent Start)
4 Pause Young (Prepare Mixed)
1 Pause Full (G1 Compaction Pause)
three longest pauses:
GC(51) Pause Full (G1 Compaction Pause) 1023M->464M(1024M) 467.847ms
GC(49) Pause Young (Mixed) (G1 Evacuation Pause) (Evacuation Failure: Allocation) 970M->979M(1024M) 132.893ms
GC(2) Pause Young (Normal) (G1 Evacuation Pause) 410M->410M(1024M) 111.761ms
## run 3
pause counts by type:
37 Pause Young (Normal)
5 Pause Young (Mixed)
5 Pause Young (Concurrent Start)
4 Pause Young (Prepare Mixed)
1 Pause Full (G1 Compaction Pause)
three longest pauses:
GC(51) Pause Full (G1 Compaction Pause) 1024M->464M(1024M) 507.602ms
GC(49) Pause Young (Mixed) (G1 Evacuation Pause) (Evacuation Failure: Allocation) 971M->975M(1024M) 121.870ms
GC(2) Pause Young (Normal) (G1 Evacuation Pause) 409M->410M(1024M) 118.036ms
+25
View File
@@ -0,0 +1,25 @@
# Heap is not the whole process: Native Memory Tracking on a live JVM (JDK 25, G1, -Xmx512m, 150 MB live)
$ jcmd <pid> VM.native_memory summary (category totals only)
Total: reserved=1973775KB, committed=597955KB
Java Heap (reserved=524288KB, committed=524288KB)
Class (reserved=1048647KB, committed=199KB)
Thread (reserved=20545KB, committed=1053KB)
Code (reserved=247794KB, committed=7650KB)
GC (reserved=47599KB, committed=47599KB)
GCCardSet (reserved=9KB, committed=9KB)
Compiler (reserved=214KB, committed=214KB)
Internal (reserved=1297KB, committed=1297KB)
Symbol (reserved=1131KB, committed=1131KB)
Native Memory Tracking (reserved=180KB, committed=180KB)
Shared class space (reserved=16384KB, committed=13928KB, readonly=0KB)
Arena Chunk (reserved=1KB, committed=1KB)
Tracing (reserved=5KB, committed=5KB)
Module (reserved=40KB, committed=40KB)
Safepoint (reserved=8KB, committed=8KB)
Synchronization (reserved=66KB, committed=66KB)
Serviceability (reserved=17KB, committed=17KB)
Metaspace (reserved=65548KB, committed=268KB)
String Deduplication (reserved=1KB, committed=1KB)
Object Monitors (reserved=1KB, committed=1KB)
resident set size of the process, from /proc: 549.477 MB
+11
View File
@@ -0,0 +1,11 @@
# A few defaults worth knowing (JDK 25)
$ java -Xmx1g -XX:+UseG1GC -XX:+PrintFlagsFinal -version | grep ...
uint ConcGCThreads = 1 {product} {ergonomic}
size_t G1HeapRegionSize = 1048576 {product} {ergonomic}
uint InitiatingHeapOccupancyPercent = 45 {product} {default}
uintx MaxGCPauseMillis = 200 {product} {default}
uint ParallelGCThreads = 2 {product} {default}
$ java -Xmx1g -XX:+UseSerialGC -XX:+PrintFlagsFinal -version | grep -E 'NewRatio|SurvivorRatio|MaxTenuringThreshold'
uint MaxTenuringThreshold = 15 {product} {default}
uintx NewRatio = 2 {product} {default}
uintx SurvivorRatio = 8 {product} {default}
+25
View File
@@ -0,0 +1,25 @@
# Shenandoah by JDK and mode, same graph workload as 07 (-Xms1g -Xmx1g, 450 MB live), one run each
## JDK 25, -XX:ShenandoahGCMode=satb
requests : 539727 (2633 MB allocated)
ticker max stall: 11.3 ms; wakeups >10 ms: 2; >50 ms: 0
## JDK 25, -XX:ShenandoahGCMode=generational
requests : 1710722 (8347 MB allocated)
ticker max stall: 10.7 ms; wakeups >10 ms: 1; >50 ms: 0
## JDK 27, -XX:ShenandoahGCMode=satb
requests : 652517 (3183 MB allocated)
ticker max stall: 1125.5 ms; wakeups >10 ms: 11; >50 ms: 11
## JDK 27, -XX:ShenandoahGCMode=generational
requests : 1075858 (5249 MB allocated)
ticker max stall: 1345.8 ms; wakeups >10 ms: 16; >50 ms: 16
## JDK 28, -XX:ShenandoahGCMode=satb
requests : 750947 (3663 MB allocated)
ticker max stall: 952.4 ms; wakeups >10 ms: 14; >50 ms: 14
## JDK 28, -XX:ShenandoahGCMode=generational
requests : 1854932 (9052 MB allocated)
ticker max stall: 307.9 ms; wakeups >10 ms: 27; >50 ms: 25
+21
View File
@@ -0,0 +1,21 @@
# Headroom: the graph workload again with a 2 GB heap instead of 1 GB (450 MB live is now 22 percent of the heap), JDK 25, one run each
## -XX:+UseSerialGC
requests : 4584936 (22369 MB allocated)
ticker max stall: 121.8 ms; wakeups >10 ms: 43; >50 ms: 43
## -XX:+UseParallelGC
requests : 6845614 (33401 MB allocated)
ticker max stall: 52.3 ms; wakeups >10 ms: 68; >50 ms: 1
## -XX:+UseG1GC
requests : 5148123 (25117 MB allocated)
ticker max stall: 132.1 ms; wakeups >10 ms: 23; >50 ms: 23
## -XX:+UseZGC
requests : 6501613 (31722 MB allocated)
ticker max stall: 8.1 ms; wakeups >10 ms: 0; >50 ms: 0
## -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational
requests : 2706531 (13206 MB allocated)
ticker max stall: 6.9 ms; wakeups >10 ms: 0; >50 ms: 0
@@ -0,0 +1,33 @@
# What Shenandoah did on each JDK for the graph workload (-Xms1g -Xmx1g, 450 MB live, generational mode, one run each): pause types and the three longest pauses
## JDK 25
pause counts by type:
42 Pause Final Mark
41 Pause Init Mark
38 Pause Init Update Refs
38 Pause Final Update Refs
three longest pauses:
GC(15) Pause Final Mark (Young) 5.844ms
GC(8) Pause Final Update Refs 1.035ms
GC(14) Pause Final Update Refs 0.412ms
## JDK 27
pause counts by type:
16 Pause Init Mark
15 Pause Degenerated GC
5 Pause Final Mark
3 Pause Full
three longest pauses:
GC(27) Pause Full 993M->434M(1024M) 1304.922ms
GC(18) Pause Full 993M->434M(1024M) 1210.941ms
GC(9) Pause Full 1024M->433M(1024M) 1152.424ms
## JDK 28
pause counts by type:
36 Pause Init Mark
35 Pause Final Mark
34 Pause Final Update Refs
three longest pauses:
GC(1) Pause Final Mark (Young) 0.321ms
GC(6) Pause Final Mark (Global) 0.265ms
GC(18) Pause Final Mark (Young) 0.232ms
@@ -0,0 +1,21 @@
# A pause-free log is not a stall-free application: Shenandoah allocation stalls (generational mode, graph workload, -Xms1g -Xmx1g, 450 MB live, one run each)
## JDK 25
allocation stalls logged : 0
longest allocation stalls :
longest stop-the-world pause: 0.913ms
ticker max stall: 5.2 ms; wakeups >10 ms: 0; >50 ms: 0
## JDK 27
allocation stalls logged : 0
longest allocation stalls :
longest stop-the-world pause: 1293.419ms
ticker max stall: 1292.7 ms; wakeups >10 ms: 15; >50 ms: 15
## JDK 28
allocation stalls logged : 83
longest allocation stalls :
Allocation Stall (main) 285.067ms
Allocation Stall (main) 282.400ms
longest stop-the-world pause: 0.372ms
ticker max stall: 281.3 ms; wakeups >10 ms: 28; >50 ms: 22
@@ -0,0 +1,19 @@
# The default collector inside real container limits (docker run --cpus --memory; needs Docker and the eclipse-temurin:25-jre and :27-jre images)
## eclipse-temurin:25-jre
--cpus=1 --memory=4g -> UseSerialGC
--cpus=2 --memory=1g -> UseSerialGC
--cpus=2 --memory=1500m -> UseSerialGC
--cpus=2 --memory=1791m -> UseSerialGC
--cpus=2 --memory=1792m -> UseG1GC
--cpus=2 --memory=1800m -> UseG1GC
--cpus=2 --memory=2g -> UseG1GC
## eclipse-temurin:27-jre
--cpus=1 --memory=4g -> UseG1GC
--cpus=2 --memory=1g -> UseG1GC
--cpus=2 --memory=1500m -> UseG1GC
--cpus=2 --memory=1791m -> UseG1GC
--cpus=2 --memory=1792m -> UseG1GC
--cpus=2 --memory=1800m -> UseG1GC
--cpus=2 --memory=2g -> UseG1GC
+126
View File
@@ -0,0 +1,126 @@
#!/usr/bin/env bash
# Regenerates every file in output/. Needs JDK 25, 27 and a 28 early-access build on disk.
# JDK25=/path JDK27=/path JDK28=/path ./run.sh [step ...] (no step = all)
# Timings are machine dependent: read them as shape, not as results. Everything else is stable.
set -u
unset JAVA_TOOL_OPTIONS _JAVA_OPTIONS # sandbox proxy banner is noise in a transcript
HERE="$(cd "$(dirname "$0")" && pwd)"
JDK25="${JDK25:-/tmp/tools/j25}"; JDK27="${JDK27:-/tmp/tools/j27}"; JDK28="${JDK28:-/tmp/tools/j28}"
OUT="$HERE/output"; CLS="$HERE/target/classes"; mkdir -p "$OUT" "$CLS"
"$JDK25/bin/javac" -Xlint:deprecation -d "$CLS" "$HERE"/src/*.java 2>&1 | sed "s#$HERE/##" > "$OUT/00-javac-warnings.txt"
J() { local v=$1; shift; "$(eval echo \$JDK$v)/bin/java" "$@"; } # J 25 -cp ... Main
ver() { "$(eval echo \$JDK$1)/bin/java" -version 2>&1 | sed -n '2p'; }
say() { printf '\n## %s\n' "$*"; }
step_01() { { echo "# Which collector does the JVM pick when you name none? (emulating a 1, 2 and 4 CPU machine with -XX:ActiveProcessorCount)"
for v in 25 27 28; do say "JDK $v: $(ver $v)"
for p in 1 2 4; do echo "\$ java -XX:ActiveProcessorCount=$p Which"; J $v -XX:ActiveProcessorCount=$p -cp "$CLS" Which 2>&1 | grep -v 'processors seen\|max heap'; done
done; } > "$OUT/01-default-collector.txt"; }
step_02() { { echo "# The memory half of the rule cannot be emulated with -XX:MaxRAM on 27 or later"
for v in 25 27 28; do say "JDK $v"; echo "\$ java -XX:MaxRAM=1g -XX:+PrintFlagsFinal -version | grep MaxRAM"; J $v -XX:MaxRAM=1g -XX:+PrintFlagsFinal -version 2>&1 | grep -E ' MaxRAM |Ignoring option|Unrecognized VM|Could not create' | sed 's/ */ /g' | head -3; done; } > "$OUT/02-maxram.txt"; }
step_03() { { echo "# Shenandoah: which mode is the default, and is the generational mode still experimental? (mode read from -Xlog:gc+init)"
for v in 25 27 28; do say "JDK $v, no mode flag"; echo "\$ java -XX:+UseShenandoahGC -Xlog:gc+init -version | grep Mode"; J $v -XX:+UseShenandoahGC -Xlog:gc+init -version 2>&1 | grep -E 'Mode:|warning'; done
say "JDK 25: generational mode without -XX:+UnlockExperimentalVMOptions"; echo "\$ java -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational -Xlog:gc+init -version"; J 25 -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational -Xlog:gc+init -version 2>&1 | grep -E 'Mode:|rror|warning|Unlock'
say "JDK 24-style spelling on 25 (the unlock flag is now unnecessary but harmless)"; J 25 -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational -Xlog:gc+init -version 2>&1 | grep -E 'Mode:|rror|warning'
say "JDK 28 early access: asking for the old mode explicitly"; echo "\$ java -XX:+UseShenandoahGC -XX:ShenandoahGCMode=satb -Xlog:gc+init -version"; J 28 -XX:+UseShenandoahGC -XX:ShenandoahGCMode=satb -Xlog:gc+init -version 2>&1 | grep -E 'Mode:|rror|warning|eprecat'; echo "(no deprecation warning is printed by this early-access build)"
} > "$OUT/03-shenandoah-modes.txt"; }
step_04() { { echo "# ZGC: the non-generational mode is gone, and what 28 adds"
say "JDK 25: ask for the old mode"; echo "\$ java -XX:+UseZGC -XX:-ZGenerational -version"; J 25 -XX:+UseZGC -XX:-ZGenerational -version 2>&1 | head -2
say "JDK 25: plain ZGC"; J 25 -XX:+UseZGC -cp "$CLS" Which 2>&1 | grep -v 'processors\|max heap'
say "ZGC flags present on 28 early access but not on 27 (diff of -XX:+PrintFlagsFinal -XX:+UseZGC, names only)"
J 27 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -XX:+PrintFlagsFinal -version 2>/dev/null | awk '$2 ~ /^Z/ {print $2}' | sort > /tmp/z27.txt
J 28 -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -XX:+PrintFlagsFinal -version 2>/dev/null | awk '$2 ~ /^Z/ {print $2}' | sort > /tmp/z28.txt
echo "only on 28:"; comm -13 /tmp/z27.txt /tmp/z28.txt; echo "only on 27:"; comm -23 /tmp/z27.txt /tmp/z28.txt
say "its default on 28"; J 28 -XX:+UseZGC -XX:+PrintFlagsFinal -version 2>/dev/null | grep -E 'ZAdaptiveHeapSizing|SoftMaxHeapSize' | sed 's/ */ /g'
} > "$OUT/04-zgc.txt"; }
step_05() { { echo "# Reachability observed with weak references (JDK 25)"; J 25 -cp "$CLS" Reach; } > "$OUT/05-reachability.txt"; }
step_06() { { echo "# Reading a G1 log: the first lines of -Xlog:gc for the Churn workload (JDK 25, -Xms512m -Xmx512m, 150 MB live)"
echo "\$ java -Xms512m -Xmx512m -Xlog:gc Churn 150 3"; J 25 -Xms512m -Xmx512m -Xlog:gc -cp "$CLS" Churn 150 3 | grep -E '^\[' | head -24
echo; echo "# the same run, counting what G1 did"; J 25 -Xms512m -Xmx512m -Xlog:gc -cp "$CLS" Churn 150 3 | grep ' Pause ' | sed -E 's/^\[[^]]*\]\[[^]]*\]\[[^]]*\] GC\([0-9]+\) //; s/[0-9]+M->[0-9]+M\([0-9]+M\)//; s/[0-9.]+ms//' | sed 's/ *$//' | sort | uniq -c | sort -rn
} > "$OUT/06-g1-log.txt"; }
step_07() { { echo "# Same workload, five collectors, JDK 25, -Xms1g -Xmx1g, 450 MB live set of small linked objects that are constantly replaced, 8 s measured after a 1.5 s warm-up, three runs each"
echo "# (2 vCPU sandbox: shape, not result. 'requests' counts completed loop iterations, a rough throughput signal)"
for gc in "-XX:+UseSerialGC" "-XX:+UseParallelGC" "-XX:+UseG1GC" "-XX:+UseZGC" "-XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational"; do
say "$gc"; for r in 1 2 3; do J 25 -Xms1g -Xmx1g $gc -cp "$CLS" Churn 450 8 graph | grep -E 'requests|ticker'| sed 's/^/ run '$r' /' ; done; done; } > "$OUT/07-five-collectors.txt"; }
step_08() { { echo "# The defaults on each release, same workload and heap as 07, two runs each"
for v in 25 27 28; do say "JDK $v: $(ver $v), no collector flag"; for r in 1 2; do J $v -Xms1g -Xmx1g -cp "$CLS" Churn 450 8 graph | grep -E 'collector|requests|ticker' | sed 's/^/ run '$r' /'; done; done
say "JDK 28 early access, -XX:+UseShenandoahGC with no mode flag"; for r in 1 2; do J 28 -Xms1g -Xmx1g -XX:+UseShenandoahGC -cp "$CLS" Churn 450 8 graph | grep -E 'collector|requests|ticker' | sed 's/^/ run '$r' /'; done; } > "$OUT/08-defaults-by-release.txt"; }
step_09() { { echo "# jcmd GC.heap_info on a live process, per collector (JDK 25, 512 MB heap, 150 MB live)"
for gc in "-XX:+UseParallelGC" "-XX:+UseG1GC" "-XX:+UseZGC" "-XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational"; do
say "$gc"; rm -f /tmp/churn.out; ( exec "$JDK25/bin/java" -Xms512m -Xmx512m $gc -cp "$CLS" Churn 150 3 flat wait > /tmp/churn.out 2>&1 ) &
pid=$!; for i in $(seq 1 60); do grep -q READY /tmp/churn.out 2>/dev/null && break; sleep 0.5; done
"$JDK25/bin/jcmd" $pid GC.heap_info 2>&1 | grep -v "^$pid" ; wait $pid; done; } > "$OUT/09-heap-info.txt"; }
step_10() { { echo "# Running out of memory: the message depends on how the collector fails (JDK 25, -Xmx64m)"
say "G1, 1 MB arrays"; J 25 -Xmx64m -cp "$CLS" Leak big 2>&1 | tail -2
say "G1, tiny objects"; J 25 -Xmx64m -cp "$CLS" Leak small 2>&1 | tail -3
say "Parallel, tiny objects"; J 25 -Xmx64m -XX:+UseParallelGC -cp "$CLS" Leak small 2>&1 | tail -3
} > "$OUT/10-out-of-memory.txt"; }
step_11() { { echo "# What one System.gc() costs (JDK 25, G1, 150 MB live)"
say "default"; J 25 -Xlog:gc -cp "$CLS" SysGc | grep -E 'Pause Full|System.gc|live chunks'
say "-XX:+DisableExplicitGC"; J 25 -XX:+DisableExplicitGC -Xlog:gc -cp "$CLS" SysGc | grep -E 'System.gc|live chunks'
say "-XX:+ExplicitGCInvokesConcurrent"; J 25 -XX:+ExplicitGCInvokesConcurrent -Xlog:gc -cp "$CLS" SysGc | grep -E '\(System.gc\(\)\)|System.gc\(\) #|live chunks'
say "ZGC"; J 25 -XX:+UseZGC -cp "$CLS" SysGc
} > "$OUT/11-system-gc.txt"; }
step_12() { { echo "# finalize() today"
say "what javac says (JDK 25, -Xlint:deprecation)"; cat "$OUT/00-javac-warnings.txt"
for v in 25 27 28; do say "JDK $v, default"; J $v -cp "$CLS" 'Finalize' 2>&1 | head -3; say "JDK $v, --finalization=disabled"; J $v --finalization=disabled -cp "$CLS" Finalize 2>&1 | head -3; done; } > "$OUT/12-finalize.txt"; }
step_14() { { echo "# The baseline: a FLAT live set (one list of 1 KB byte arrays, no pointers inside), JDK 25, -Xms512m -Xmx512m, 150 MB live, 8 s, one run each"
for gc in "-XX:+UseSerialGC" "-XX:+UseParallelGC" "-XX:+UseG1GC" "-XX:+UseZGC" "-XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational"; do
say "$gc"; J 25 -Xms512m -Xmx512m $gc -cp "$CLS" Churn 150 8 flat | grep -E 'requests|ticker' | sed 's/^/ /'; done; } > "$OUT/14-flat-live-set-baseline.txt"; }
step_15() { { echo "# Why is G1's worst case so uneven? Pause types and the three longest pauses in three G1 runs of the graph workload (JDK 25, -Xms1g -Xmx1g, 450 MB live)"
for r in 1 2 3; do say "run $r"; J 25 -Xms1g -Xmx1g -Xlog:gc -cp "$CLS" Churn 450 8 graph > /tmp/g1.log
echo "pause counts by type:"; grep -o 'Pause [A-Za-z]* ([^)]*)' /tmp/g1.log | sort | uniq -c | sort -rn
echo "three longest pauses:"; grep ' Pause ' /tmp/g1.log | awk '{print $NF "\t" $0}' | sort -rn | head -3 | cut -f2- | sed -E 's/^\[[^]]*\]\[[^]]*\]\[[^]]*\] //'; done; } > "$OUT/15-g1-pause-types.txt"; }
step_16() { { echo "# Heap is not the whole process: Native Memory Tracking on a live JVM (JDK 25, G1, -Xmx512m, 150 MB live)"
rm -f /tmp/churn.out; ( exec "$JDK25/bin/java" -Xms512m -Xmx512m -XX:NativeMemoryTracking=summary -cp "$CLS" Churn 150 3 flat wait > /tmp/churn.out 2>&1 ) &
pid=$!; for i in $(seq 1 60); do grep -q READY /tmp/churn.out 2>/dev/null && break; sleep 0.5; done
echo "\$ jcmd <pid> VM.native_memory summary (category totals only)"; "$JDK25/bin/jcmd" $pid VM.native_memory summary 2>&1 | grep -E '^-|^Total' | sed -E 's/^- +//' | cut -c1-110
echo; echo "resident set size of the process, from /proc: $(awk '/VmRSS/{print $2/1024 " MB"}' /proc/$pid/status)"; wait $pid; } > "$OUT/16-native-memory.txt"; }
step_17() { { echo "# A few defaults worth knowing (JDK 25)"
echo "\$ java -Xmx1g -XX:+UseG1GC -XX:+PrintFlagsFinal -version | grep ..."; J 25 -Xmx1g -XX:+UseG1GC -XX:+PrintFlagsFinal -version 2>/dev/null | grep -E ' MaxGCPauseMillis | G1HeapRegionSize | InitiatingHeapOccupancyPercent | ParallelGCThreads | ConcGCThreads ' | sed 's/ */ /g'
echo "\$ java -Xmx1g -XX:+UseSerialGC -XX:+PrintFlagsFinal -version | grep -E 'NewRatio|SurvivorRatio|MaxTenuringThreshold'"; J 25 -Xmx1g -XX:+UseSerialGC -XX:+PrintFlagsFinal -version 2>/dev/null | grep -E ' NewRatio | SurvivorRatio | MaxTenuringThreshold ' | sed 's/ */ /g'; } > "$OUT/17-flag-defaults.txt"; }
step_18() { { echo "# Shenandoah by JDK and mode, same graph workload as 07 (-Xms1g -Xmx1g, 450 MB live), one run each"
for v in 25 27 28; do for m in satb generational; do say "JDK $v, -XX:ShenandoahGCMode=$m"; J $v -Xms1g -Xmx1g -XX:+UseShenandoahGC -XX:ShenandoahGCMode=$m -cp "$CLS" Churn 450 8 graph | grep -E 'requests|ticker' | sed 's/^/ /'; done; done; } > "$OUT/18-shenandoah-by-jdk.txt"; }
step_19() { { echo "# Headroom: the graph workload again with a 2 GB heap instead of 1 GB (450 MB live is now 22 percent of the heap), JDK 25, one run each"
for gc in "-XX:+UseSerialGC" "-XX:+UseParallelGC" "-XX:+UseG1GC" "-XX:+UseZGC" "-XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational"; do
say "$gc"; J 25 -Xms2g -Xmx2g $gc -cp "$CLS" Churn 450 8 graph | grep -E 'requests|ticker' | sed 's/^/ /'; done; } > "$OUT/19-headroom-2g.txt"; }
step_20() { { echo "# What Shenandoah did on each JDK for the graph workload (-Xms1g -Xmx1g, 450 MB live, generational mode, one run each): pause types and the three longest pauses"
for v in 25 27 28; do say "JDK $v"; J $v -Xms1g -Xmx1g -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational -Xlog:gc -cp "$CLS" Churn 450 8 graph > /tmp/sh.log
echo "pause counts by type:"; grep ' Pause ' /tmp/sh.log | grep -oE 'Pause (Init Mark|Final Mark|Degenerated GC|Full|Init Update Refs|Final Update Refs)' | sort | uniq -c | sort -rn
echo "three longest pauses:"; grep ' Pause ' /tmp/sh.log | awk '{print $NF "\t" $0}' | sort -rn | head -3 | cut -f2- | sed -E 's/^\[[^]]*\]\[[^]]*\]\[[^]]*\] //'; done; } > "$OUT/20-shenandoah-pause-types.txt"; }
step_21() { { echo "# A pause-free log is not a stall-free application: Shenandoah allocation stalls (generational mode, graph workload, -Xms1g -Xmx1g, 450 MB live, one run each)"
for v in 25 27 28; do say "JDK $v"; J $v -Xms1g -Xmx1g -XX:+UseShenandoahGC -XX:ShenandoahGCMode=generational -Xlog:gc -cp "$CLS" Churn 450 8 graph > /tmp/sh.log
echo "allocation stalls logged : $(grep -c 'Allocation Stall' /tmp/sh.log)"
echo "longest allocation stalls :"; grep 'Allocation Stall' /tmp/sh.log | awk '{print $NF "\t" $0}' | sort -rn | head -2 | cut -f2- | sed -E 's/^\[[^]]*\]\[[^]]*\]\[[^]]*\] //; s/^/ /'
echo "longest stop-the-world pause: $(grep ' Pause ' /tmp/sh.log | awk '{print $NF}' | sort -rn | head -1)"; grep ticker /tmp/sh.log; done; } > "$OUT/21-shenandoah-allocation-stalls.txt"; }
step_22() { { echo "# The default collector inside real container limits (docker run --cpus --memory; needs Docker and the eclipse-temurin:25-jre and :27-jre images)"
for img in 25 27; do say "eclipse-temurin:$img-jre"
for lim in "--cpus=1 --memory=4g" "--cpus=2 --memory=1g" "--cpus=2 --memory=1500m" "--cpus=2 --memory=1791m" "--cpus=2 --memory=1792m" "--cpus=2 --memory=1800m" "--cpus=2 --memory=2g"; do
printf '%-28s -> ' "$lim"; docker run --rm $lim eclipse-temurin:$img-jre java -XX:+PrintFlagsFinal -version 2>/dev/null | grep -E ' Use(G1|Serial)GC ' | awk '$4=="true"{printf "%s", $2}'; echo
done; done; } > "$OUT/22-container-default-collector.txt"; }
step_13() { { echo "# the other two snippets in the article, run"; "$JDK25/bin/java" -cp "$CLS" StackAndHeap; "$JDK25/bin/java" -cp "$CLS" TryWithResources; } > "$OUT/13-small-demos.txt"; }
if [ $# -eq 0 ]; then set -- 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22; fi
for s in "$@"; do echo "step $s"; "step_$s"; done
echo "versions: $(ver 25) | $(ver 27) | $(ver 28)" > "$OUT/00-versions.txt"
+93
View File
@@ -0,0 +1,93 @@
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
/**
* A synthetic service: a live set that stays in memory plus a steady stream of short-lived garbage.
* A "ticker" thread asks to sleep 1 ms in a loop and records how late it wakes up; a stop-the-world pause shows up as lateness.
* Usage: Churn <liveMB> <seconds> [flat|graph] [wait]
* flat (default): the live set is one big list of 1 KB byte arrays: cheap for a collector to copy, no pointers inside.
* graph: the live set is a HashMap of small entries that are replaced all the time: many objects, many old-to-young pointers.
* wait: parks the JVM at the end so jcmd can look at it.
*/
public class Churn {
static volatile boolean stop;
static long maxStallNs, over10ms, over50ms;
static final List<byte[]> live = new ArrayList<>();
static final java.util.HashMap<Integer, byte[]> graph = new java.util.HashMap<>(); // "graph" mode: many small linked objects, mutated often
static boolean graphMode;
static int graphKeys;
static final byte[][] ring = new byte[16384][]; // recent "requests" stay reachable briefly, so the JIT cannot optimise the allocations away
static int slot;
static long allocatedBytes, ops;
/** Runs the request loop for the given time. */
static void serve(long nanos) {
var rnd = ThreadLocalRandom.current();
long end = System.nanoTime() + nanos;
allocatedBytes = 0;
ops = 0;
while (System.nanoTime() < end) {
// short-lived garbage: a few small arrays per "request"
for (int k = 0; k < 8; k++) {
byte[] b = new byte[256 + rnd.nextInt(768)];
ring[slot] = b;
slot = (slot + 1) & (ring.length - 1);
allocatedBytes += b.length;
}
// some requests replace one piece of the live set, so some objects live a while and then die
if (graphMode) { if ((ops & 1) == 0) graph.put(rnd.nextInt(graphKeys), new byte[48]); }
else if (ops % 200 == 0) live.set(rnd.nextInt(live.size()), new byte[1024]);
ops++;
}
}
public static void main(String[] args) throws Exception {
int liveMb = Integer.parseInt(args[0]);
int seconds = Integer.parseInt(args[1]);
graphMode = args.length > 2 && args[2].equals("graph");
boolean waitForJcmd = args.length > 3 && args[3].equals("wait");
if (graphMode) {
graphKeys = liveMb * 1024 * 1024 / 120; // roughly 120 bytes per entry: HashMap node + Integer key + 48-byte array
for (int i = 0; i < graphKeys; i++) graph.put(i, new byte[48]);
} else {
for (int i = 0; i < liveMb * 1024; i++) live.add(new byte[1024]); // the live set: 1 KB chunks reachable for the whole run
}
// Warm-up: let the JIT compile the loop and let the collector move the freshly built live set out of the young
// generation. Only after that does the ticker start, so a one-off startup pause is not mistaken for steady state.
serve(1_500_000_000L);
Thread ticker = new Thread(() -> {
long prev = System.nanoTime();
while (!stop) {
try { Thread.sleep(1); } catch (InterruptedException e) { return; }
long now = System.nanoTime();
long late = now - prev - 1_000_000L;
if (late > maxStallNs) maxStallNs = late;
if (late > 10_000_000L) over10ms++;
if (late > 50_000_000L) over50ms++;
prev = now;
}
});
ticker.setDaemon(true);
ticker.start();
serve(seconds * 1_000_000_000L);
if (waitForJcmd) { System.out.println("READY"); System.out.flush(); Thread.sleep(4000); }
stop = true;
Thread.sleep(5);
StringBuilder names = new StringBuilder();
for (GarbageCollectorMXBean b : ManagementFactory.getGarbageCollectorMXBeans()) names.append(b.getName()).append(" ");
System.out.printf("collector beans : %s%n", names.toString().trim());
System.out.printf("live set : %d MB (%s), heap max %d MB%n", liveMb, graphMode ? "graph" : "flat", Runtime.getRuntime().maxMemory() >> 20);
System.out.printf("requests : %d (%.0f MB allocated)%n", ops, allocatedBytes / 1048576.0);
System.out.printf("ticker max stall: %.1f ms; wakeups >10 ms: %d; >50 ms: %d%n", maxStallNs / 1e6, over10ms, over50ms);
}
}
+24
View File
@@ -0,0 +1,24 @@
/** What finalize() does today. Compile with -Xlint:deprecation to see the compiler's view of it. */
public class Finalize {
static class Holder {
@SuppressWarnings({"removal", "deprecation"})
@Override
protected void finalize() {
System.out.println("finalize() ran");
}
}
static class Holder2 {
@Override
protected void finalize() { // deliberately not suppressed: javac prints a removal warning
System.out.println("finalize() ran");
}
}
public static void main(String[] args) throws Exception {
new Holder();
System.gc();
Thread.sleep(500);
System.out.println("main done");
}
}
+22
View File
@@ -0,0 +1,22 @@
import java.util.ArrayList;
import java.util.List;
/** A classic leak: a static collection that only ever grows. Argument "big" keeps 1 MB arrays, "small" keeps tiny objects. */
public class Leak {
static final List<Object> CACHE = new ArrayList<>(); // a GC root: nothing ever removes from it
public static void main(String[] args) {
boolean big = args.length > 0 && args[0].equals("big");
long n = 0;
try {
while (true) {
CACHE.add(big ? new byte[1 << 20] : new int[8]);
n++;
}
} catch (OutOfMemoryError e) {
int kept = CACHE.size();
CACHE.clear(); // give the JVM room to print
System.out.println("kept " + kept + (big ? " arrays of 1 MB" : " tiny objects") + " then: java.lang.OutOfMemoryError: " + e.getMessage());
}
}
}
+44
View File
@@ -0,0 +1,44 @@
import java.lang.ref.WeakReference;
/**
* Reachability, observed. A WeakReference does not keep its target alive, so after a full collection
* "get() == null" means "the collector found it unreachable from the roots".
*/
public class Reach {
static class MyObject {
final String name;
MyObject child;
MyObject(String name) { this.name = name; }
}
public static MyObject staticObj; // a GC root: a static field
static WeakReference<MyObject> weak(MyObject o) { return new WeakReference<>(o); }
public static void main(String[] args) throws Exception {
MyObject localObj = new MyObject("A"); // a GC root: a local variable
staticObj = new MyObject("B");
localObj.child = new MyObject("C"); // 'C' is reachable from 'A'
MyObject unreachable = new MyObject("D"); // referenced by nothing once we drop the variable
// An "island": two objects that point at each other, with no path from any root.
MyObject x = new MyObject("X");
MyObject y = new MyObject("Y");
x.child = y;
y.child = x;
var refs = new WeakReference[] { weak(localObj), weak(staticObj), weak(localObj.child), weak(unreachable), weak(x), weak(y) };
String[] names = { "A (local variable)", "B (static field)", "C (child of A)", "D (no references)", "X (cycle with Y, no root)", "Y (cycle with X, no root)" };
unreachable = null;
x = null;
y = null;
System.gc(); // for the demonstration only; see "Forget System.gc() exists" in the article
for (int i = 0; i < refs.length; i++) {
System.out.printf("%-28s %s%n", names[i], refs[i].get() != null ? "alive" : "collected");
}
System.out.println("A and C are still used here: " + localObj.name + " -> " + localObj.child.name);
}
}
+13
View File
@@ -0,0 +1,13 @@
/** The stack-versus-heap snippet from the article: what lives where, and when it goes away. */
public class StackAndHeap {
public void myMethod() {
int localValue = 10; // stored directly on the stack for this method's frame
String message = "Hello"; // the *reference* 'message' is on the stack...
// ...but the String object itself is on the heap (a literal lives in the string pool, also on the heap).
System.out.println(localValue + " " + message);
} // the frame is popped here; 'localValue' and 'message' (the reference) are gone
public static void main(String[] args) {
new StackAndHeap().myMethod();
}
}
+16
View File
@@ -0,0 +1,16 @@
import java.util.ArrayList;
import java.util.List;
/** What one System.gc() call costs when the heap holds a real live set. */
public class SysGc {
public static void main(String[] args) {
List<byte[]> live = new ArrayList<>();
for (int i = 0; i < 150 * 1024; i++) live.add(new byte[1024]); // 150 MB that must be kept
for (int i = 1; i <= 3; i++) {
long t = System.nanoTime();
System.gc();
System.out.printf("System.gc() #%d took %.1f ms%n", i, (System.nanoTime() - t) / 1e6);
}
System.out.println("live chunks still referenced: " + live.size());
}
}
+19
View File
@@ -0,0 +1,19 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
/** The deterministic way to release a file handle: try-with-resources, not finalize(). */
public class TryWithResources {
public static void main(String[] args) throws IOException {
Path file = Files.createTempFile("gc-basics", ".txt");
Files.writeString(file, "hello");
try (FileInputStream fis = new FileInputStream(file.toFile())) {
System.out.println("read byte count: " + fis.readAllBytes().length);
} catch (IOException e) {
System.out.println("handle exception here: " + e);
} // fis.close() has already run here, whether or not an exception was thrown
System.out.println("stream closed deterministically at the end of the try block");
Files.delete(file);
}
}
+12
View File
@@ -0,0 +1,12 @@
import java.lang.management.ManagementFactory;
import java.util.stream.Collectors;
/** Prints which collector this JVM actually picked, taken from the JVM itself rather than from a flag listing. */
public class Which {
public static void main(String[] args) {
var beans = ManagementFactory.getGarbageCollectorMXBeans();
System.out.println("processors seen by JVM : " + Runtime.getRuntime().availableProcessors());
System.out.println("max heap : " + Runtime.getRuntime().maxMemory() / (1024 * 1024) + " MB");
System.out.println("collector MXBeans : " + beans.stream().map(b -> b.getName()).collect(Collectors.joining(" | ")));
}
}