Skip to Content

Run the garbage collector using System class

  1. /*
  2.         Run the garbage collector using System class
  3.         This example shows how to run the garbage collector
  4.         by using gc method of Java System class.
  5. */
  6.  
  7. public class RunTheGarbageCollector {
  8.  
  9.         public static void main(String[] args) {
  10.                
  11.                 /*
  12.                  * To run the garbage collector thread use
  13.                  * public static void gc() method of System class.
  14.                  *
  15.                  * Please note that, calling this method only suggest JVM
  16.                  * to run the garbage collection process. It does not guarantee
  17.                  * that all eligible objects will be garbage collected.
  18.                  */
  19.                  
  20.                  System.out.println("Suggesting VM to run garbage collector");
  21.                  System.gc();
  22.                  System.out.println("Suggested VM to garbage collect objects");
  23.         }
  24. }