Example Program of Using the gc() Method

This section provides a tutorial example on how to use the garbage collection method gc() on the default Runtime object to test object references and memory usages.

If you want to monitor memory usage of your data objects, you can call the freeMemory(), totalMemory(), and maxMemory() methods of the default Runtime object.

Here is a simple tutorial example to test object references and memory usages:

/* ObjectReferenceAndMemory.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
public class ObjectReferenceAndMemory {
   private static long[] fm = new long[2];
   private static long[] tm = new long[2];
   private static long[] mm = new long[2];
   private static long[] um = new long[2];
   public static void main(String[] args) {
      Runtime rt = Runtime.getRuntime();
      Object h, l, t;
      getMemoryInfo(rt,0);
      h = createOne();
      getMemoryInfo(rt,1);
      printMemoryInfo("One object created:");
      getMemoryInfo(rt,0);
      h = null;
      getMemoryInfo(rt,1);
      printMemoryInfo("One object removed:");

      getMemoryInfo(rt,0);
      h = createList();
      getMemoryInfo(rt,1);
      printMemoryInfo("A linked list of objects created:");
      getMemoryInfo(rt,0);
      h = null;
      getMemoryInfo(rt,1);
      printMemoryInfo("The reference of the first object removed:");

      getMemoryInfo(rt,0);
      h = createList();
      getMemoryInfo(rt,1);
      printMemoryInfo("A linked list of objects created:");
      getMemoryInfo(rt,0);
      l = ((OneMegaArray) h).last(); // the last object of the list
      h = null;
      getMemoryInfo(rt,1);
      printMemoryInfo("The reference of the first object removed,"
         + "\nbut the reference of the last object is maintained:");

      getMemoryInfo(rt,0);
      h = createCircle();
      getMemoryInfo(rt,1);
      printMemoryInfo("A linked circle of objects created:");
      getMemoryInfo(rt,0);
      t = ((OneMegaArray) h).next;
      t = ((OneMegaArray) t).next; // the third object of the circle
      h = null;
      getMemoryInfo(rt,1);
      printMemoryInfo("The reference of the first object removed,"
         + "\nbut the reference of the third object is maintained:");
   }
   public static Object createOne() {
      return new OneMegaArray();
   }
   public static Object createList() {
      OneMegaArray o3 = new OneMegaArray();
      OneMegaArray o2 = new OneMegaArray();
      o2.next = o3;
      OneMegaArray o1 = new OneMegaArray();
      o1.next = o2;
      return o1;
   }
   public static Object createCircle() {
      OneMegaArray o3 = new OneMegaArray();
      OneMegaArray o2 = new OneMegaArray();
      o2.next = o3;
      OneMegaArray o1 = new OneMegaArray();
      o1.next = o2;
      o3.next = o1;
      return o1;
   }
   public static void printMemoryInfo(String title) {
      System.out.println("\n"+title);
      System.out.println("Free memory: "+fm[1]+" - "+fm[0]
         +" = " +(fm[1]-fm[0]));
      System.out.println("Total memory: "+tm[1]+" - "+tm[0]
         +" = " +(tm[1]-tm[0]));
      System.out.println("Maximum memory: "+mm[1]+" - "+mm[0]
         +" = " +(mm[1]-mm[0]));
      System.out.println("Memory used: "+um[1]+" - "+um[0]
         +" = " +(um[1]-um[0]));
   }
   public static void getMemoryInfo(Runtime rt, int i) {
      rt.gc();
      fm[i] = rt.freeMemory();
      tm[i] = rt.totalMemory();
      mm[i] = rt.maxMemory();
      um[i] = tm[i]-fm[i];
   }
   private static class OneMegaArray {
      private long[] data = new long[128*1024]; // 1 MB data
      public Object next = null; // a pointer
      public Object last() {
         OneMegaArray o = this;
         while (o.next!=null) o = (OneMegaArray)o.next;
         return o;
      }
   }
}

Running this modified sample program with JDK 1.6, I got the following output:

One object created:
Free memory: 4018760 - 5067440 = -1048680
Total memory: 5177344 - 5177344 = 0
Maximum memory: 66650112 - 66650112 = 0
Memory used: 1158584 - 109904 = 1048680

One object removed:
Free memory: 5067584 - 4018760 = 1048824
Total memory: 5177344 - 5177344 = 0
Maximum memory: 66650112 - 66650112 = 0
Memory used: 109760 - 1158584 = -1048824

A linked list of objects created:
Free memory: 3154656 - 5067584 = -1912928
Total memory: 6410240 - 5177344 = 1232896
Maximum memory: 66650112 - 66650112 = 0
Memory used: 3255584 - 109760 = 3145824

The reference of the first object removed:
Free memory: 6300480 - 3154656 = 3145824
Total memory: 6410240 - 6410240 = 0
Maximum memory: 66650112 - 66650112 = 0
Memory used: 109760 - 3255584 = -3145824

A linked list of objects created:
Free memory: 3154656 - 6300480 = -3145824
Total memory: 6410240 - 6410240 = 0
Maximum memory: 66650112 - 66650112 = 0
Memory used: 3255584 - 109760 = 3145824

The reference of the first object removed,
but the reference of the last object is maintained:
Free memory: 5251872 - 3154656 = 2097216
Total memory: 6410240 - 6410240 = 0
Maximum memory: 66650112 - 66650112 = 0
Memory used: 1158368 - 3255584 = -2097216

A linked circle of objects created:
Free memory: 3855040 - 5251872 = -1396832
Total memory: 8159232 - 6410240 = 1748992
Maximum memory: 66650112 - 66650112 = 0
Memory used: 4304192 - 1158368 = 3145824

The reference of the first object removed,
but the reference of the third object is maintained:
Free memory: 3855040 - 3855040 = 0
Total memory: 8159232 - 8159232 = 0
Maximum memory: 66650112 - 66650112 = 0
Memory used: 4304192 - 4304192 = 0

Nothing unexpected in the output.

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

Garbage Collection and the gc() Method

 Garbage Collection and Unused Objects

 The Automated Garbage Collection Process

 gc() - The Garbage Collection Method

Example Program of Using the gc() Method

 Assert Statements and -ea" Option

 Annotation Statements and Declarations

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB