- Garbage collection is a mechanism provided by JVM used to reclaim heap space from objects which are eligible for Garbage collection.
- In java, Garbage collection is carried by daemon thread [Background thread] called Garbage Collector.
- Java supports System.gc() and Runtime.gc() to send Garbage collection request to JVM.
- The eligible object is removed from memory after invoking the finalize() method by GarbageCollection thread. It performs some cleanup operation on the same object,
How JVM will decide the object is eligible for Garbage collection?
- If object reference is set null externally e.g. object = null
- 2 If object is created inside a block/ scope. Once execution thread comes out from the scope, the object reference could not be reachable.
eg:
public boolean getStatus()
{
boolean status=false;
Test t=new Test();
// implementation code
return status;
}
- 3 Parent object set to null, if an object holds reference of another object and when you set container object's reference null, child or contained object automatically becomes eligible for garbage collection.
0 comments :
Post a Comment