A garbage collector is a program that ensures automatic memory management and runs periodically to recycle unreachable objects. It is responsible for reclaiming already occupied memory referenced by unreachable objects, the process is called garbage collection.
When an object doesn’t have any references, it becomes automatically eligible for garbage collection.
A reference object gets memory at runtime which occupies space in heap area. If Java finds it as unreachable or unused, then it is considered as an dead which becomes eligible for garbage collection.
However, there are four ways to make an object qualified for the garbage collection.
[Also read: How to Force Garbage Collection in Java With Program Example]
4 ways to make an object eligible for garbage collection
Nullifying the reference variable
A reference variable always points to an address of the actual copy of an object. If this particular address is replaced by null value, the object can no longer be accessed through that variable.
Therefore, assigning null value would break the link between the object and reference variable, making it eligible for garbage collection.
public class Gcrun { Gcrun x; @Override protected void finalize () throws Throwable { System.out.println ("I am releasing resources !"); } public static void main (String[]args) { Gcrun obj = new Gcrun (); // object is created obj = null; System.gc (); } }
Reassign the reference variable
In this, if an object is not required and at the same time there is need to work on another object, we simply shift reference of the variable from useless object to a new object.
As a result, the old one becomes eligible for garbage collection.
public class Gcrun { Gcrun x; @Override protected void finalize () throws Throwable { System.out.println ("I am releasing resources !"); } public static void main (String[]args) { Gcrun obj = new Gcrun (); // object is created Gcrun obj1 = new Gcrun (); // object is created obj = obj1; System.gc (); } }
Object created in a method
When an object is created in a local method, it acts like a local variable. And when that particular method finishes its execution, it goes out of scope, the reference variable pointing to the object automatically gets destroyed, but not the actual object.
However, if a method returns an object and its reference is assigned to a variable, then the object would no longer be eligible for garbage collection.
As a result, the reference to the object breaks and the object becomes unreachable.
public class Gcrun { Gcrun x; @Override protected void finalize () throws Throwable { System.out.println ("I am releasing resources !"); } public static void main () { Gcrun obj = new Gcrun (); // object is created Gcrun obj1 = new Gcrun (); // object is created } public static void main (String[]args) { main (); System.gc (); } }
Island of isolation
When there is a group of objects that are internally referencing to each other and if no root object reference to them, then these group of objects collectively eligible for garbage collection.
Program:
public class Gcrun { Gcrun x; @Override protected void finalize () throws Throwable { System.out.println ("I am releasing resources !"); } public static void main (String[]args) { Gcrun obj = new Gcrun (); // object is created Gcrun obj1 = new Gcrun (); // object is created obj1.x = obj; obj.x = obj1; obj = null; // now object is unreachable obj1 = null; // now object is unreachable System.gc (); } }