Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is Weak Reference

 
Senthil Kumar Sekar
Ranch Hand
Posts: 45
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi All,
I was going through many links on Weak Reference,Weak HashMap.
Still iam not clear.So, I have chosen this 2 way communication.
Can anybody explain me?

Thanks.

Regards
Senthil Kumar Sekar
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several kinds of reference, the commonest being strong. This means that an object is always “reachable” until the reference is broken, and that object cannot be deleted by the garbage collector.
There are at least two other kinds (weak and phantom). What they mean is that the object can be garbage collected, provided it has no strong references remaining. Have you found the package summary?
It appears that objects which cease to be strongly reachable are put into a queue to be deleted; when they are removed from the queue, their memory can be reclaimed because they are no longer reachable.

I am sure other people will know more about this than me.
 
Siva Vulchi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically Weak References are one way of controlling object's garbage collection.

When an object is only reachable through Weak reference (which means object is neither strongly reachable nor softly reachable), then it becomes eligible for GC. During the run of GC, this object will be marked and garbage collected.

In case of WeakHashMap, keys are referenced through Weak references. When a key is not being used or it is not strongly reachable, then in next run of GC, these keys will be garbage collected and hence mapping entry will be removed from the hashmap.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic