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 explicitly:

/* RuntimeGarbageCollection.java
 * Copyright (c) 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("    Used memory: " 
         + (rt.totalMemory()-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("    Used memory: " 
         + (rt.totalMemory()-rt.freeMemory()));

      out.println("Allocating and discarding object...");
      bigObect();
      out.println("   Total memory: " + rt.totalMemory());
      out.println("    Free memory: " + rt.freeMemory());
      out.println("    Used memory: " 
         + (rt.totalMemory()-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("    Used memory: " 
         + (rt.totalMemory()-rt.freeMemory()));
   }
   public static void bigObect() {
      Object o = new long[2*1024*128]; // 2 MB
      o = null;
   }
}

When executed with JVM in JDK 10, I got this result in 2018:

C:\herong>\Progra~1\Java\jdk-10.0.1\bin\java RuntimeGarbageCollection
Starting the test...
   Total memory: 62914560
    Free memory: 60982824
    Used memory: 1931736
Freeing unused objects...
   Total memory: 8388608
    Free memory: 7463536
    Used memory: 925072
Allocating and discarding object...
   Total memory: 8388608
    Free memory: 4322392
    Used memory: 4066216
Freeing unused objects...
   Total memory: 8388608
    Free memory: 7485208
    Used memory: 903400

The result tells me that:

When executed with JVM in JDK 1.7, I got this result in 2013:

 
C:\>\Progra~1\java\jdk1.7.0\bin\java RuntimeGarbageCollection
Starting the test...
   Total memory: 16252928
    Free memory: 15964936
    Used memory: 287992
Freeing unused objects...
   Total memory: 16318464
    Free memory: 15978808
    Used memory: 339656
Allocating and discarding object...
   Total memory: 16318464
    Free memory: 13881640
    Used memory: 2436824
Freeing unused objects...
   Total memory: 16318464
    Free memory: 16070416
    Used memory: 248048

The test result tells me that:

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 for you.

Last update: 2018.

Table of Contents

 About This Book

 JVM (Java Virtual Machine) Specification

 Java HotSpot VM - JVM by Oracle/Sun

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

 JVM Runtime Data Areas

 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

 JRockit JVM 28.2.7 by Oracle Corporation

 Outdated Tutorials

 References

 Full Version in PDF/EPUB