Monday, June 2, 2008

WeakHashMap : An Observation

Consider the following code snippet.

1 public class TestWeakHashMap {
2 public static void main(String[] args) {
3 WeakHashMap map=new WeakHashMap();

4 String s1=new String("java");
5 map.put(s1, "good");

6 String s2=new String("java");
7 map.put(s2,"ok");

//Since s1.equals(s2) is true and hash is same, the earlier value
//against key s1 ("good") in the map is replaced by the new one. ("ok")

8 s1=null;

9 System.gc();
//Verify Full GC with the -verbose:gc option

10 System.out.println(map.size());
11 }
12 }

What do we expect the output to be? 1? No, Not exactly.

Here s1 and s2 are two different objects on the heap. So in line 5, a new (key,value) pair with key s1 is put into the map. Later when a (key,value) with key s2 is being put into the map, it checks for equals on s1 and s2 and their hashcode. When it finds the equals returns true and hashcde is same, it replaces the value of the earlier entry with the new value. But the issue(?) here is, WeakHashMap/HashMap does not replace the earlier key while adding a (key, value) pair whose key is actually a duplicate key in the map.
So even after putting an entry with key s2, the WeakHashMap has only one entry whose key refers to the object refered by s1 and not s2.
Now the object on the heap refered by s1, has one strong reference(through s1) and one weak reference through the WeakHashMap.
Later when I say s1=null, the object on the heap refered to by s1 lost the strong reference and when gc happens, the entry is removed from the map.

So thats how it works.

Also note WeakHashMap is only a wrapper over HashMap and the HashMap's put api says " If the map previously contained a mapping for this key, the old value is replaced by the specified value."

So just be careful when you use WeakHashMap and your usage scenario is similar to the above.


akhilesh said...

Jobs site

India's growing job site.Jobs available in sectors like Accounting,
Marketing,IT,Engineering,Finance,BPO/Call Centers,Bio Technology,
Business Mgmt.,Retails,Hotels & Others.
Find Jobs In India,UK,USA,Middle East.

Anonymous said...

turning whichever retina uksvjksa goodwill hilarious assembly eliminates multiple gases hindley
semelokertes marchimundui

Ashank said...

nice post. thanks

Joshua Smith said...

Also you may turn your attention on offshore software development company.

Quentin Soto said...

You may convert your interest on overseas application growth company.

for IT the said...

Java SE WeakHashMap Courses An Observation Training Java Training Institutes in Chennai Java SE Course WeakHashMap has only one entry Training whose key refers Java Training in Chennai Java SE & Java EE Courses wrapper over HashMap and the HashMap's Training Java Online Course WeakHashMap/HashMap does not replace the earlier key Training Java SE Java EE Training

Kristi Byrd said...

Thanks intended for expressing an exceptionally helpful and intensely educational blog.nimble app

Rose Angel said...

IVR administration is driving ivr organization supplier in USA with all part twilio IVR, for example, laborer after structure and call recording organization. here is association for more data

Giri Mani 2 said...

This blog provides a observation of week hash map. This helps to learn more about this process. This gives the coded explanation. That is very helpful to me. Thanks for this blog.
Java Training in Chennai