How to do it...

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

  1. The G1 GC is the default collector since Java 9, so you don't have to set anything if it is ...

Get Java 11 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.