Garbage Collection Testing Program

This section provides a tutorial example on how to write a simple program to test how the garbage collector works.

Since most of "jstat" options are about garbage collection statistics, I wrote this garbage collection testing program to test the "jstat" tool:

/* GarbageCollection.java
#- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
 */
class GarbageCollection {
   public static void main(String[] a) {
      int max = 10000;
      int min = 16;
      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;
   }
}

When I run this program, I get this output:

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

C:\herong>\Progra~1\java\jdk1.8.0\bin\java 
   -Xms24m -Xmx24m GarbageCollection

Free/total memory:
23725256   25034752
22710400   25034752
21618728   25034752
20523584   25034752
19429328   25034752
18330752   25034752
17237416   25034752
16137560   25034752
15040664   25034752
13943848   25034752
12843752   25034752
11747448   25034752
10650832   25034752
 9552096   25034752
 8452192   25034752
 7357216   25034752
 6258720   25034752
 5159112   25034752
 4060976   25034752
 2965296   25034752
 1865656   25034752
  767256   25034752
 7356032   25034752   // Garbage Collection
 6257576   25034752
 5159368   25034752
 4060712   25034752
 2964336   25034752
 1867032   25034752
  767176   25034752
 7356512   25034752   // Garbage Collection
 6258136   25034752
 5159360   25034752
 4060464   25034752
 2964064   25034752
 1866728   25034752
  766568   25034752
 7356272   25034752   // Garbage Collection
...

The output shows that the garbage collector is working correctly to remove those objects shifted out of the arr[].

Last update: 2015.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing Java 8 on Windows

 'javac' - The Java Program Compiler

 'java' - The Java Program Launcher

 'jdb' - The Java Debugger

 'jconsole' - Java Monitoring and Management Console

'jstat' - JVM Statistics Monitoring Tool

 'jps' - JVM Process Status Tool

 Listing JVM Processes on the Local Machine with "jps"

 'jstatd' - JVM Remote Monitoring Server

 Starting 'jstatd' with a Security Policy File

 Connecting to 'jps' to Remote 'jstatd'

 'jstat' Command Options and Parameters

Garbage Collection Testing Program

 'jstat -gcutil' - Garbage Collection Statistics

 Accessing Remote JVM Processes with 'jstat'

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

 'jar' - The JAR File Tool

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

 'native2ascii' - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 PDF Printing Version