Garbage Collection: G1, ZGC and Tuning Basics

How the JVM reclaims memory: reachability and GC roots, the generational hypothesis, mark-sweep-compact, Serial, Parallel, G1, generational ZGC and Shenandoah compared, pause vs throughput trade-offs, heap sizing in containers, reading GC logs, memory leaks, and recent changes through Java 27.

On this page

Simple definition#

Garbage collection (GC) automatically frees heap memory used by objects that the program can no longer reach. The JVM ships several collectors that trade pause time, throughput and memory overhead differently.

The problem#

Manual memory management causes leaks and use-after-free bugs. Automatic GC removes those but costs CPU and sometimes pauses the application. The right collector and heap size can turn 2-second pauses into sub-millisecond ones, or double throughput.

Reachability#

An object is live if it can be reached from a GC root:

  • local variables and parameters on thread stacks
  • static fields of loaded classes
  • JNI references
  • active monitors.

Everything else is garbage, including cycles. Java does not use reference counting.

Mark from roots; unreachable objects are garbage
flowchart LR
    R1["GC root: stack variable"] --> A["Order"] --> B["List of lines"] --> C["OrderLine"]
    R2["GC root: static cache"] --> D["Customer"]
    X["Old Order (unreachable)"] --> Y["Its lines"]

Generational collection#

The weak generational hypothesis says most objects die young: request DTOs, iterators, temporary strings.

RegionContentsCollected by
Young generation (Eden + 2 survivor spaces)New objectsFrequent, fast minor GCs: copy live objects, discard the rest
Old generationObjects that survived several minor GCs (tenured)Less frequent major / mixed collections

Basic phases: mark live objects, sweep dead ones, compact to avoid fragmentation. Copying collectors evacuate live objects to a new area, which compacts as a side effect.

The collectors#

CollectorPausesThroughputHeap sizesUse for
SerialStop-the-world, single threadOK on small heaps< ~100 MBTiny containers, CLI tools
ParallelStop-the-world, multi-threadedHighestAnyBatch jobs where pauses do not matter
G1 (default)Mostly concurrent marking; region-based evacuation with a pause-time goal (default 200 ms)GoodMedium to largeGeneral-purpose server default
ZGC (generational since JDK 21, the only mode since JDK 23)Sub-millisecond pauses, independent of heap sizeSlightly lowerUp to multi-terabyteLatency-sensitive services, large heaps
Shenandoah (generational mode in recent JDKs)Very low pauses, concurrent compactionSlightly lowerMedium to largeLow-latency alternative (OpenJDK builds that include it)
  • G1 splits the heap into equal regions. It collects the regions with the most garbage first (“garbage first”) and adapts young-generation size to meet -XX:MaxGCPauseMillis.
  • ZGC uses coloured pointers and load barriers to relocate objects concurrently while the application runs.

Choosing and sizing#

Picking a collector
flowchart TB
    Q1{"Strict latency target<br/>(p99.9 under 10 ms) or heap over 16 GB?"} -->|"yes"| ZGC["-XX:+UseZGC"]
    Q1 -->|"no"| Q2{"Batch / throughput only?"}
    Q2 -->|"yes"| PAR["-XX:+UseParallelGC"]
    Q2 -->|"no"| G1["G1 (default): set heap size, maybe MaxGCPauseMillis"]

Containers: the JVM is container-aware. Set heap as a percentage of the container memory limit:

JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=75 -XX:+UseG1GC -Xlog:gc*:file=/logs/gc.log:time,uptime:filecount=5,filesize=10m"

Leave about 25% for metaspace, thread stacks, direct buffers, the code cache and the GC’s own structures, or the container gets OOM-killed even though the heap looks fine.

Tuning order:

  1. Measure with GC logs, JFR and metrics (jvm.gc.pause in Micrometer).
  2. Right-size the heap.
  3. Pick the collector.
  4. Only then touch advanced flags.
  5. Most problems are allocation rate or leaks in the code, not GC flags.

Memory leaks in a garbage-collected language#

A “leak” in Java is an object that is still reachable but no longer needed:

  • static collections and caches without bounds or eviction (use Caffeine with a maximum size)
  • listeners and callbacks never unregistered
  • ThreadLocals not removed in thread pools
  • ClassLoader leaks on redeploys
  • large sessions or unbounded queues.

Diagnose with a heap dump (jcmd <pid> GC.heap_dump) analysed in Eclipse MAT (“dominator tree”, “leak suspects”), and with JFR allocation profiling (jdk.ObjectAllocationSample).

Reference types#

TypeCollected whenUse
StrongNever while reachableNormal references
SoftReferenceUnder memory pressureRarely; prefer a real cache
WeakReferenceAt the next GC once only weakly reachableCanonicalising maps, WeakHashMap
PhantomReference / CleanerAfter finalisation, for cleanupReleasing native resources (finalize() is deprecated for removal)

Common mistakes#

Quick check#

When is an object eligible for GC? When it is no longer reachable from any GC root (stacks, statics, JNI references), even if it is part of a cycle.

Why are heaps generational? Most objects die young, so collecting the young generation often and cheaply reclaims most garbage with little work.

G1 vs ZGC? G1 is the balanced default with a pause-time goal. ZGC does almost all work concurrently, giving sub-millisecond pauses on any heap size, at some throughput cost.

How should heap be sized in containers? As a percentage of the container limit (MaxRAMPercentage, around 75%), leaving headroom for native memory.

What is a Java memory leak? Objects that are still reachable, for example from a static cache or an unremoved listener, but no longer needed.

Summary#

  • GC frees unreachable objects; generations make that cheap for short-lived data.
  • G1 is the default; ZGC is for low latency and huge heaps; Parallel is for throughput.
  • Size heaps relative to container limits; measure with GC logs and JFR before tuning.
  • Leaks are reachable garbage. Find them with heap dumps.

References#