On this page
Garbage Collection (G1 & ZGC)
Prev: JVM ClassLoading, Bytecode & JIT | Up: JVM & OpenJDK | Next: Diagnostics, JFR & jcmd
Rel: Memory Management Models, OpenJDK Distributions & Toolchain, finalize (Legacy)
Ref: OpenJDK HotSpot GC Tuning Guide , OpenJDK Site
Tags: #public #java #jvm #openjdk #gc #performance #tier/standard
Overview
OpenJDK's HotSpot JVM provides automatic tracing garbage collection based on the weak generational hypothesis: most objects die young (short-lived request objects, iterators, temporary buffers), while a small fraction survive long term. Modern Java offers pluggable collectors tuned for different latency vs. throughput goals.
Choosing a Collector in Java 21+
| Collector | Flag | Pause Target | Best For |
|---|---|---|---|
| G1 GC (default) | -XX:+UseG1GC |
~10โ200 ms (-XX:MaxGCPauseMillis) |
General-purpose servers, balanced throughput and latency |
| Generational ZGC | -XX:+UseZGC -XX:+ZGenerational |
Sub-millisecond (< 1 ms) | Latency-sensitive services, large heaps (multi-GB to TB) |
| Parallel GC | -XX:+UseParallelGC |
Higher STW pauses | Batch processing, maximum raw throughput |
| Serial GC | -XX:+UseSerialGC |
Single-threaded STW | Tiny containers (< 100โ256 MB heap), short-lived CLIs |
| Shenandoah (in some OpenJDK builds) | -XX:+UseShenandoahGC |
Low-millisecond | Low-pause concurrent compaction where available |
How G1 GC Works (Default)
- Divides the heap into equal-sized regions (1โ32 MB) dynamically assigned as Eden, Survivor, Old, or Humongous.
- Young collections evacuate live objects from Eden/Survivor regions in parallel during short stop-the-world (STW) pauses.
- Concurrent marking traces live objects in Old regions while application threads run, then Mixed collections evacuate the regions with the most reclaimable garbage ("Garbage-First") to meet
-XX:MaxGCPauseMillis.
How Generational ZGC Works
- Uses colored pointers and load/store barriers to mark, relocate, and compact objects concurrently while application threads keep running.
- Pause times stay under 1 ms regardless of heap size, at a modest CPU/memory overhead compared to G1.
- Separating young and old generations (introduced in Java 21) dramatically cuts allocation stalls and reduces total CPU cost compared to non-generational ZGC.
Practical Heap & GC Flags
# Set explicit min/max heap (or use -XX:MaxRAMPercentage=75.0 in containers)
java -Xms1g -Xmx1g -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -jar app.jar
# Enable Generational ZGC for ultra-low tail latency
java -Xms4g -Xmx4g -XX:+UseZGC -XX:+ZGenerational -jar app.jar
# Unified GC logging (Java 9+ syntax)
java -Xlog:gc*:file=gc.log:time,uptime,level,tags -jar app.jar
Memory Leak Patterns in Garbage-Collected Java
GC cannot free objects that remain strongly reachable from GC roots (thread stacks, static fields, active classloaders):
- Unbounded static caches or
Maps without eviction. - Registered event listeners or callbacks that are never unregistered.
ThreadLocalvalues not cleared viaremove()in pooled threads.- Unclosed
AutoCloseableresources holding off-heap/native memory.
Linked from
Related notes
- tuts/java/jvm_and_openjdk/jvm_classloading_bytecode_and_jit Linked here; links to this page; shared tags: #java, #jvm, #openjdk, #tier
- tuts/java/jvm_and_openjdk Linked here; links to this page; shared tags: #java, #jvm, #openjdk
- tuts/java/jvm_and_openjdk/diagnostics_jfr_and_jcmd Linked here; links to this page; shared tags: #java, #openjdk, #tier
- tuts/java/jvm_and_openjdk/openjdk_distributions_and_toolchain Linked here; links to this page; shared tags: #java, #openjdk, #tier
- tuts/programming/memory_and_execution/memory_management_models Linked here; links to this page; shared tags: #gc
- tuts/java/builtins/finalize Linked here; shared tags: #java, #tier
- tuts/interop/java_and_c_ffm_and_jni shared tags: #java, #openjdk, #tier
- tuts/java shared tags: #java, #jvm, #openjdk
- tuts/java/builtins/AutoCloseable shared tags: #java, #tier
- tuts/java/builtins/enum shared tags: #java, #tier