Garbage collection in Java

In java, Garbage means unreferenced objects.

Garbage Collection is process of reclaiming the runtime unused memory automatically. In other words, it is a way to destroy the unused objects.

In the Java programming language, dynamic allocation of objects is achieved using the new operator. An object once created uses some memory and the memory remains allocated till there are references for the use of the object.

When there are no references to an object, it is assumed to be no longer needed, and the memory, occupied by the object can be reclaimed. There is no explicit need to destroy an object as Java handles the de-allocation automatically.

The technique that accomplishes this is known as Garbage Collection. Those Programs that do not de-allocate memory can eventually crash when there is no memory left in the system to allocate. These programs are said to have memory leaks.

In C language, it is the programmer’s responsibility to de-allocate memory allocated dynamically using free() function. This is where Java memory management has an advantage.

When does java perform garbage collection?

There are many ways:

  • By nulling the reference
  • By assigning a reference to another
  • By anonymous object etc
  1. By nulling a reference:
    Department obj=new Department ();
    obj=null;
  2. By assigning a reference to another:
    Department obj1=new Department ();
    Department obj2=new Department ();
    obj1=obj2; //now the first object referred by obj1
    // is available for garbage collection
  3. By anonymous object:
    new Department ();

How to request JVM for garbage collection

Before we find out these ways, we must know what is finalize()

Finalization

finalize() method is present in Object class with following prototype. Syntax : protected void finalize() throws Throwable.

Just before destroying an object, Garbage Collector calls finalize() method on the object to perform cleanup activities. Once finalize() method completes, Garbage Collector destroys that object.

Once we made object eligible for garbage collection, it may not destroy immediately by garbage collector. Whenever JVM runs Garbage Collector program, then only object will be destroyed. But when JVM runs Garbage Collector, we can not expect.

We can also request JVM to run Garbage Collector.

There are two ways to do it :

  • Using System.gc() method : System class contain static method gc() for requesting JVM to run Garbage Collector.
  • 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 Garbage Collector.
// Java program to demonstrate requesting 
// JVM to run Garbage Collector
public class Test
{
    public static void main(String[] args) throws InterruptedException
    {
        Test t1 = new Test();
        Test t2 = new Test();
          
        // Nullifying the reference variable
        t1 = null;
          
        // requesting JVM for running Garbage Collector
        System.gc();
          
        // Nullifying the reference variable
        t2 = null;
          
        // requesting JVM for running Garbage Collector
        Runtime.getRuntime().gc();
      
    }
      
    @Override
    // finalize method is called on object once 
    // before garbage collecting it
    protected void finalize() throws Throwable
    {
        System.out.println("Garbage collector called");
        System.out.println("Object garbage collected : " + this);
    }
}
              

OUTPUT

Garbage collector called
Object garbage collected : Test@46d08f12
Garbage collector called
Object garbage collected : Test@481779b8
              

NoteObject class finalize() method has empty implementation, thus it is recommended to override finalize() method to dispose of system resources or to perform other cleanup.

Summary

In this article we learnt about Garbage Collection in Java. Finalization in garbage collection and how to request JVM to run Garbage Collection.
Hope you liked the article !


Leave a Comment