Fundamentals 8 min read

How ZGC Achieves Sub‑10 ms Pauses: A Deep Dive into Java’s Low‑Latency GC

ZGC is a scalable, low‑latency Java garbage collector designed to keep pause times under 10 ms regardless of heap size, supporting up to 4 TB, and leveraging concurrent, region‑based, compacting, NUMA‑aware techniques, colored pointers, and load barriers, with detailed compilation and tuning guidance.

Programmer DD
Programmer DD
Programmer DD
How ZGC Achieves Sub‑10 ms Pauses: A Deep Dive into Java’s Low‑Latency GC

Overview of ZGC

Z Garbage Collector (ZGC) is a scalable, low‑latency garbage collector designed to keep pause times below 10 ms, independent of heap size, and to support heaps up to 4 TB.

Pause time ≤ 10 ms

Pause time does not grow with heap size

Supports heaps from hundreds of MB to several TB (max 4 TB)

In the SPECjbb 2015 benchmark with a 128 GB heap, the maximum pause was only 1.68 ms, far below the 10 ms target and noticeably better than G1.

ZGC performance
ZGC performance

Compared with G1, ZGC achieves superior pause times on large heaps thanks to several key features.

Key Design Features

Concurrent

Only brief STW phases exist; most work, including concurrent marking and relocation, runs in parallel with application threads.

Region‑Based

ZGC abandons the traditional young/old generation model and manages memory in fixed‑size pages, allocating and reclaiming objects per page.

Compacting

Each GC cycle compacts pages, eliminating fragmentation problems seen in CMS.

NUMA‑Aware

ZGC detects the NUMA node of the current thread and prefers allocating objects in memory local to that node, yielding up to 40 % performance gains in SPECjbb 2005.

Colored Pointers

Instead of marking object headers, ZGC encodes mark bits in the high bits of object pointers.

Colored pointers
Colored pointers

The low 42 bits hold the address; bits 42‑45 are used for marking.

Load Barriers

During concurrent marking and relocation, read barriers ensure that a thread sees a consistent view of objects whose references may be moving.

Evolution and Platform Support

JDK 11

Initial ZGC release

No class unloading support

JDK 12

Further reduced pause times

Added class unloading support

Platform

ZGC is currently available only on Linux/x64 64‑bit systems.

Mac compilation error
Mac compilation error

Building ZGC from Source

$ hg clone https://hg.openjdk.java.net/jdk/jdk
$ cd jdk
$ sh configure
$ make images

For JDK 11.0.0‑11.0.2 builds, add the option --jvm-features=zgc. From JDK 11.0.3 onward this flag is unnecessary.

./build/linux-x86_64-normal-server-release/images/jdk

Run ./bin/java -version to verify the build.

Running with ZGC

Enable ZGC with the following JVM flags:

-XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xmx10g -Xlog:gc

Heap Size

Set the maximum heap with -Xmx. Larger heaps improve throughput but must be balanced against memory usage.

Concurrent GC Threads

Configure with -XX:ConcGCThreads=4. The default is 12.5 % of CPU cores; adjust based on latency requirements.

Parallel GC Threads

Configure with -XX:ParallelGCThreads=20. During STW phases, more threads shorten pause duration.

Conclusion

ZGC differs fundamentally from previous collectors, offering sub‑10 ms pauses even on multi‑terabyte heaps, making it attractive for latency‑sensitive Java applications.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaGarbage CollectionzgcJDKLow latencyNUMAconcurrent
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.