Saturday, September 3, 2011

Suggest JVM to Run Garbage Collector

     /*
    Suggest JVM to Run Garbage Collector Example
    This Java example shows how to suggest the JVM to run garbage
    collection using gc method of Runtime class.
   
    */

    package org.best.example;

    public class RunGarbageCollector {
   
    public static void main(String args[])
    {
    /*
    * get current Java Runtime using getRuntime()
    * method of Runtime class.
    */
    Runtime runtime = Runtime.getRuntime();
   
    /*
    * To suggest the JVM to run garbage collector, use
    *
    * void gc()
    * method of Runtime class.
    *
    */
   
    runtime.gc();
    System.out.println("JVM has made best effort to garbage collect!");
    }
   
    }
   
    /*
    Output of this program would be
    JVM has made best effort to garbage collect!
    */

No comments: