File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes What is Weak Reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is Weak Reference " Watch "What is Weak Reference " New topic

What is Weak Reference

Senthil Kumar Sekar
Ranch Hand

Joined: Aug 08, 2010
Posts: 45

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?


Senthil Kumar Sekar
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
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

Joined: Nov 17, 2011
Posts: 30
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.
I agree. Here's the link:
subject: What is Weak Reference
It's not a secret anymore!