Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How an object becomes eligible for Garbage Collections?

user-image
Question added by Ajith Fernandez , Software Engineer , Nayasoft Solutions Pvt.ltd
Date Posted: 2013/12/03
Mohd shahnawaz khan
by Mohd shahnawaz khan , Associate Project , Cognizant Technology Solution

An object becomes eligible for Garbage collection or GC if its not reachable from any live threads or any static references in other words you can say that an object becomes eligible for garbage collection if its all references are null. Cyclic dependencies are not counted as reference so if Object A has reference of object B and object B has reference of Object A and they don't have any other live reference then both Objects A and B will be eligible for Garbage collection. Generally an object becomes eligible for garbage collection in Java on following cases: 1) All references of that object explicitly set to null e.g. object = null 2) Object is created inside a block and reference goes out scope once control exit that block. 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.

Huzefa chikhly
by Huzefa chikhly , JAVA DEVELOPER , PRIMALINK SOLUTIONS INDIA PVT LTD

object becomes eligible for Garbage Collections when no any refrence point to that object. but in cyclic dependency where object A point object B and object B point object A it lead to memory leak and non of the object get garbage collected so this type of code is not recommended and its bad code. may be im wrong and mr Mohd shahnawaz is right but this is what i learn during my java training.

Deleted user
by Deleted user

An object becomes eligible for garbage collection when the reference of that particular object is set to null, implicitly or explicitly, even though reference is set to null garbage collector will call "finalize()"  method on that object which is eligible for garbage collection, we can't say immediatly that object is garbage collected((destroyed) which is nothing but destruction phase).i hope u get my Point...

More Questions Like This