File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Mock Exam Errata and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic

Garbage Collection

Malini Santhanam

Joined: Jul 20, 2004
Posts: 4
Can anybody let me know when an object is eligible for garbage collection with some examples?

Thanks in advance,
Divij Mahajan

Joined: Jul 26, 2004
Posts: 8
Hi Malini,

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.
I agree. Here's the link:
subject: Garbage Collection
It's not a secret anymore!