MemoreTest2

package education.jtrainer;

import java.util.ArrayList;

public class MemoryTest2 {

        ArrayList<byte[]> arrayHeap= new ArrayList<>();
        public static void main(String[] args) throws InterruptedException { 
            //Thread.sleep(60000);
            System.out.println("*****************MAIN METHOD STARTED*****************");
            MemoryTest2 MemoryTest=new MemoryTest2();
            for (int i=0; i<100000;i++) {
                MemoryTest.FillTheHeap();
                System.out.println("Heap filled with " + MemoryTest.arrayHeap.size()/10.0 +" Mbytes");    
                
            }
            System.out.println("*****************MEMORY TEST FINISHED*****************");
            
        }
        
        private void FillTheHeap() {
            byte[] numberObj=new byte[1000000]; // numberObj is a 1Mbyte array created in the Heap,  numberObj reference is kept in the stack
            arrayHeap.add(numberObj); //arrayHeap is referenced int he stack but the item of the list are in the heap
            try {
                Thread.sleep(100); //this is done for visualization purposes in VisualGC
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }