Running the Garbage Collector Explicitly

This section provides a tutorial example on how to run the Garbage Collector explicitly by calling the gc() method on the Runtime instance.

In the next test, I want to learn how to use runFinalization() and gc() methods to force the Garbage Collector to run:

/**
 * RuntimeGarbageCollection.java
 * Copyright (c) 2010, HerongYang.com, All Rights Reserved.
 */
class RuntimeGarbageCollection {
   public static void main(String[] a) {
      java.io.PrintStream out = System.out;
      Runtime rt = Runtime.getRuntime();

      out.println("Starting the test...");
      out.println("   Total memory: " + rt.totalMemory());
      out.println("    Free memory: " + rt.freeMemory());

      out.println("Freeing unused objects...");
      rt.runFinalization();
      rt.gc();
      out.println("   Total memory: " + rt.totalMemory());
      out.println("    Free memory: " + rt.freeMemory());

      out.println("Allocating and discarding object...");
      bigObect();
      out.println("   Total memory: " + rt.totalMemory());
      out.println("    Free memory: " + rt.freeMemory());

      out.println("Freeing unused objects...");
      rt.runFinalization();
      rt.gc();
      out.println("   Total memory: " + rt.totalMemory());
      out.println("    Free memory: " + rt.freeMemory());
   }
   public static void bigObect() {
      Object o = new long[2*1024*128]; // 2 MB
      o = null;
   }
}

When executed on my Windows 7 system with JDK 1.7.0, I got this result:

C:\herong\jvm>java RuntimeGarbageCollection
Starting the test...
   Total memory: 16252928
    Free memory: 15965120
Freeing unused objects...
   Total memory: 16318464
    Free memory: 15978472
Allocating and discarding object...
   Total memory: 16318464
    Free memory: 13881304
Freeing unused objects...
   Total memory: 16318464
    Free memory: 16070080

The test result tells me that:

As a comparison, here is the result of the same program executed on my Windows XP system with JDK 1.6.0 in 2010:

C:\herong\jvm>java RuntimeGarbageCollection
Starting the test...
   Total memory: 5177344
    Free memory: 4993128
Freeing unused objects...
   Total memory: 5177344
    Free memory: 5049032
Allocating and discarding object...
   Total memory: 5177344
    Free memory: 2951864
Freeing unused objects...
   Total memory: 5177344
    Free memory: 5049032

Conclusion: running the garbage collector explicitly does free up memory used by discarded objects. But you don't have to do this in your application, since JVM has a separate thread running the garbage collector automatically.

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

 What Is Runtime?

 Printing Runtime Basic Information

Running the Garbage Collector Explicitly

 Shutting Down or Terminating the JVM Instance

 Executing System Commands

 Loading Native Libraries

 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

 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