Signup/Sign In

Garbage Collection in Java

Java garbage collection is the process of releasing unused memory occupied by unused objects. This process is done by the JVM automatically because it is essential for memory management.

When a Java programs run on the JVM, objects are created on the heap, which is a portion of memory dedicated to the program. Eventually, some objects will no longer be needed.

When there is no reference to an object, then that object is assumed to be no longer needed and the memory occupied by the object are released. This technique is called Garbage Collection.

How Garbage Collection Works?

The garbage collection is a part of the JVM and is an automatic process done by JVM. We do not need to explicitly mark objects to be deleted. However, we can request to the JVM for garbage collection of an object but ultimately it depends on the JVM to call garbage collector.

Unlike C++ there is no explicit way to destroy object.

In the below image, you can understand that if an object does not have any reference than it will be dumped by the JVM.

garbage collection in java


Can the Garbage Collection be forced explicitly ?

No, the Garbage Collection can not be forced explicitly. We may request JVM for garbage collection by calling System.gc() method. But This does not guarantee that JVM will perform the garbage collection.


Advantages of Garbage Collection

  1. Programmer doesn't need to worry about dereferencing an object.
  2. It is done automatically by JVM.
  3. Increases memory efficiency and decreases the chances for memory leak.

An object is able to get garbage collect if it is non-reference. We can make an object non-reference by using three ways.

1. set null to object reference which makes it able for garbage collection. For example:


Demo demo = new Demo();
demo = null; // ready for garbage collection

2. We can non-reference an object by setting new reference to it which makes it able for garbage collection. For example

    
Demo demo = new Demo();
Demo demo2 = new Demo();
demo2 = demo // referring object
    

3. Anonymous object does not have any reference so if it is not in use, it is ready for the garbage collection.


finalize() method

Sometime an object will need to perform some specific task before it is destroyed such as closing an open connection or releasing any resources held. To handle such situation finalize() method is used.

The finalize() method is called by garbage collection thread before collecting object. Its the last chance for any object to perform cleanup utility.

Signature of finalize() method

protected void finalize()
{
 //finalize-code
}


Some Important Points to Remember

  1. It is defined in java.lang.Object class, therefore it is available to all the classes.
  2. It is declare as proctected inside Object class.
  3. It gets called only once by a Daemon thread named GC (Garbage Collector) thread.

Request for Garbage Collection

We can request to JVM for garbage collection however, it is upto the JVM when to start the garbage collector.

Java gc() method is used to call garbage collector explicitly. However gc() method does not guarantee that JVM will perform the garbage collection. It only request the JVM for garbage collection. This method is present in System and Runtime class.

Example of gc() Method

Let's take an example and understand the functioning of the gc() method.

public class Test
{

    public static void main(String[] args)
    {
        Test t = new Test();
        t=null;
        System.gc();
    }
    public void finalize()
    {
        System.out.println("Garbage Collected");
    }
}

Garbage Collected