Garbage Collection Demo Program

This section provides a tutorial example on watch how the garbage collection process works on HotSpot and other JVMs.

In the previous tutorial, we observed how JVM allocates more and more objects in the Heap. Eventually, JVM throws the OutOfMemoryError when the Heap runs out of space.

In this tutorial, we will observe how JVM deallocates unreferenced objects to free up space the Heap. The deallocation of unreferenced objects from the Heap is also called "Garbage Collection". The code in the JVM that allocates and deallocates objects is also called "Garbage Collector".

Here is my first sample program, GarbageCollection.java, that will shows us how the garbage collector works.

/* GarbageCollection.java
 * Copyright (c) 2018, HerongYang.com, All Rights Reserved.
 */
class GarbageCollection {
   public static void main(String[] a) {
      int max = 10000;
      int min = 32;
      Object[] arr = new Object[min];
      Runtime rt = Runtime.getRuntime();
      System.out.println("Step/TotalMemory/FreeMemory/UsedMemory:");
      for (int m=0; m<max; m++) {
      	 for (int n=0; n<min-1; n++) arr[min-n-1] = arr[min-n-2];
         arr[0] = getOneMega();
         long total = rt.totalMemory();
         long free = rt.freeMemory();
         System.out.println((m+1)+"   "+total+"   "+free+"   "
            +(total-free));
      	 try {
      	    Thread.sleep(1000);
      	 } catch (InterruptedException e) {
            System.out.println("Interreupted...");
      	 }
      }
   }
   private static Object getOneMega() {
      Object[] lst = new Object[10];
      lst[0] = new long[256*128]; // 1/4 MB
      lst[1] = new int[256*256]; // 1/4 MB
      lst[2] = new double[256*128]; // 1/4 MB
      lst[3] = new float[64*256]; // 1/16 MB
      lst[4] = new byte[64*1024]; // 1/16 MB
      String[] l = new String[64*64]; // 1/16 MB
      for (int i=0; i<64*64; i++) 
      	 l[i] = new String("12345678"); // 16B
      lst[5] = l;
      lst[6] = new char[64*512]; // 1/16 MB
      return lst;
   }
}

Notes on this tutorial program:

Compile and run the program with HotSpot 1.8 with -Xms2m -Xmx64m:

C:\herong>\progra~1\java\jdk1.8.0\bin\javac GarbageCollection.java

C:\>\progra~1\java\jdk1.8.0\bin\java -Xms2m -Xmx64m GarbageCollection
Step/TotalMemory/FreeMemory/UsedMemory:
  1    2031616     696840    1334776
  2    5562368    3165016    2397352
  3    5562368    2102168    3460200
  4    5562368    1043888    4518480
  5   12759040    7172168    5586872
  6   12759040    6102408    6656632
  7   12759040    5044352    7714688
  8   12759040    3974592    8784448
  9   12759040    2915128    9843912
 10   12759040    1848840   10910200
 11   12759040     782552   11976488
 12   28614656   15595824   13018832
 13   28614656   14528928   14085728
 14   28614656   13462032   15152624
 15   28614656   12395136   16219520
 16   28614656   11328240   17286416
 17   28614656   10261344   18353312
 18   28614656    9194448   19420208
 19   28614656    8135768   20478888
 20   28614656    7064272   21550384
 21   28614656    6007168   22607488
 22   28614656    4935672   23678984
 23   28614656    3878568   24736088
 24   28614656    2807072   25807584
 25   28614656    1735576   26879080
 26   63868928   35937040   27931888
 27   63868928   34852040   29016888
 28   63868928   33783440   30085488
 29   63868928   32714840   31154088
 30   63868928   31674728   32194200
 31   63868928   30589728   33279200
 32   63868928   29521128   34347800 - Array is fully loaded
 33   63868928   28452528   35416400
 34   63868928   27412416   36456512
 35   63868928   26327416   37541512
 36   63868928   25258816   38610112
 37   63868928   24190216   39678712
 38   63868928   23150104   40718824
 39   63868928   22081504   41787424
 40   63868928   20996504   42872424
 41   63868928   19927904   43941024
 42   63868928   18897752   44971176
 43   63868928   17831912   46037016
 44   63868928   16766072   47102856
 45   63868928   15672664   48196264
 46   63868928   14606824   49262104
 47   63868928   13540984   50327944
 48   63868928   12475144   51393784
 49   63868928   11409304   52459624
 50   63868928   10343464   53525464
 51   63868928    9277624   54591304
 52   63868928    8211784   55657144
 53   63868928    7145944   56722984
 54   63868928    6080104   57788824
 55   63868928    5014264   58854664
 56   63868928    3948424   59920504
 57   63868928    2882584   60986344
 58   64880640   28542728   36337912 - Garbage collected
 59   64880640   27460008   37420632
 60   64880640   26393688   38486952
 61   64880640   25327368   39553272
 62   64880640   24261048   40619592
 63   64880640   23194728   41685912
 64   64880640   22128408   42752232
 65   64880640   21062088   43818552
 66   64880640   19995768   44884872
 67   64880640   18929448   45951192
 68   64880640   17863128   47017512
 69   64880640   16796808   48083832
 70   64880640   15730488   49150152
 71   64880640   14664168   50216472
 72   64880640   13597848   51282792
 73   64880640   12531528   52349112
 74   64880640   11465208   53415432
 75   64880640   28542128   36338512 - Garbage collected
 76   64880640   27458808   37421832
 77   64880640   26391888   38488752
 78   64880640   25324968   39555672
 79   64880640   24258048   40622592
 80   64880640   23191128   41689512
 81   64880640   22124208   42756432
 82   64880640   21057288   43823352
 83   64880640   19990368   44890272
 84   64880640   18923448   45957192
 85   64880640   17856528   47024112
 86   64880640   16789608   48091032
 87   64880640   15722688   49157952
 88   64880640   14655768   50224872
 89   64880640   13588848   51291792
 90   64880640   12521928   52358712
 91   64880640   30561512   34319128 - Garbage collected
 92   64880640   29493832   35386808
 93   64880640   28409816   36470824
 94   64880640   27342200   37538440
 95   64880640   26274584   38606056
 96   64880640   25206968   39673672
 97   64880640   24139352   40741288
 98   64880640   23099896   41780744
 99   64880640   22015880   42864760
100   64880640   20948264   43932376
101   64880640   19880648   44999992
102   64880640   18813032   46067608
103   64880640   17745416   47135224
104   64880640   16705960   48174680
105   64880640   15638344   49242296
106   64880640   14554328   50326312
107   64880640   13486712   51393928
108   64880640   30560984   34319656 - Garbage collected
109   64880640   29467848   35412792
...

Notes on the output:

Running GarbageCollection.java on other versions of HotSpot and JRockit gives similar results.

Last update: 2018.

Table of Contents

 About This Book

 Heap Memory Area and Size Control

JVM Garbage Collection Logging

Garbage Collection Demo Program

 Garbage Collection Logging Options

 "-XX:+PrintGC" - GC Logging Option for Short Messages

 "-XX:+PrintGCDetails" - Option for Detailed GC Messages

 "-XX:+PrintGCDateStamps" - Calendar Timestamp on GC Messages

 "-XX:+PrintGCApplicationStoppedTime" - Application Pause Durations

 "-XX:+PrintGCApplicationConcurrentTime" - Application Running Durations

 "-XX:+PrintHeapAtGC" - Print Heap Summary per GC

 "-XX:+PrintTenuringDistribution" - Tunuring Distribution

 "-XX:+PrintReferenceGC" - Reference Counts per GC

 "-XX:+PrintFlagsFinal" - Print JVM Options

 Introduction of Garbage Collectors

 Serial Collector - "+XX:+UseSerialGC"

 Parallel Collector - "+XX:+UseParallelGC"

 Concurrent Mark-Sweep (CMS) Collector - "+XX:+UseConcMarkSweepGC"

 Garbage First (G1) Collector - "+XX:+UseG1GC"

 Object References and Garbage Collection

 Garbage Collection Performance Test Program

 Performance Tests on Serial Collector

 Performance Tests on Parallel collector

 Performance Tests on Concurrent collector

 Performance Tests on G1 collector

 Garbage Collection Performance Test Summary

 Outdated Tutorials

 References

 Full Version in PDF/EPUB