JVM Tutorials - Herong's Tutorial Examples - v5.13, by Herong Yang
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..."); bigObject(); 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 bigObject() { Object o = new long[2*1024*128]; // 2 MB o = null; } }
When executed with JVM in JDK 17, I got this result:
herong> \progra~1\java\jdk-17.0.1\bin\java RuntimeGarbageCollection.java Starting the test... Total memory: 67108864 Free memory: 49824072 Used memory: 17284792 Freeing unused objects... Total memory: 10485760 Free memory: 7714608 Used memory: 2771152 Allocating and discarding object... Total memory: 10485760 Free memory: 4326512 Used memory: 6159248 Freeing unused objects... Total memory: 10485760 Free memory: 7725784 Used memory: 2759976
The result tells me that:
When executed with JVM in JDK 10, I got this result:
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:
herong> \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.
Table of Contents
JVM (Java Virtual Machine) Specification
Java HotSpot VM - JVM by Oracle/Sun
►java.lang.Runtime Class - The JVM Instance
Printing Runtime Basic Information
►Running the Garbage Collector Explicitly
Shutting Down or Terminating the JVM Instance
java.lang.System Class - The Operating System
ClassLoader Class - Class Loaders
Class Class - Class Reflections
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
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