Java Z Garbage Collector (ZGC) - Low-Latency and High-Scalability

The Z Garbage Collector (ZGC) is designed for low-latency performance and scalable applications. First introduced in Java 11 and refined over time, ZGC aims to minimize garbage collection pause times, even with large heap sizes. Learn how ZGC works, its features, and improvements in recent Java versions.



Java - Z Garbage Collector (ZGC)

What is ZGC?

ZGC stands for Z Garbage Collector, which was introduced in Java 11 as a low-latency garbage collection mechanism. Initially, ZGC was released as an experimental feature due to concerns within the developer community regarding its size and complexity. ZGC ensures that garbage collection pause times are not affected by heap size, maintaining a maximum pause time of 10 milliseconds, regardless of whether the heap size is 2MB or 2GB.

However, ZGC initially had a limitation in returning unused heap memory to the operating system, unlike other HotSpot VM garbage collectors such as G1 and Shenandoah. With the release of Java 15, ZGC became a standard feature, having been in experimental mode until then. It is recognized as a low-latency and highly scalable garbage collector, capable of efficiently handling massive data applications like machine learning without causing long pauses due to garbage collection. ZGC supports Linux, Windows, and macOS.

Features of Z Garbage Collector

With the introduction of Java 16, several enhancements were made to ZGC, including:

  • Thread-stack processing was moved from safepoints to concurrent phases, significantly improving efficiency.
  • ZGC now returns uncommitted memory to the operating system by default until the maximum heap size is reached.
  • Performance improvements have been made with a reduced memory footprint.
  • Heap size support has increased from 4TB to 16TB.
  • Thread-stack processing is now lazy, cooperative, concurrent, and incremental.
  • All per-thread root processing has been removed from ZGC safepoints.
  • HotSpot subsystems can now lazily process stacks.
  • Concurrent class unloading is supported.
  • Uncommitting of unused memory has been implemented.
  • Support for Class Data Sharing has been added.
  • NUMA (Non-Uniform Memory Access) awareness has been included.
  • Multithreaded heap pre-touching is now available.
  • The maximum heap size limit has increased from 4TB to 16TB.

Using Older Way of Garbage Collection

To revert back to the Java 11 method of garbage collection, you can use the following options:

  • Use the -XX:-ZUncommit option.
  • Set the initial heap size (-Xms) and maximum heap size (-Xmx) to the same value.