WEAKHASHMAP It's not unusual to store meta information about objects in an application; for example, information on a previous window position for a JFrame or a sequence of object events for an undo function. A common method for storing this information is in a HashMap class that uses the object as a key to retrieve the stored value. But this is a common source of memory leaks, and you have to be careful to remove the key from the HashMap when the referenced object is no longer needed. If you fail to do this, it will never be garbage collected. A normal "strong" object reference, such as ones used by HashMap, prevents an object from being garbage collected as long as that reference exists. The solution to this problem is WeakHashMaps. WeakHashMaps provide a simple way to manage collections of objects that are automatically garbage collected when no longer referenced from elsewhere in an application. A WeakHashMap holds its keys using weak references and does not inhibit the garbage collector from releasing the objects referenced when needed. Note that the key objects in the WeakHashMap will only be evaluated for release by the garbage collector when the WeakHashMap is accessed, such as via get() or put(). If the WeakHashMap object is left alone, the referenced keys will not be evaluated nor released. Another excellent use for WeakHashMap is with the observer/observable pattern. When objects register themselves as observers (via the Swing addActionListener() method), they are usually added to a List or Vector in the observable object. This is also a prime source of memory leaks, as the observer needs to be removed from the observable object's list of listeners in order to be garbage collected. If the observable object keeps its list of listeners in a WeakHashMap, however, this step is no longer necessary and the observer will automatically be garbage collected when no other references exist. ------------------------------------------