• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Phantom reference

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic