GCTest2.java - Garbage Collection Test Program

This section describes a garbage collection test program - GCTest2.java.

We all know that objects that are not referenced will be removed from memory by the garbage collector. But we don't know how this process works exactly. I wrote the following program to try to figure this out.

/* GCTest2.java
 * Copyright (c) 2013, HerongYang.com, All Rights Reserved.
 */
import java.util.*;
class GCTest2 {
   static MyList objList = null;
   static int wait = 1000; // in milliseconds: 1 second
   static int objSize = 1280; // in KB, default = 1.25 M
   static int initSteps = 320; // # of initial objects
   static int testSteps = 32; // # of added objects
   public static void main(String[] arg) {
      if (arg.length>0) objSize = Integer.parseInt(arg[0]);
      if (arg.length>1) initSteps = Integer.parseInt(arg[1]);
      if (arg.length>2) testSteps = Integer.parseInt(arg[2]);
      System.out.println("Test parameters:");
      System.out.println("   Object size: "+objSize+"KB");
      System.out.println("   Initial objects and data size: "
         +initSteps+", "+(initSteps*objSize)+"KB");
      System.out.println("   Added objects and data size: "
         +testSteps+", "+(testSteps*objSize)+"KB");
      objList = new MyList();
      myTest();
   }
   public static void myTest() {
      for (int m=0; m<initSteps; m++) {
         objList.add(new MyObject());
      }

      Runtime rt = Runtime.getRuntime();
      System.out.println(
         "Time  Total  Free  Used  Free  Total  Act.  Dead  Over");
      System.out.println(
         "sec.   Mem.  Mem.  Mem.    %.   Obj.  Obj.  Obj.  Head");
      long dt0 = System.currentTimeMillis()/1000;
      while (true) {
         for (int m=0; m<testSteps; m++) {
            objList.add(new MyObject());
         }
         for (int m=0; m<testSteps; m++) {
            objList.removeTail();
         }

         mySleep(wait);
         long tm = rt.totalMemory()/1024;
         long fm = rt.freeMemory()/1024;
         long ratio = (100*fm)/tm;
         long dt = System.currentTimeMillis()/1000 - dt0;
         long to = MyObject.getCount()*objSize;
         long ao = MyList.getCount()*objSize;
         System.out.println(dt 
            +"  "+tm+"  "+fm+"  "+(tm-fm)+"  "+ratio+"%"
            +"  "+to+"  "+ao+"  "+(to-ao)
            +"  "+(tm-fm-to));
      }
   }
   static void mySleep(int t) {
      try {
         Thread.sleep(t);
      } catch (InterruptedException e) {
         System.out.println("Interreupted...");
      }
   }
   static class MyObject {
      private static long count = 0;
      private long[] obj = null;
      public MyObject next = null;
      public MyObject prev = null;
      public MyObject() {
         count++;
         obj = new long[objSize*128]; //128*8=1024 bytes
      }
      protected final void finalize() {
         count--;
      }
      static long getCount() {
         return count;
      }
   }
   static class MyList {
      static long count = 0;
      MyObject head = null;
      MyObject tail = null;
      static long getCount() {
         return count;
      }
      void add(MyObject o) {
      	 // add the new object to the head;
         if (head==null) {
            head = o;
            tail = o; 
         } else {
            o.prev = head;
            head.next = o;
            head = o;
         }
         count++;
      }
      void removeTail() {
      	 if (tail!=null) {
      	    if (tail.next==null) {
      	       tail = null;
      	       head = null;
      	    } else {
      	       tail = tail.next; 
      	       tail.prev = null;
      	    }
      	    count--;
      	 }
      }
      void removeHead() {
      	 if (head!=null) {
      	    if (head.prev==null) {
      	       tail = null;
      	       head = null;
      	    } else {
      	       head = head.prev;
      	       head.next = null;
      	    }
      	    count--;
      	 }
      }
   }
}

Some notes on the test program:

Last update: 2014.

Table of Contents

 About This Book

 Downloading and Installing JDK 1.8.0 on Windows

 Downloading and Installing JDK 1.7.0 on Windows

 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 28.2.7 by Oracle Corporation

 JVM Runtime Data Areas

 Memory Management and Garbage Collectors

Garbage Collection Tests

GCTest2.java - Garbage Collection Test Program

 640/400/40MB Test on HotSpot Client 1.7

 640/400/40MB Test on HotSpot Client 1.6

 640/400/40MB Test on JRockit 28.2

 441/400/40MB Test on JRockit 28.2

 1076/800/80MB Test on HotSpot Client 1.7

 1076/800/80MB Test on HotSpot Client 1.6

 1076/800/80MB Test on JRockit 28.2

 JVM Stack, Frame and Stack Overflow

 Thread Testing Program and Result

 CPU Impact of Multi-Thread Applications

 I/O Impact of Multi-Thread Applications

 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

 Outdated Tutorials

 References

 PDF Printing Version