Is it good practice to call System.gc() in Java?



In this tutorial we will learn is it good practice to call System.gc() in Java?

Contents of page >
  • First let’s know what System.gc() method can do in java >
  • Is it good practice to call System.gc() in Java?
  • But, why we must not call System.gc() in java?
  • How System.gc() works in java?
  • System.gc() method can be called using any of the below >
  • Which method does System.gc() method calls internally in java?
  • Program/Example to call System.gc() method in Java >


How garbage collection works internally in java | Mark and sweep algorithm




First let’s know what System.gc() method can do in java >

We can force early garbage collection in java by calling  java.lang.System.gc();
         
By calling System.gc method JVM runs the finalize() methods of any objects pending finalization i.e. objects which have been discarded but there finalize method is yet to be run. After finalize method is executed JVM reclaims space from all the discarded objects.

Is it good practice to call System.gc() in Java?
Answer in short is No, but let’s learn why we must not call System.gc() in java.

But, why we must not call System.gc() in java?
  • First of all calling System.gc() does not guarantee that it will immediately start performing garbage collection.
  • Even calling System.gc() may not do anything. Call to perform garbage collection may be ignored completely.
  • JVM is different for different platforms because java is platform independent language, so you never know about which garbage collector your jvm will run i.e. what algorithm does it follow to perform garbage collection.


How System.gc() works in java?
When any of System.gc() methods is called daemon threads which are low priority threads which runs intermittently in background are given high priority to perform garbage collection.


System.gc() method can be called using any of the below >

       System.gc();
       Runtime.getRuntime().gc();
         
       System.runFinalization();
       Runtime.getRuntime().runFinalization();


Which method does System.gc() method calls internally in java?

System.gc() internally calls Runtime.getRuntime().gc()
and
System.runFinalization() internally calls Runtime.getRuntime().runFinalization()

Runtime class is also found in java.lang package.


Program/Example to call System.gc() method in Java >

public class RunGarbageCollector {
   public static void main(String[] args) {
          System.out.println("About to call garbage collection - using System.gc() method");
          /*
          * By calling these methods JVM runs the finalize() methods of
          * any objects pending finalization i.e. objects which have been
          * discarded but there finalize method is yet to be run.
          * After finalize method is executed JVM reclaims space
          * from all the discarded objects.
                
                 Note : Calling these methods does not guarantee that it
                 will immediately start performing garbage collection.
          */
          System.gc();
         
          System.out.println("garbage collection - using System.gc() method called");
   }
}
/*OUTPUT
About to call garbage collection - using System.gc() method
garbage collection - using System.gc() method called
*/



Summary -
So in this tutorial we will learned is it good practice to call System.gc() in Java, about what System.gc() method can do in java, why we must not call System.gc() in java? How System.gc() works in java? Which method does System.gc() method calls internally in java? Program/Example to call System.gc() method in Java.



Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.




RELATED LINKS>

finalize method in java - 13 salient features, force jvm to run garbage collection

How garbage collection works internally in java | Mark and sweep algorithm



 

Different type of garbage collectors in java>

>Serial collector / Serial GC (Garbage collector) in java

>Throughput GC (Garbage collector) or Parallel collector in java

>Concurrent Mark Sweep (CMS) collector / concurrent low pause garbage collector in java

>G1 garbage collector / Garbage first collector in java


JVM in detail - Garbage collection & heap structure >

>JVM Heap memory (Hotspot heap structure) with diagram in java


>What are Minor, Major and Full garbage collection in JVM in java



Important VM parameters >

>Most important and frequently used VM (JVM) PARAMETERS with examples in JVM Heap memory in java


Monitor, analyze garbage collection and fix MEMORY LEAK >

>How to monitor and analyze the garbage collection in 10 ways in java

>Detecting and fixing memory leak in java


eEdit
Must read for you :