Java Garbage Collection


In languages like C/C++, it is the duty of the programmer to create and destroy objects. But if the programmer does not perform their duty, sufficient memory may not be available for the creation of a new object, and the program may terminate causing OutOfMemoryErrors.

Java relieves the programmer from memory management task and itself reclaims the memory occupied by the objects which are no longer in use.

Garbage Collection in Java is carried out by a daemon thread called Garbage Collector. Java Virtual Machine (JVM) invokes it when there is a lack of memory (heap) for new objects.

When an object becomes eligible for Garbage Collection?

  • An object becomes eligible for Garbage Collection if it is not reachable from any live threads or any static references.
  • An object becomes eligible for Garbage Collection if it’s all references are null.
Integer n = new Integer(); n = null; //the Integer object is no longer accessible
  • Cyclic dependencies are not counted as a reference so if Object X has a reference of Object Y and Object Y has a reference of Object X and they don’t have any other live reference then both Objects X and Y will be eligible for Garbage Collection.
  • An object is also eligible for garbage collection when its reference variable is re-assigned and that object no longer has any other reference to it. Look at the code snippet below:
Integer i1 = new Integer(10); //new Integer object created Integer i2 = new Integer(20); //Another new Integer object created i1 = i2;//i1 now references to i2 (re-assigned), therefore 10 (object in the heap) becomes eligible for garbage collection since it no longer has any other reference to it

How to manually make an object eligible for Garbage Collection?

  • Even though it is not the task of the programmer to destroy the objects, it is a good programming practice to make an object unreachable(thus eligible for GC) after it is used.
  • There are generally four different ways to make an object eligible for garbage collection.
  1. Nullifying the reference variable
  2. Re-assigning the reference variable
  3. An object is created inside a block and reference goes out of scope once control exit that block.
  4. Island of Isolation

Ways of requesting JVM to run Garbage Collector

  • Though making an object eligible for Garbage Collection, it depends on sole discretion of JVM to run the Garbage Collector to destroy it.
  • We can also request JVM to run Garbage Collector. There are two ways to do it :
  1. Using System.gc() method: System class contain static method gc() for requesting JVM to run the Garbage Collector.
  2. Using Runtime.getRuntime().gc() method: Runtime class allows the application to interface with the JVM in which the application is running. Hence by using its gc() method, we can request JVM to run the Garbage Collector.
// Java program to request // JVM to run Garbage Collector public class Test { public static void main(String[] args) throws InterruptedException { Test test1 = new Test(); Test test2 = new Test(); // Nullifying the reference variable test1 = null; // requesting JVM for running Garbage Collector System.gc(); // Nullifying the reference variable test2 = null; // requesting JVM for running Garbage Collector Runtime.getRuntime().gc(); } @Override // finalize method is a method which is called on an object once // before garbage collector is destroying it and reclaiming its memory protected void finalize() throws Throwable { System.out.println("Garbage collector is called"); System.out.println("Object garbage collected : " + this); } }
Garbage collector called Object garbage collected : [email protected]46d08f12 Garbage collector called Object garbage collected : [email protected]481779b8

Note :

  1. There is no guarantee that any one of above two methods will definitely run Garbage Collector.
  2. The call System.gc() is effectively equivalent to the call : Runtime.getRuntime().gc()

Object Finalization

  • Objects have resources associated with them. It is their responsibility to free the resources.
  • The finalize(), is declared in Object class and is called by garbage collector once, just before destroying the object. An object can take any last action using this method just before its area is reclaimed by the garbage collector.
  • finalize() method is present in Object class with the following prototype.
protected void finalize() throws Throwable

NOTE1 :

  1. The finalize() method called by Garbage Collector, not JVM. Although Garbage Collector is one of the modules of JVM.
  2. Object class finalize() method has an empty implementation, thus it is recommended to override finalize() method to dispose of system resources or to perform other cleanups.
  3. The finalize() method is never invoked more than once for any given object.
  4. If an uncaught exception is thrown by the finalize() method, the exception is ignored and finalization of that object terminates.

SOURCES

  1. geeksforgeeks.Accessed: October 24,2017.

This article needs improvement. You can help improve this article. You can also write similar articles and help the community.