Getting started with ZGC Garbage collector

This article will discuss the Java ZGC garbage collector and how you can configure and use it to meet your needs in modern Java applications.

What is the Java ZGC?

The Java ZGC is a scalable low latency garbage collector that is production-ready since Java 15. The ZGC Collector is designed to minimize GC pauses, while still providing high throughput. ZGC is a concurrent and incremental collector, meaning that it can run in the background while the application is still running. It also uses a parallel and concurrent marking algorithm, which helps to keep GC pauses to a minimum.

What are its goals?

The main goals of the ZGC collector are the following ones:

  • Low latency: The Java ZGC is able to minimize GC pauses and keep them short. The goal is that pauses do not exceed 10ms
  • Scalability: The Java ZGC can efficiently handle heaps up to several terabytes in size.
  • Ease of use: The Java ZGC is easy to use and require minimal configuration.

What are its features?

One of the most important features of the Java ZGC is its scalability. Previous generations of GCs would often struggle to keep up with large applications, leading to long pauses and poor performance. The Java ZGC is designed to be much more scalable, able to handle large heaps with ease. This means that your application can continue running smoothly, even as it grows in size.

Another great feature of the Java ZGC is its low latency. This means that your application will suffer less from GC pauses, and will be able to respond more quickly to user input. In addition, the Java ZGC is more efficient than previous GC generations, meaning that it will use less CPU time and memory. This makes it ideal for applications that need to be highly responsive, such as games or real-time applications.

How does it work?

Java ZGC uses a number of techniques to minimize latency and maximize throughput. One of these is concurrent marking, which allows the garbage collector to run in the background while the application is running. This means that there is no need for pauses or stops in order to collect garbage. Additionally, ZGC uses a number of heuristics to optimize its performance. For example, it uses an adaptive mutator detection algorithm to identify which threads are most active and need the most resources. This helps ZGC to focus its resources on the areas of the application that are most important.

ZGC Phases

The Java ZGC garbage collector uses a set of phases to complete its garbage collection cycles.

  • Pause Mark Start: The ZGC walks through the object graph to mark the objects live or garbage. This phase also includes the remapping of live data.
  • Pause Mark End: This is where reference preprocessing is done. The class unloading and relocation set selection are also done in this phase.
  • Pause Relocate Start, the last phase, is where the heavy job of compacting the heap happens.
zgc garbage collector

Enabling the ZGC

In order to use the ZGC as your Garbage collector add the following option to the start up of your Java application:

 -XX:+UseZGC

Please note: from the JDK 11 until the JDK 15 release, you must unlock the experimental options to use the ZGC algorithm:

-XX:+UnlockExperimentalVMOptions -XX:+UseZGC

Along with the above option, it is crucial that you include the max heap size (-Xmx) option for your application. This is because the ZGC behavior depends on allocation rate variance and how much of the data set is live.

ZCG settings

To Control the number of Threads the ZGC has heuristics to automatically select a fair number. This heuristic usually works in most cases. However, in some cases you might need to adjust it through the following parameter:

  • -XX:ConcGCThreads : Sets the number of concurrent GC threads (default -XX:+ConcGCThreads=1). An high number of threads might cause the GC will steal to overuse the CPU-time from the application. A low number might cause your application to allocate garbage faster than the GC can collect it.

The following settings are peculiar of ZGC Collector:

  • -XX:ZAllocationSpikeTolerance : Sets the allocation spike tolerance for ZGC. The default value is 2. The bigger this value, the higher allocation rate you can expect.
  • -XX:ZCollectionInterval : This is the minimum time interval for ZGC to occur, in seconds (default = 0.000000)
  • -XX:ZFragmentationLimit : If the current region fragmentation is greater than this value, the region will be reclaimed (default = 25.000000) -XX:ZMarkStackSpaceLimit specifies the maximum number of bytes allocated for the mark stack (default = 8589934592 = 8096M)
  • -XX:ZProactive : Whether to enable active recycling (default true)
  • -XX:ZUncommit : Whether to return unused memory to the OS (default true)
  • -XX:ZUncommitDelay : Sets the amount of time (in seconds) that heap memory must have been idle before being uncommitted

Conclusion

The ZGC is a new garbage collector that is able to provide low latency GC with high scalability. Since Java 15 you can now use it in production environments to allow large Java applications to suffer less from GC pauses while being able to respond more quickly to user input.

Found the article helpful? if so please follow us on Socials