Link Queues
As soon as the WeakReference function starts returning null, the object it pointed to becomes garbage, and the WeakReference object is practically useless. This usually means that some kind of cleaning is required; For example, WeakHashMap must delete such non-existent entries in order to avoid holding onto the ever-increasing number of dead WeakReferences.
The ReferenceQueue class makes tracking dead links easier. If you pass the ReferenceQueue to a weak link builder, the link object will automatically be inserted into the link queue when the object it points to becomes garbage. You can then process the ReferenceQueue at some regular interval and do any cleanup for dead links.
See this page for usage guide.
Could you please indicate why you are using this? Very few valid uses.
i.e. cache is invalid (or at least not good)
Edit:
This code is equivalent to using weakHashMap, but you need to explicitly do this to adjust the queue with the map.
HashMap aHashMap = new HashMap(); ReferenceQueue Queue = new ReferenceQueue(); MyWeakReference RefKey = new MyWeakReference(key, Queue); aHashMap.put(RefKey, value);
source share