I create a WeakHashMap as
WeakHashMap<Employee,String> map = new WeakHashMap<Employee,String>(); map.put(emp,"hello");
where emp is an Employee object. Now if I do emp = null or say emp object is no longer referenced, then will the entry be removed from the WeakHashMap i.e. will the size of Map be zero?
And will it be vice-versa in case of HashMap?
Is my understanding of WeakHashMap correct?
WeakHashMap is an implementation of the Map interface that stores only weak references to its keys. Storing only weak references allows a key-value pair to be garbage-collected when its key is no longer referenced outside of the WeakHashMap. This class provides the easiest way to harness the power of weak references.
You can use a WeakHashmap to reduce the chance of a memory leak as a result of caching some object. The WeakHashMap will automatically remove entries whenever all references to the key are removed.
A WeakHashMap has key-value pairs i.e. it is quite similar to a HashMap in Java. A difference is that the WeakHashMap object that is specified as a key is still eligible for garbage collection. This means that the garbage collector has dominance over the WeakHashMap.
WeakHashMap is an implementation of the Map interface. WeakHashMap is almost same as HashMap except in case of WeakHashMap, if object is specified as key doesn't contain any references- it is eligible for garbage collection even though it is associated with WeakHashMap. i.e Garbage Collector dominates over WeakHashMap.
A very simple example, to enlighten what has already been said :
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 < 10000; 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; } } }
Output :
map contains someDataObject ? true ... At iteration 6216 the map still holds the reference on someDataObject At iteration 6217 the map still holds the reference on someDataObject At iteration 6218 the map still holds the reference on someDataObject somDataObject has finally been garbage collected at iteration 6219, hence the map is now empty
I ran the sample code to understand the difference between HashMap
and WeakHashMap
Map hashMap= new HashMap(); Map weakHashMap = new WeakHashMap(); String keyHashMap = new String("keyHashMap"); String keyWeakHashMap = new String("keyWeakHashMap"); hashMap.put(keyHashMap, "helloHash"); weakHashMap.put(keyWeakHashMap, "helloWeakHash"); System.out.println("Before: hash map value:"+hashMap.get("keyHashMap")+" and weak hash map value:"+weakHashMap.get("keyWeakHashMap")); keyHashMap = null; keyWeakHashMap = null; System.gc(); System.out.println("After: hash map value:"+hashMap.get("keyHashMap")+" and weak hash map value:"+weakHashMap.get("keyWeakHashMap"));
The output will be:
Before: hash map value:helloHash and weak hash map value:helloWeakHash After: hash map value:helloHash and weak hash map value:null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With