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:
class Test
{
String obj_name;
public Test(String obj_name)
{
this .obj_name = obj_name;
}
static void show()
{
Test t1 = new Test( "t1" );
display();
}
static void display()
{
Test t2 = new Test( "t2" );
}
public static void main(String args[])
{
show();
System.gc();
}
@Override
protected void finalize() throws Throwable
{
System.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:
class Test
{
String obj_name;
public Test(String obj_name)
{
this .obj_name = obj_name;
}
public static void main(String args[])
{
Test t1 = new Test( "t1" );
Test t2 = new Test( "t2" );
t1 = t2;
System.gc();
}
@Override
protected void finalize() throws Throwable
{
System.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:
class Test
{
String obj_name;
public Test(String obj_name)
{
this .obj_name = obj_name;
}
public static void main(String args[])
{
Test t1 = new Test( "t1" );
t1 = null ;
System.gc();
}
@Override
protected void finalize() throws Throwable
{
System.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:
class Test
{
String obj_name;
public Test(String obj_name)
{
this .obj_name = obj_name;
}
public static void main(String args[])
{
new Test( "t1" );
System.gc();
}
@Override
protected void finalize() throws Throwable
{
System.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 GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks 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.