• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reference API  RSS feed

 
Ashwin Sharma
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi....
I have read that if there is weak reference to the object, then it is collected by garbage collector but if there is phantom reference then the object has to be explicitly cleared(though it cannot be accessed). But what would be the behavior of garbage collector if the object has both phantom and weak reference both, whether garbage collector will collect the object and enqueue it in Reference Queue or just enqueue it in Reference Queue.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the JVM does the following steps: -

  • Waits until all strong references (ordinary Java references) and SoftReferences are cleared.
  • Clears all WeakReferences
  • Enqueues all WeakReferences that had ReferenceQueues
  • Object eligible for finalisation
  • Waits for finalisation to occur. You can get a strong reference to the object at this point and resurrect it, but don't do that!
  • Enqueues all PhantomReferences (but does not clear them). From this point, it is impossible to get a strong reference to the object.
  • Waits for all PhantomReferences to be explicitly cleared
  • Object eligible for garbage collection

  •  
    Ashwin Sharma
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for the reply
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!