Explain Garbage collection mechanism in Java?

  1. Garbage collection is one of the most important features of Java. 
  2. The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. 
  3. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. 
  4. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects (value is null) from the memory. 
  5. Every class inherits finalize() method from java.lang.Object, the finalize() method is called by garbage collector when it determines no more references to the object exists. 
  6. In Java, it is good idea to explicitly assign null into a variable when no more in use. 
  7. In Java on calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected. Garbage collection is an automatic process and can’t be forced. 
  8. There is no guarantee that Garbage collection will start immediately upon request of System.gc().


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email