Close

Java - Understanding WeakHashMap

[Last Updated: Jul 8, 2017]

java.util.WeakHashMap has weak keys. Each key object in a WeakHashMap is stored as the referent of a weak reference. An entry in a WeakHashMap will automatically be removed when its key is no longer externally referenced and the key is due for garbage collection. Let's understand that with examples.

Examples

Example with no external references of Keys and values

In following example, we are not going to keep references of both keys and values of the WeakHashMap.

public class WeakHashMapExample {

  public static void main(String[] args) throws InterruptedException {
      WeakHashMap<Key, MyObject> map = new WeakHashMap<>();
      int size = 10000;
      for (int i = 0; i < size; i++) {
          Key key = new Key();
          MyObject value = new MyObject();
          map.put(key, value);
      }

      System.gc();
      System.out.println("keys gced: " + Key.getKeyFinalizeCount());
      System.out.println("values gced: " + MyObject.getValueFinalizeCount());

      System.out.println("Map initial size: " + size);
      System.out.println("Map current size: " + map.size());
  }
}
public class Key {
  private static int keyFinalizeCount;

  public static int getKeyFinalizeCount() {
      return keyFinalizeCount;
  }

  @Override
  protected void finalize() throws Throwable {
      keyFinalizeCount++;
  }
}
public class MyObject {
  private static int valueFinalizeCount;
  private int[] bigArray = new int[10000];

  public static int getValueFinalizeCount() {
      return valueFinalizeCount;
  }

  @Override
  protected void finalize() throws Throwable {
      valueFinalizeCount++;
  }
}

Output

keys gced: 8001
values gced: 3384
Map initial size: 10000
Map current size: 1999

Many keys and values were garbage collected and the related entries were removed from the map.

Example with only keys having external references

public class WeakHashMapExample2 {

  public static void main(String[] args) throws InterruptedException {
      WeakHashMap<Key, MyObject> map = new WeakHashMap<>();
      List<Key> keys = new ArrayList<>();
      int size = 10000;
      for (int i = 0; i < size; i++) {
          Key key = new Key();
          MyObject value = new MyObject();
          map.put(key, value);
          keys.add(key);
      }

      System.gc();
      System.out.println("keys gced: " + Key.getKeyFinalizeCount());
      System.out.println("values gced: " + MyObject.getValueFinalizeCount());

      System.out.println("Map initial size: " + size);
      System.out.println("Map current size: " + map.size());
  }
}

Output

keys gced: 0
values gced: 0
Map initial size: 10000
Map current size: 10000

No keys or values were garbage collected and no entries of the map were removed.

Example with only values having external references

public class WeakHashMapExample3 {

  public static void main(String[] args) throws InterruptedException {
      WeakHashMap<Key, MyObject> map = new WeakHashMap<>();
      List<MyObject> values = new ArrayList<>();
      int size = 10000;

      for (int i = 0; i < size; i++) {
          Key key = new Key();
          MyObject value = new MyObject();
          map.put(key, value);
          values.add(value);
      }

      System.gc();
      System.out.println("keys gced: " + Key.getKeyFinalizeCount());
      System.out.println("values gced: " + MyObject.getValueFinalizeCount());

      System.out.println("Map initial size: " + size);
      System.out.println("Map current size: " + map.size());
  }
}

Output

keys gced: 6648
values gced: 0
Map initial size: 10000
Map current size: 3381

Many keys were garbage collected and corresponding map entries were automatically removed, but values were not garbage collected till the end of the main method.

Conclusion

A map entry is eligible for removal if no references of corresponding key are used externally, regardless of if corresponding values are externally referenced or not.

Example Project

Dependencies and Technologies Used:

  • JDK 1.8
  • Maven 3.3.9

WeakHashMap Example Project Select All Download
  • weak-hash-map-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • WeakHashMapExample.java

    See Also