- To see how GC works, write the following program:
public class Chapter11Memory { public static void main(String... args) { int max = 99_888_999; System.out.println("Chapter11Memory.main() for " + max + " is running..."); List<AnObject> list = new ArrayList<>(); IntStream.range(0, max) .forEach(i -> list.add(new AnObject(i))); } private static class AnObject { private int prop; AnObject(int i){ this.prop = i; } } }
As you can see, it creates 99,888,999 objects and adds them to the List<AnObject> list collection. You might tune it by decreasing the maximum number of objects (max) to match the configuration of your computer.
- The G1 GC is the default collector since Java 9, so you don't have to set anything if it is ...