ZGC — A Scalable Low-Latency Garbage Collector

Rishi Jain
2 min readDec 6, 2022

The Z Garbage Collector, also known as ZGC, is a scalable low latency garbage collector designed to meet the following goals:

  • Sub-millisecond max pause times
  • Pause times do not increase with the heap, live-set or root-set size
  • Handle heaps ranging from a 8MB to 16TB in size

ZGC was initially introduced as an experimental feature in JDK 11, and was declared Production Ready in JDK 15.

At a glance, ZGC is:

  • Concurrent
  • Region-based
  • Compacting
  • NUMA-aware
  • Using colored pointers
  • Using load barriers

At its core, ZGC is a concurrent garbage collector, meaning all heavy lifting work is done while Java threads continue to execute. This greatly limits the impact garbage collection will have on your application’s response time.

If you’re trying out ZGC for the first time, start by using the following GC options:

-XX:+UseZGC -Xmx<size> -Xlog:gc

For more detailed logging, use the following options:

-XX:+UseZGC -Xmx<size> -Xlog:gc*

To try this out in one of my applications. I first installed OpenJDK17 in mac

$ brew install openjdk@17

Added in jenv

$ jenv add /opt/homebrew/opt/openjdk@17

Added jvm argument -XX:+UseZGC to enable ZGC

In logs

1 [2022–12–06T17:43:41.062+0530][0.020s] Using The Z Garbage Collector
2 [2022–12–06T17:43:42.194+0530][1.152s] GC(0) Garbage Collection (Warmup) 140M(14%)->46M(4%)
3 [2022–12–06T17:43:42.901+0530][1.859s] GC(1) Garbage Collection (Warmup) 276M(27%)->78M(8%)
4 [2022–12–06T17:43:43.298+0530][2.256s] GC(2) Garbage Collection (Warmup) 320M(31%)->58M(6%)
5 [2022–12–06T17:43:44.701+0530][3.659s] GC(3) Garbage Collection (Proactive) 256M(25%)->76M(7%)
6 [2022–12–06T17:43:46.102+0530][5.060s] GC(4) Garbage Collection (Proactive) 332M(32%)->94M(9%)
7 [2022–12–06T17:43:47.400+0530][6.358s] GC(5) Garbage Collection (Proactive) 216M(21%)->108M(11%)
8 [2022–12–06T17:43:48.611+0530][7.569s] GC(6) Garbage Collection (Proactive) 310M(30%)->118M(12%)
9 [2022–12–06T17:43:50.611+0530][9.569s] GC(7) Garbage Collection (Allocation Rate) 780M(76%)->240M(23%)
10 [2022–12–06T17:43:50.915+0530][9.872s] GC(8) Garbage Collection (Allocation Rate) 830M(81%)->230M(22%)
11 [2022–12–06T17:43:51.017+0530][9.975s] GC(9) Garbage Collection (Allocation Rate) 486M(47%)->330M(32%)

Enable detailed GC logging by adding -Xlog:gc* JVM argument and it printed really nice summary.

Conclusion:

We just tried enabling ZGC in one of the JAVA applications.

--

--

Rishi Jain

Software Support Engineer @StreamSets | Hadoop | DataOps | RHCA | Ex-RedHatter | Ex-Cloudera