Files
javademos/gc-basics/README.md
T

60 lines
3.0 KiB
Markdown

# 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.