3

There are four type of references in Java: Strong, Soft, Weak, and phantom. Kindly elaborate where and how Java exactly use phantom reference.

EDIT :

I did read the javaDoc, as assylias posted below. the section says an object is phantom rechable if it has been finalized and some phantom refernce refers to it. I dint quite get that statement.

What is the use of phantom and in which cases shall we use it?

Hash
  • 4,647
  • 5
  • 21
  • 39
Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
  • 7
    Welcome to Stack Overflow! We encourage you to [research your questions](http://stackoverflow.com/questions/how-to-ask). If you've [tried something already](http://whathaveyoutried.com/), please add it to the question - if not, research and attempt your question first, and then come back. –  Oct 01 '12 at 11:58
  • I typed that word on google, and it gave me this link: - [Documentation](http://docs.oracle.com/javase/7/docs/api/java/lang/ref/PhantomReference.html) - MORAL -- Be friend with google.. – Rohit Jain Oct 01 '12 at 11:59
  • 1
    see http://stackoverflow.com/questions/80690/phantom-referenced-objects – CAMOBAP Oct 01 '12 at 11:59
  • 1
    @Raedwald, i dont want anyone to do my homework for me. I was seeking some help. I did read the javaDoc, as assylias posted below. the section says an object is phantom rechable if it has been finalized, and some phantom refernce refers to it. I dint quite get that statement. I should have added that in my description and also i dint get the use of phantom , why it is used. that is why i asked where it is used in my question – Mukul Goel Oct 01 '12 at 12:22
  • @RohitJain I did read that documentation, but dint quite get the concept and usage of phantom. that is why the question. I should have added all that description in my question – Mukul Goel Oct 01 '12 at 12:24
  • 2
    @Tichodroma Yes, I did read through java doc, I`ll take care, that if i ask question, I do add my research background so people don't take it as if I am asking them to do my homework – Mukul Goel Oct 01 '12 at 12:26
  • 2
    @MukulGoel.. Its no problem.. But for future, while asking question, remember to tell what you already know, what you have already done.. This will prevent us from trying the same thing again.. – Rohit Jain Oct 01 '12 at 12:27
  • possible duplicate of [Understanding Java's Reference classes: SoftReference, WeakReference, and PhantomReference](http://stackoverflow.com/questions/3329691/understanding-javas-reference-classes-softreference-weakreference-and-phanto) – Louis Wasserman Oct 01 '12 at 17:59

1 Answers1

12

How each type of reference works is described in the java.lang.ref package javadoc:

Going from strongest to weakest, the different levels of reachability reflect the life cycle of an object. They are operationally defined as follows:

  • An object is strongly reachable if it can be reached by some thread without traversing any reference objects. A newly-created object is strongly reachable by the thread that created it.
  • An object is softly reachable if it is not strongly reachable but can be reached by traversing a soft reference.
  • An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing a weak reference. When the weak references to a weakly-reachable object are cleared, the object becomes eligible for finalization.
  • An object is phantom reachable if it is neither strongly, softly, nor weakly reachable, it has been finalized, and some phantom reference refers to it.
  • Finally, an object is unreachable, and therefore eligible for reclamation, when it is not reachable in any of the above ways.

There are a few posts on Stack OVerflow that give examples of how and when you might need a PhantomReference:

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • 2
    Thanks, but i did read that from javadoc. understood the strong,soft and weak. but dint quite get phantom. what exactly it is and where should I be using it. – Mukul Goel Oct 01 '12 at 12:27
  • @MukulGoel Ok that was not clear in your original question. This post answers your question: http://stackoverflow.com/questions/9826741/java-phantom-reference-when-we-using-it - short answer: it has little practical use and 99.9% of developers won't ever use that feature. – assylias Oct 01 '12 at 12:29
  • Yea, the original question was little too generic. Thanks. That link says what i was looking for. – Mukul Goel Oct 01 '12 at 12:31
  • @MukulGoel I have edited my post with a second link. – assylias Oct 01 '12 at 12:31
  • How do phantom references make sense then? Except perhaps for debugging the JVM how it is useful to delay collecting an object until the phantom reference is gone? Isn't this increasing likelyhood of an OOM. Also it seems there is no way to tell if particular object has been actually collected. – akostadinov Mar 07 '13 at 15:03
  • @akostadinov I have personally never used them - the 2 posts at the bottom of my answer give a few use cases. – assylias Mar 07 '13 at 15:19