JVM Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.10

Garbage Collection Process

This section provides a tutorial example on watch how the garbage collection process works.

I wrote the following program to watch how the garbage collection process works:

/**
 * GarbageCollection.java
 * Copyright (c) 2002 by Dr. Herong Yang, herongyang.com
 */
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("Free/total memory:");
      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();
         System.out.println(rt.freeMemory()+"   "+rt.totalMemory());
      	 try {
      	    Thread.sleep(1000/10);
      	 } catch (InterruptedException e) {
            System.out.println("Interreupted...");
      	 }
      }
   }
   private static Object getOneMega() {
      // return new long[1024*128]; // 1MB
      Object[] lst = new Object[10];
      lst[0] = new long[256*128];
      lst[1] = new int[256*256];
      lst[2] = new double[256*128];
      lst[3] = new float[64*256];
      lst[4] = new byte[64*1024];
      String[] l = new String[64*64];
      for (int i=0; i<64*64; i++) 
      	 l[i] = new String("12345678"); // 16B
      lst[5] = l;
      lst[6] = new char[64*512];
      return lst;
   }
}

Output on HotSpot Client VM with -Xms2m -Xmx64m

Free/total memory:
......
30568544   65839104
29469976   65839104
28371408   65839104
27272840   65839104
26176960   65839104
25078392   65839104
23979824   65839104
22881256   65839104
21782688   65839104
20687480   65839104
19588912   65839104
18490344   65839104
17391776   65839104
16295896   65839104
15197328   65839104
14098760   65839104
13000192   65839104
11901624   65839104
10806416   65839104
9707848   65839104
8609280   65839104
7510712   65839104
6414832   65839104
5316264   65839104
4217696   65839104
3119128   65839104
2020560   65839104
30568544   65839104
......

I am only showing one segment of the output here. You can see that the garbage collector only starts to work when the free memory is getting close the specified maximum, 64MB.

Last update: 2002.

Table of Contents

 About This Book

 Download and Install Java SE 1.6 Update 2

 java.lang.Runtime Class - The JVM Instance

 java.lang.System Class - The Operating System

 ClassLoader Class - Class Loaders

 Class Class - Class Reflections

 Sun's JVM - Java HotSpot VM

 JRockit JVM 7.0 by BEA Systems

 JRockit JVM 8.0 by BEA Systems

Memory Management Rules and Tests

 Memory Management General Rules

 Java Exception: java.lang.OutOfMemoryError

Garbage Collection Process

 Garbage Collection Tests

 Stack Overflow Tests

 Thread Testing Program and Result

 StringBuffer Testing Program and Result

 CDS (Class Data Sharing)

 Micro Benchmark Runner and JVM Options

 Micro Benchmark Tests on "int" Operations

 Micro Benchmark Tests on "long" Operations

 Micro Benchmark Tests in JIT Compilation Mode

 Micro Benchmark Tests on "float" and "double" Operations

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2010
Garbage Collection Process