Working with ZGC involves multiple steps. You should install the JDK binary, which is specific to Linux/x64, and build and start it. You can use the following commands to download ZGC and build it on your system:
$ hg clone http://hg.openjdk.java.net/jdk/jdk $ cd zgc $ sh configure --with-jvm-features=zgc $ make images
After execution of the preceding commands, you can find the JDK root directory in the following location:
g./build/linux-x86_64-normal-server-release/images/jdk
Java tools, such as java, javac, and others can be found in the /bin subdirectory of the preceding path (its usual location).
Let's create a basic HelloZGC class, as follows:
class HelloZGC { public static void main(String[] args) { System.out.println("Say hello to new low pause GC - ZGC!"); } }
You can use the following command to enable ZGC and use it:
java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC HelloZGC
Since ZGC is an experimental GC, you need to unlock it using the runtime option, that is, XX:+UnlockExperimentalVMOptions.
For enabling basic GC logging, you can add the -Xlog:gc option. Let's modify the preceding code, as follows:
java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xlog:gc HelloZGC
Detailed logging is helpful when you are fine-tuning your application. You can enable it by using the -Xlog:gc* option as follows:
java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xlog:gc* HelloZGC
The previous command will output all the logs to the console, which could make it difficult to search for specific content. You can specify the logs to be written to a file as follows:
java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xlog:gc:mylog.log* HelloZGC
Let's take a sneak peek into how ZGC arranges the heap for object allocation (in short, let's start with exploring the secret sauce of ZGC).