Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect when an object is no longer referenced

Is there a way to create register a handler that will be called exactly at the time when the last reference to a certain object is released?

An example would be an object that is backed by a physical data file and once the object become unreferenced, the file should be closed and than renamed. It would be nice if this was possible without having to explicitly call a "close" method on that object.

All the notification mechanisms I am aware of from the Weak/Phantom reference area only state that notification will occur at some point in time but there is no gurantee as to when this will happen...

like image 909
VoidPointer Avatar asked Oct 16 '08 13:10

VoidPointer


People also ask

What happens when an object is no longer referenced?

When there are no references to an object, the object is marked as garbage and is automatically removed from memory using Java's automatic garbage collection feature.

When object is no longer used which method is called?

The Java runtime environment deletes objects when it determines that they are no longer being used. This process is called garbage collection. An object is eligible for garbage collection when there are no more references to that object.

Can an object reference variable refer to no object?

A reference variable sometimes does and sometimes does not refer to an object, and can refer to different objects at different times. You need a way to say that a variable does not now refer to an object.

How do you dereference an object in Java?

Java Language Reference Data Types Dereferencing operator: Object obj = new Object(); String text = obj. toString(); // 'obj' is dereferenced. Dereferencing follows the memory address stored in a reference, to the place in memory where the actual object resides.


2 Answers

I think WeakReference does what you want. A WeakReference gets put into the ReferenceQueue as soon as its weakly reachable (i.e. all strong references are gone).

See this article by Ethan Nicholas.

If you are worried about some references not reaching the ReferenceQueue at shutdown, then keep a list of all objects created (using WeakReferences or PhantomReferences). Add a shutdown hook that checks the list for any outstanding references and perform whatever action you need.

like image 151
James Schek Avatar answered Nov 02 '22 23:11

James Schek


Problem is, "How do you implement this, without something holding a reference to the object?"

Even if you could get passed that problem, say with a service we'll call the HandleManager, the HandleManager would then have to create a new reference to the object, to pass to your handler. Then, your handler could either (a) store a reference to it, which would confuse the HandleManager which was expecting to destroy the unreferenced object; or (b) release the reference, which means that the final reference was once again released, which means the Handler has to be called again....

like image 37
James Curran Avatar answered Nov 02 '22 23:11

James Curran