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 ...