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

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

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