ZGC options with Java 16

Java 16 provides production ready ZGC garbage collection.

For example here is a sample code that is going to run out of memory with a regular JRE integration, I compared the code run with Java 8 and Java 16. There is an option to log the GC cycles with a VM option, but i felt it was too overwhelming for me to understand, so I took a simpler stats information.

package me.sathish;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class MemorySample {
    public static void main(String[] args) {
        LocalDateTime startTime = LocalDateTime.now();
        System.out.println("Starting time--" + startTime);
        getMemoryStats();
        List<Integer> someList = new ArrayList<>();
        List<Integer> someList1 = new ArrayList<>();
        try {
            for (int i = 0; i < 100000000; i++) {
                someList.add(i);
                someList1.add(i);
            }
        } finally {
            Duration duration = Duration.between(LocalDateTime.now(), startTime);
            getMemoryStats();
            System.out.println("The time diff is --" + duration.getSeconds());
        }
    }

    /**
     * Code barrowed from 
     * https://www.viralpatel.net/getting-jvm-heap-size-used-memory-total-memory-using-java-runtime/
     */
    private static void getMemoryStats() {
        Runtime runtime = Runtime.getRuntime();
        int mb = 1024 * 1024;
        System.out.println("##### Heap utilization statistics [MB] #####");
        //Print free memory
        System.out.println("Used Memory:"
                + (runtime.totalMemory() - runtime.freeMemory()) / mb);
        System.out.println("Free Memory:"
                + runtime.freeMemory() / mb);
        System.out.println("Total Memory:" + runtime.totalMemory() / mb);
        System.out.println("Max Memory:" + runtime.maxMemory() / mb);
    }
}

Now with JAVA 8 the increase the heap VM options to –Xmx4096m– took a total of 18 seconds to complete the above program

##### Start Heap utilization statistics [MB] #####
Used Memory:2
Free Memory:189
Total Memory:192
Max Memory:4096


##### End Heap utilization statistics [MB] #####
Used Memory:3857
Free Memory:238
Total Memory:4096
Max Memory:4096
The time diff is ---18

Enter new JAVA 16 world., with VM options -Xmx4096m -XX:+UseZGC

Total Memory:192
Max Memory:4096
##### Heap utilization statistics [MB] #####
Used Memory:3492
Free Memory:604
Total Memory:4096
Max Memory:4096
The time diff is ---29
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.base/java.util.Arrays.copyOf(Arrays.java:3511)
	at java.base/java.util.Arrays.copyOf(Arrays.java:3480)
	at java.base/java.util.ArrayList.grow(ArrayList.java:237)
	at java.base/java.util.ArrayList.grow(ArrayList.java:244)
	at java.base/java.util.ArrayList.add(ArrayList.java:454)
	at java.base/java.util.ArrayList.add(ArrayList.java:467)

Another set of options that JAVA 16 gives., new VM options for this run was

-XX:+UseZGC -XX:ZUncommitDelay=10 -XX:MaxHeapSize=10000000024,

A slick 10 seconds to complete everything

##### Start Heap utilization statistics [MB] #####
Used Memory:8
Free Memory:184
Total Memory:192
Max Memory:9538

##### End Heap utilization statistics [MB] #####
Used Memory:7548
Free Memory:0
Total Memory:7548
Max Memory:9538
The time diff is ---10

With JAVA 16 the uncommitDelay can be reduced, so with the following VM options

-XX:+UseZGC -Xmx9538m -XX:ZUncommitDelay=1, the code finished with a 44 second runtime.

Take note of the free memory.

##### Start Heap utilization statistics [MB] #####
Used Memory:8
Free Memory:184
Total Memory:192
Max Memory:9538
##### End Heap utilization statistics [MB] #####
Used Memory:6938
Free Memory:0
Total Memory:6938
Max Memory:9538
The time diff is ---44

So tried one other option with the a cool concurrent GC thread option so the new VM options are

XX:MaxHeapSize=10000000024 -XX:+UseParallelGC -XX:ConcGCThreads=4 -XX:ZUncommitDelay=1

The output for the VM options. The code took the time to complete, but the amount of free memory was around full 1038MB

##### Start Heap utilization statistics [MB] #####
Used Memory:4
Free Memory:179
Total Memory:184
Max Memory:8478
##### End Heap utilization statistics [MB] #####
Used Memory:3862
Free Memory:1038
Total Memory:4901
Max Memory:8478
The time diff is ---115

finally, one last VM options and the output, with a balance of memory usage

-XX:+UseZGC -XX:MaxHeapSize=5126M -XX:ZUncommitDelay=1

##### Start Heap utilization statistics [MB] #####
Used Memory:8
Free Memory:184
Total Memory:192
Max Memory:5126
##### End Heap utilization statistics [MB] #####
Used Memory:4674
Free Memory:452
Total Memory:5126
Max Memory:5126
The time diff is ---52

So nice production ready options with JAVA 16 to work with the new ZGC GC model.

Finally always a great watch.,