gc() - The Garbage Collection Method

This section provides a tutorial example on how to use the garbage collection method gc() on the default Runtime object to control the garbage collection process by yourself.

If you do not like the way JVM automatically performing the garbage collection process. you can use the gc() method of the default Runtime object to control the garbage collection process by yourself.

To test this gc() method, I modified the tutorial example presented in the previous section to explicitly perform garbage collection whenever the free memory size is below 10 MB:

/* GarbageCollectionForced.java
 - Copyright (c) 2014, HerongYang.com, All Rights Reserved.
 */
class GarbageCollectionForced {
   public static void main(String[] a) {
      int steps = 10000;
      int size = 32;
      Object[] queue = new Object[size];
      Runtime rt = Runtime.getRuntime();
      System.out.println("Memory: Maximum   Total   Free   Used");
      for (int m=0; m<steps; m++) {
         for (int n=0; n<size-1; n++) 
            queue[size-n-1] = queue[size-n-2];
         queue[0] = getOneMega();

         System.out.println(m+"   "+rt.maxMemory()
            +"   "+rt.totalMemory()+"   "+rt.freeMemory()
            +"   "+(rt.totalMemory()-rt.freeMemory()));
         // Forcing a garbage collection
         if (rt.freeMemory()<10485760) {
            rt.gc();
            System.out.println(m+"   "+rt.maxMemory()
               +"   "+rt.totalMemory()+"   "+rt.freeMemory()
               +"   "+(rt.totalMemory()-rt.freeMemory())
               +"   //Garbage collected");
         }
         try {
            Thread.sleep(1000/10);
         } catch (InterruptedException e) {
            System.out.println("Interreupted...");
         }
      }
   }
   private static Object getOneMega() {
      return new long[1024*128]; // 1MB
   }
}

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

Memory: 
      Maximum      Total       Free       Used
 0   66650112    5177344    3948456    1228888
 0   66650112    5177344    4000416    1176928   //Garbage collected
 1   66650112    5177344    2951824    2225520
 1   66650112    5177344    2951824    2225520   //Garbage collected
 2   66650112    5177344    1903232    3274112
 2   66650112    6410240    3136128    3274112   //Garbage collected
 3   66650112    6410240    2087536    4322704
 3   66650112    8159232    3836672    4322560   //Garbage collected
 4   66650112    8159232    2788080    5371152
 4   66650112    9908224    4537072    5371152   //Garbage collected
 5   66650112    9908224    3488480    6419744
 5   66650112   11653120    5233376    6419744   //Garbage collected
 6   66650112   11653120    4184784    7468336
 6   66650112   13402112    5933776    7468336   //Garbage collected
 7   66650112   13402112    4885184    8516928
 7   66650112   15347712    6826848    8520864   //Garbage collected
 8   66650112   15347712    5778256    9569456
 8   66650112   17158144    7599600    9558544   //Garbage collected
 9   66650112   17158144    6551008   10607136
 9   66650112   19038208    8434088   10604120   //Garbage collected
10   66650112   19038208    7385496   11652712
10   66650112   20918272    9267840   11650432   //Garbage collected
11   66650112   20918272    8219248   12699024
11   66650112   22794240   10096904   12697336   //Garbage collected
12   66650112   22794240    9048312   13745928
12   66650112   24674304   10929744   13744560   //Garbage collected
13   66650112   24674304    9881152   14793152
13   66650112   26554368   11762080   14792288   //Garbage collected
14   66650112   26554368   10713488   15840880
15   66650112   26554368    9665008   16889360
15   66650112   30310400   13421040   16889360   //Garbage collected
16   66650112   30310400   12372448   17937952
17   66650112   30310400   11323856   18986544
18   66650112   30310400   10275264   20035136
18   66650112   35946496   15911360   20035136   //Garbage collected
19   66650112   35946496   14862768   21083728
20   66650112   35946496   13814176   22132320
21   66650112   35946496   12765584   23180912
22   66650112   35946496   11716992   24229504
23   66650112   35946496   10668400   25278096
24   66650112   35946496    9619808   26326688
24   66650112   47218688   20892000   26326688   //Garbage collected
25   66650112   47218688   19843408   27375280
26   66650112   47218688   18794816   28423872
27   66650112   47218688   17746224   29472464
28   66650112   47218688   16697632   30521056
29   66650112   47218688   15649040   31569648
30   66650112   47218688   14600448   32618240
31   66650112   47218688   13551856   33666832
32   66650112   47218688   12503264   34715424
33   66650112   47218688   11454672   35764016
34   66650112   47218688   10406080   36812608
34   66650112   62246912   27531488   34715424   //Garbage collected
35   66650112   62246912   26482896   35764016
36   66650112   62246912   25434304   36812608
37   66650112   62246912   24385712   37861200
38   66650112   62246912   23337120   38909792
39   66650112   62246912   22288528   39958384
40   66650112   62246912   21239936   41006976
41   66650112   62246912   20191344   42055568
42   66650112   62246912   19142752   43104160
43   66650112   62246912   18094160   44152752
44   66650112   62246912   17045568   45201344
45   66650112   62246912   15996976   46249936
46   66650112   62246912   14948384   47298528
47   66650112   62246912   13899792   48347120
48   66650112   62246912   12851200   49395712
49   66650112   62246912   11802608   50444304
50   66650112   62246912   10754016   51492896
51   66650112   62246912    9705424   52541488
51   66650112   62246912   27531488   34715424   //Garbage collected
...

Several interesting notes about the output:

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 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

 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

 Outdated Tutorials

 References

 PDF Printing Version