O'Reilly logo

Java 11 Cookbook by Mohamed Sanaulla, Nick Samoylov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

To invoke the no-op garbage collector, use the -XX:+UseEpsilonGC option. At the time of writing, it requires an -XX:+UnlockExperimentalVMOptions option to access the new capability. 

We will use the following program for the demonstration:

package com.packt.cookbook.ch11_memory;import java.util.ArrayList;import java.util.List;public class Epsilon {    public static void main(String... args) {        List<byte[]> list = new ArrayList<>();        int n = 4 * 1024 * 1024;        for(int i=0; i < n; i++){            list.add(new byte[1024]);            byte[] arr = new byte[1024];        }    }}

As you can see, in this program, we are trying to allocate 4 GB of memory by adding a 1 KB array to the list at each iteration. At the same time, we also create a 1 K array, arr, at each iteration ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required