Showing posts with label Garbage Collection. Show all posts
Showing posts with label Garbage Collection. Show all posts

Thursday, 4 February 2016

Java - Difference between System.gc() and Runtime.getRuntime().gc()

public static void main(String[] args) {
    // Some code here
    // Invoke the Garbage Collector here.
     System.gc();

    // Some more code here
    // Invoke the Garbage Collector again.
     Runtime.getRuntime().gc();
}
 
In the above code, both calls seem to invoke the Garbage Collector. So what is the difference between System.gc() and Runtime.getRuntime().gc()?

Looking at the API documentation for System.gc() it invokes Runtime.getRuntime().gc()which means they are the same and System.gc() is only a convenience method.