• Post Reply Bookmark Topic Watch Topic
  • New Topic

Phantom reference  RSS feed

 
Manoj Kumkumath
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,
Have any one of you used Phantom reference in your real time projects? If yes, could you please explain me the requirement which caused to use the Phantom reference.

I am trying to find out when it is best to use a Phantom reference?

-Manoj
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I saw this post a couple of days ago, but I thought I'd wait to see if someone who has actually used PhantomReference would give a definitive answer. They haven't, so here's mine.

PhantomReference is for doing clean-up just before an object is garbage collected. It is similar in purpose to finalisation, but less broken.

I think that a plain PhantomReference is fairly useless. That is because you cannot get its referent (the object being referred-to). The usual way to use it, therefore, would be to create a subclass of PhantomReference, containing the data necessary to perform the clean-up. This should not include the referent.

When you instantiate your subclass of PhantomReference, you need to enqueue it. Then you need a thread to process the queue. Each time you take an item off the queue, you cast it to your subclass, then do whatever clean-up operation(s) your subclass provides.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter, enqueue is not the correct term. He need to add the phantom reference to a reference queue. Not the same as enqueuing, which is done by the GC.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mr. C Lamont Gilbert:
Peter, enqueue is not the correct term. He need to add the phantom reference to a reference queue. Not the same as enqueuing, which is done by the GC.


Adding something to a queue is "enqueuing" it, so I think my use of the word is not wrong. However, I accept that, in the context of a discussion about garbage collection, it is not a good choice of word, because "enqueuing" has a special meaning in this context and using it for something else is confusing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!