How To Make Object Eligible For Garbage Collector In Java

Posted By : Nanda Ram | 22-Sep-2017

 


In the old language like c++, creation and destruction of the object is a responsibility of programmer only. Usually, programmer taking very much care while creating objects and his/her neglecting destruction of useless objects due to this neglecting at the certain point of time for the creation of new object sufficient memory may not be available and the entire program will be collapse due to the memory problem. But in java, a programmer is responsible only for the creation of objects and he is not responsible for the destruction of useless objects.

 

 

Some people provided one assistant which is always running in the background for the destruction of useless objects. Due to this assistant the chance of failure java program with memory problems very rare. This assistant is nothing but Garbage Collector. Hence, the main objective of the garbage collector is to destroy useless objects.

 

 

The Various ways to make object eligible for Garbage Collector

Although programmer is not answerable to destroy meaningless objects, it is ever a better programming habit to make an object eligible for garbage collector if it is no continue needed. An object is said to be eligible for the garbage collector if it doesn't contain any reference. The following are the various possible ways to make an object eligible for garbage collector

 

 

1. Nullifying the reference variable
If an object is no longer required then assign null to all its references, the automatically that object eligible for a garbage collector.

 
 
Exam. 
       Student s1 = new Student();     // This object is not eligible for garbage collector
       Student s2 = new Student();        // This object is also not eligible for garbage collector
       s1 =null;                                      // This object is  eligible for garbage collector
 
 
 
 
 2. Island of Isolation
 Exam.
 
      class Test {
       Test i;
 public static void main(String[] args )
 {
 Test t1 = new Test();
 Test t2 = new Test();
 Test t3 = new Test();
 t1.i=t2;         // This object is not eligible for garbage collector
 t2.i=t3;        // This object is also not eligible for garbage collector
 t3.i=t1;        // This object is also not eligible for garbage collector
 t1=null;      // This object is  eligible for garbage collector
 t2= null;     // This object is  eligible for garbage collector
 t3=null;      // This object is  eligible for garbage collector
 
 
 
Note
If an object doesn't have any reference then it is always eligible for garbage collector.
Even though object having the reference still it is eligible for G.C (Garbage Collector) sometimes (island of isolation)
  

About Author

Author Image
Nanda Ram

Nanda Ram is a Java Developer, he keep conscientious about his task to complete it in a effective and efficient manner .

Request for Proposal

Name is required

Comment is required

Sending message..