Garbage Collection with finalize() method in Java with Examples (All scenerios)

An object is eligible to be garbage collected if its reference variable is lost from the program during execution.Sometimes they are also called unreachable objects.

What is reference of an object?

The new operator dynamically allocates memory for an object and returns a reference to it. This reference is the address in memory of the object allocated by new. A reference is an address that indicates where an object’s variables, methods etc. are stored.

The objects are not actually used when assigned to a variable or passed as an argument to a method . The references to objects are used everywhere. Example

Emp emp =  new Emp();   //referencing to object

In java, the memory allocated at runtime i.e. heap area can be made free by the process of garbage collection. It is nothing but just a method of making the memory free which is not being used by the programmer. Only the objects who have no longer reference to them are eligible for garbage collection in java.

Please note that the object can not become a candidate for garbage collection until all references to it are discarded.

Scenerio 1st:

Object created inside a method:

When a method is called it goes inside the stack frame. When the method is popped from the stack, all its members dies and if some objects were created inside it then these objects becomes unreachable or anonymous after method execution and thus becomes eligible for garbage collection

Lets understand it with Example:

/* Java program to demonstrate that  objects created inside a method will becomes
eligible for gc after method execution terminate */
 
class Test
{
     
    // to store object name
    String obj_name;
     
    public Test(String obj_name) 
    {
        this.obj_name = obj_name;
    }
     
    static void show()
    {
        //object t1 inside method becomes unreachable when show() removed
        Test t1 = new Test("t1"); 
        display();
         
    }
    static void display()
    {
        //object t2 inside method becomes unreachable when display() removed
        Test t2 = new Test("t2"); 
    }
     
    // Driver method
    public static void main(String args[])
    {
        // calling show()
        show();
         
        // calling garbage collector
        System.gc();
    }
     
    @Override
    /* Overriding finalize method to check which object
    is garbage collected */
    protected void finalize() throws Throwable 
    {
        // will print name of object
        System.out.println(this.obj_name + " successfully garbage collected");
    }
}

Output:

t2 successfully garbage collectedt1 successfully garbage collected

Note : If a method returns the object created inside it and we store this object reference by using a reference-type variable than it is no longer eligible for garbage collection.

Scenerio 2nd:

Reassigning the reference variable:

When reference id of one object is referenced to reference id of some other object then the previous object has no any longer reference to it and becomes unreachable and thus becomes eligible for garbage collection

Let take it by Example:

 

 

/* Java program to demonstrate gc
 when one object referred to other object */
 
class Test
{
    // to store object name
    String obj_name;
     
    public Test(String obj_name) 
    {
        this.obj_name = obj_name;
    }
     
    // Driver method
    public static void main(String args[])
    {
        Test t1 = new Test("t1");
        Test t2 = new Test("t2");
         
        //t1 now referred to t2
        t1 = t2;
         
        // calling garbage collector
        System.gc();
    }
     
    @Override
    /* Overriding finalize method to check which object
     is garbage collected */
    protected void finalize() throws Throwable 
    {
        // will print name of object
        System.out.println(this.obj_name + " successfully garbage collected");
    }
}

Output:

t1 successfully garbage collected

Scenerio 3rd:

Nullifying the reference variable :

When all the reference variables of an object are changed to NULL, it becomes unreachable and thus becomes eligible for garbage collection.

/* Java program to demonstrate gc
 when object reference changed to NULL */
 
class Test
{
    // to store object name
    String obj_name;
     
    public Test(String obj_name) 
    {
        this.obj_name = obj_name;
    }
     
    // Driver method
    public static void main(String args[])
    {
        Test t1 = new Test("t1");
          
        /* t1 being used for some purpose in program */
  
        /* When there is no more use of t1, make the object
           referred by t1 eligible for garbage collection */
        t1 = null;
  
        // calling garbage collector
        System.gc();
    }
     
    @Override
    /* Overriding finalize method to check which object
     is garbage collected */
    protected void finalize() throws Throwable 
    {
        // will print name of object
        System.out.println(this.obj_name + " successfully garbage collected");
    }
}

Output:

t1 successfully garbage collected

4th Scenerio:

Anonymous object : The reference id of an anonymous object is not stored anywhere. Hence, it becomes unreachable.

Example:

/* Java program to demonstrate gc
 of anonymous objects */
 
class Test
{
    // to store object name
    String obj_name;
     
    public Test(String obj_name) 
    {
        this.obj_name = obj_name;
    }
     
    // Driver method
    public static void main(String args[])
    {
        //anonymous object without reference id
        new Test("t1"); 
  
        // calling garbage collector
        System.gc();
    }
     
    @Override
    /* Overriding finalize method to check which object
     is garbage collected */
    protected void finalize() throws Throwable 
    {
        // will print name of object
        System.out.println(this.obj_name + " successfully garbage collected");
    }
}

Output:

t1 successfully garbage collected

Popular posts from this blog

How to change column name in Dataframe and selection of few columns in Dataframe using Pyspark with example

What is Garbage collection in Spark and its impact and resolution

Window function in PySpark with Joins example using 2 Dataframes (inner join)