How to make object eligible for garbage collection in Java?

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:
Box mybox = new Box(); //referencing to object
Role of an unreachable objects in java
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.
Ways to make an object eligible for garbage collection:
Please note that the object can not become a candidate for garbage collection until all references to it are discarded.
- 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
.Example:/* Java program to demonstrate thatobjects created inside a method will becomeseligible for gc after method execution terminate */classTest{// to store object nameString obj_name;publicTest(String obj_name){this.obj_name = obj_name;}staticvoidshow(){//object t1 inside method becomes unreachable when show() removedTest t1 =newTest("t1");display();}staticvoiddisplay(){//object t2 inside method becomes unreachable when display() removedTest t2 =newTest("t2");}// Driver methodpublicstaticvoidmain(String args[]){// calling show()show();// calling garbage collectorSystem.gc();}@Override/* Overriding finalize method to check which objectis garbage collected */protectedvoidfinalize()throwsThrowable{// will print name of objectSystem.out.println(this.obj_name +" successfully garbage collected");}}Output:
t2 successfully garbage collected t1 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.
- 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.Example:
/* Java program to demonstrate gcwhen one object referred to other object */classTest{// to store object nameString obj_name;publicTest(String obj_name){this.obj_name = obj_name;}// Driver methodpublicstaticvoidmain(String args[]){Test t1 =newTest("t1");Test t2 =newTest("t2");//t1 now referred to t2t1 = t2;// calling garbage collectorSystem.gc();}@Override/* Overriding finalize method to check which objectis garbage collected */protectedvoidfinalize()throwsThrowable{// will print name of objectSystem.out.println(this.obj_name +" successfully garbage collected");}}Output:
t1 successfully garbage collected
- 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.Example:
/* Java program to demonstrate gcwhen object reference changed to NULL */classTest{// to store object nameString obj_name;publicTest(String obj_name){this.obj_name = obj_name;}// Driver methodpublicstaticvoidmain(String args[]){Test t1 =newTest("t1");/* t1 being used for some purpose in program *//* When there is no more use of t1, make the objectreferred by t1 eligible for garbage collection */t1 =null;// calling garbage collectorSystem.gc();}@Override/* Overriding finalize method to check which objectis garbage collected */protectedvoidfinalize()throwsThrowable{// will print name of objectSystem.out.println(this.obj_name +" successfully garbage collected");}}Output:
t1 successfully garbage collected
- Anonymous object : The reference id of an anonymous object is not stored anywhere. Hence, it becomes unreachable.
Example:/* Java program to demonstrate gcof anonymous objects */classTest{// to store object nameString obj_name;publicTest(String obj_name){this.obj_name = obj_name;}// Driver methodpublicstaticvoidmain(String args[]){//anonymous object without reference idnewTest("t1");// calling garbage collectorSystem.gc();}@Override/* Overriding finalize method to check which objectis garbage collected */protectedvoidfinalize()throwsThrowable{// will print name of objectSystem.out.println(this.obj_name +" successfully garbage collected");}}Output:
t1 successfully garbage collected
Related Article: Island of Isolation
This article is contributed by Apoorva Singh and Gaurav Miglani. If you like Lazyroar and would like to contribute, you can also write an article using contribute.zambiatek.com or mail your article to contribute@zambiatek.com. See your article appearing on the Lazyroar main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



