O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

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:
        package com.packt.cookbook.ch12_memory;        import java.util.ArrayList;        import java.util.List;        import java.util.stream.IntStream;        public class Chapter12Memory {          public static void main(String... args) {            int max = 99888999;            System.out.println("Chapter12Memory.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 ...

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