When are keys removed in Weakhashmap

Let’s say that we want to build a cache that keeps big image objects as values, and image names as keys. We want to pick a proper map implementation for solving that problem.

Using a simple HashMap will not be a good choice because the value objects may occupy a lot of memory. What’s more, they’ll never be reclaimed from the cache by a GC process, even when they are not in use in our application anymore.

Ideally, we want a Map implementation that allows GC to automatically delete unused objects. When a key of a big image object is not in use in our application in any place, that entry will be deleted from memory.

Fortunately, the WeakHashMap has exactly these characteristics. Let’s test our WeakHashMap and see how it behaves:

import java.util.WeakHashMap;
 
public class WeakHashMapDemo {
 
    public static void main(String[] args) {
        // -- Fill a weak hash map with one entry
        WeakHashMap<Data, String> map = new WeakHashMap<Data, String>();
        Data someDataObject = new Data("foo");
        map.put(someDataObject, someDataObject.value);
        System.out.println("map contains someDataObject ? " + map.containsKey(someDataObject));
 
        // -- now make someDataObject elligible for garbage collection...
        someDataObject = null;
 
        for (int i = 0; i < 1000000; i++) {
            if (map.size() != 0) {
                System.out.println("At iteration " + i + " the map still holds the reference on someDataObject");
            } else {
                System.out.println("somDataObject has finally been garbage collected at iteration " + i + ", hence the map is now empty");
                break;
            }
        }
    }
 
    static class Data {
        String value;
        Data(String value) {
            this.value = value;
        }
    }
}

This is how output will look like. It will vary from system to system

At iteration 70678 the map still holds the reference on someDataObject
At iteration 70679 the map still holds the reference on someDataObject
At iteration 70680 the map still holds the reference on someDataObject
At iteration 70681 the map still holds the reference on someDataObject
At iteration 70682 the map still holds the reference on someDataObject
At iteration 70683 the map still holds the reference on someDataObject
At iteration 70684 the map still holds the reference on someDataObject
At iteration 70685 the map still holds the reference on someDataObject
At iteration 70686 the map still holds the reference on someDataObject
At iteration 70687 the map still holds the reference on someDataObject
At iteration 70688 the map still holds the reference on someDataObject
At iteration 70689 the map still holds the reference on someDataObject
At iteration 70690 the map still holds the reference on someDataObject
At iteration 70691 the map still holds the reference on someDataObject
At iteration 70692 the map still holds the reference on someDataObject
At iteration 70693 the map still holds the reference on someDataObject
somDataObject has finally been garbage collected at iteration 70694, hence the map is now empty

An entry in a WeakHashMap will automatically be removed when its key is no longer in ordinary use (even having mapping for a given key will not prevent the key from being discarded by the garbage collector that is made finalizable , finalized and then reclaimed)

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *