Can anybody let me know when an object is eligible for garbage collection with some examples?
Thanks in advance, Malini
posted 11 years ago
An object is said to be ready for garbage collection whenever it is not holding any references. The JVM may then execute the Garabge Collector "at will" and remove such unreferenced objects from memory.
Another scenario is when a gc may need to free up memory for other objects. At this time it may remove objects in a given order (depends on JVM), say in order of last usage time or if the object is holding weak references.
Finally, a developer may want to call gc at a specific time and thats where one uses calls to methods of the GC or System class explicitly.(ref api)
-- A coding best practice related to GC is to call the finalize method, wherein references held by objects, that are no longer in use can be released. Finalise method is executed just before gc completes execution. But beware that this method may not be called everytime so try using alternatives.
Lemme know if you need more details.
-Divij<br />---------------------------------<br />Weinberg's Law: If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization.