tags:

views:

296

answers:

3

The only thing i know about phanom reference is,

  • if you use its get() method it will always return null, and not the object. [ then whats the use of it ]
  • By using phantom reference you make it sure that the object cannot be resurrected from finalize method;

But what is the use of this concept/class? Have you ever used this in any of your project, or do you have any example where we should use this? Or is this concept made just for interview point of view ;)

+3  A: 

General diced-up table: http://mindprod.com/jgloss/phantom.html

Which of course coincides with http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ref/PhantomReference.html:
"""Phantom reference objects, which are enqueued after the collector determines that their referents may otherwise be reclaimed. Phantom references are most often used for scheduling pre-mortem cleanup actions in a more flexible way than is possible with the Java finalization mechanism."""

And last but not least, all the gory details (this is a good read): http://www.kdgregory.com/index.php?page=java.refobj

Happy coding. (But to answer the question, I've only ever used WeakReferences.)

pst
+8  A: 

I used Phantom references in a simplistic, very specialised kind of memory profiler to monitor object creation and destruction. I needed them to keep track of destruction. But the approach is out-dated. (It was written in 2004 targeting J2SE 1.4.) Professional profiling tools are much more powerful and reliable and the newer Java 5 features like JMX or agents and JVMTI can be used for that too.

PhantomReferences (allways used together with the Reference queue) are superior to finalize which has some problems and should therefore be avoided. Mainly making objects reachable again. This could be avoided with the finalizer guardian idiom (-> read more in 'Effective Java'). So they are also the new finalize.

Further more phantom references

allow you to determine exactly when an object was removed from memory. They are in fact the only way to determine that. This isn't generally that useful, but might come in handy in certain very specific circumstances like manipulating large images: if you know for sure that an image should be garbage collected, you can wait until it actually is before attempting to load the next image, and therefore make the dreaded OutOfMemoryError less likely. (Quoted from enicholas.)

And as psd wrote first, Roedy Green has a good summary of references.

Peter Kofler
A: 

It is common to use WeakReference where PhantomReference is more appropriate. This avoids certain problems of being able to resurrect objects after a WeakReference is cleared/enqueued by the garbage collector. Usually the difference doesn't matter because people are not playing silly buggers.

Using PhantomReference tends to be a bit more intrusive because you can't pretend that the get method works. You can't, for example, write a Phantom[Identity]HashMap.

Tom Hawtin - tackline
IdentityHashMap<PhantomReference> is actually one of the appropriate places for an IdentityHashMap. Note that *strong reference* kept to the PhantomReference, *but not the referent*.
pst
Or did I read that completely wrong? I apologize if I did.
pst