Java Reference
In-Depth Information
You may not see any important use of a weak reference because its referent is reclaimed when the garbage
collector is run. Generally, weak references are not used to maintain caches. They are used to associate extra data with
an object. Suppose you have a person's details and his address. If you lose his details, you will not be interested in his
address. However, as long as the person's details are accessible, you want to keep his address information. This kind
of information can be stored using weak references and a Hashtable . A Hashtable stores objects in key-value pairs.
While adding a key-value pair to a Hashtable , you need to wrap the key object in a WeakReference object. The key and
value are not garbage collected when the key is accessible or in use. When the key object is no longer in use, it will
be garbage collected because it was wrapped inside a WeakReference . At that point, you can remove that entry from
the Hashtable , so the value object will also be eligible for the garbage collection. The following is a sample snippet of
code using Hashtable and WeakReference objects:
// Create a Hashtable object
Hashtable ht = new Hashtable();
// Create a reference queue, so we can check when a key was garbage collected
Referencequeue q = new ReferenceQueue();
// Create key and value objects
key = your key object creation logic goes here
value = your value object creation logic goes here
// Create a weak reference object using the key object as the referent
WeakReference wKey = new WeakReference(key, q);
// Place the key-value pair in the Hashtable. Note that we place key wrapped
// in the weak reference. That is, we will use wKey as key
ht.put(wKey, value);
/* Use key and value objects in your program... */
// When done with the key object, set it to null, so it will not be strongly reachable.
key = null;
/* At this point, if garbage collector is run, weak reference to key object will be cleared and the
WeakReference, wr, will be placed in reference queue, q. */
// Your logic to remove the entry for garbage collected key object will be as follows
if (wr.isEnqueued()) {
// This will make value object eligible for reclamation
ht.remove(wr);
}
Note that using a WeakReference object to associate extra information with an object using a Hashtable involves
some complex code and logic. The java.util.WeakHashMap class provides this functionality without writing any
complex logic. You add the key-value pairs to a WeakHashMap without wrapping the key object inside a WeakReference .
The WeakHashMap class takes care of creating a reference queue and wrapping the key object in a WeakReference .
There is one important point to remember while using a WeakHashMap . The key object is reclaimed when it is not
strongly reachable. However, the value object is not reclaimed immediately. The value object is reclaimed after
the entry is removed from the map. The WeakHashMap removes the entry after the weak reference to the key has
been cleared and one of its methods put() , remove() , or clear() is called. Listing 11-9 demonstrates the use of a
WeakHashMap . The example uses objects of the BigObject class as keys as well as values. The messages in the output
Search WWH ::




Custom Search