Is there any way that an application can know when an object created in its context is garbage collected.
I dont know if this is the right fourm to post this question.
- Garbage Collection Question by Paul Strack on September 06 2003 12:41 EDT
- Garbage Collection Question by D S on September 08 2003 11:13 EDT
- fyi, Weak references, Soft references by Sean Sullivan on September 08 2003 16:32 EDT
You can use the finalize() method, which is (sometimes) invoked when the object is garbage collected. Unfortunately, this method is not guaranteed, so it not 100% accurate.
There is no guarantee that the Object eligible for Garbage collection will be Garbage collected. But finalize() method is guaranted to be invoked once on the object being Garbage collected.
You can read about garbage collection at http://www.java4all.info/articles/corejava/garbage-collection-java.html
I think you can do this using PhantomReferences and ReferenceQueues, although it's not something I've done myself. I vaguely remember reading an article about it some time ago.
Weak references and soft references:
A program may request to be notified of changes in an object's reachability by registering an appropriate reference object with a reference queue at the time the reference object is created. Some time after the garbage collector determines that the reachability of the referent has changed to the value corresponding to the type of the reference, it will add the reference to the associated queue.
Thanks a lot for all your replies. They were helpful, I'll do some experimentation based on the suggestions, and keep the group posted.