The Z Garbage Collector algorithm (JDK 15 version)
By: Jesús Navarrete
Tags: Java, Zgc, Algorithms, Jdk 15, Garbage Collector

Tuning options

Let me describe how to use ZGC and some interesting parameters to tune. From the JDK 11 until the JDK 15 release, you must unlock the experimental options if you want to use the ZGC algorithm:

-XX:+UnlockExperimentalVMOptions -XX:+UseZGC

From JDK 15 in advance, you can use it just by specifying:

-XX:+UseZGC

The ZGC was designed to be easy to tune. Below there is a list of the specific ZGC options:

In order to appreciate the times used and to see some numbers about the behavior of the algorithm, it is good to print the garbage collector logs, just add the following command when you choose ZGC to see the simple log:

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

Or if you want to print the garbage collector logs with more details:

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

Now, let’s start taking a look at the most interesting tuning options.

Setting Heap Size

One of the most important options to tune in ZGC is setting the max heap size (-Xmx<size>). We must find the correct value for our application because we don’t want to lose memory, and we want to allow our application to have enough space for live objects and allocations to happen, while GC is running. The following is an example of use:

-XX:+UseZGC -Xmx<size>

Setting Concurrent GC Threads

Although ZGC has heuristics for setting this number automatically, sometimes, depending on our application it could be interesting to specify the number of concurrent GC threads. This option rules how much CPU will your GC take, so you must be careful about the capacity you want to give.

-XX:+UseZGC -Xmx<size> -XX:ConcGCThreads=<number>

Returning Unused Memory to the Operating System

Unlike other GC algorithms, ZGC uncommits unused memory, giving it back to the operating system. This can be necessary for applications where memory footprint can be a problem. If you want to disable this option, you can use -XX:-ZUncommit.

-XX:+UseZGC -Xmx<size> -XX:-ZUncommit

Enabling Large Pages On Linux

This is an option that comes with an improvement in performance and no disadvantages or side effects. The only issue is that it requires root privileges, that’s why is not a default option and might not be possible to enable it for your application. Take a look at the documentation to set up this option properly. It requires to prepare somethings, and the options would look like the following:

-XX:+UseZGC -Xms16G -Xmx16G -XX:+UseLargePages

Enabling Transparent Huge Pages On Linux

Huges pages are not recommended for latency-sensitive applications, although it serves as a good alternative to the previous tuning option.

-XX:+UseZGC -… -XX:+UseLargePages -XX:+UseTransparentHugePages

In this case, I highly recommend you to experiment with it in your application and pay attention to the spikes, if they happen maybe it’s not an option for your case.

Enabling NUMA Support

As I said previously, ZGC is NUMA-aware, which means this option is enabled by default. This will direct Java heap allocations to NUMA-local memory. It can automatically be disabled by the JVM, if you need to override explicitly the behavior you can use the options -XX:+UseNUMA or -XX:-UseNUMA.

-XX:+UseZGC -Xmx<size> -XX:+UseNUMA

or

-XX:+UseZGC -Xmx<size> -XX:-UseNUMA

Many more on the wiki page of the algorithm.