Tuesday, June 3, 2008

Some more play with WeakHashMap

Last couple of days, I ran into some interesting observations while doing some memory optimizations CXF.
Consider the below snippet.

import java.util.Map;
import java.util.WeakHashMap;

public class TestWeakHeakHashMap
private String name = new String("java");
private Map cache = new WeakHashMap<String, ComplexDO>();

public void testMethod()
cache.put(name, new ComplexDO("1", name));

//Discard the strong reference to the key
name = null;
while (true) {
* Verify Full GC with the -verbose:gc option
Since there is no strong reference to the key, it is assumed that the
entry has been removed from the WeakHashMap

private class ComplexDO
private String id;
private String name;

public ComplexDO(String id, String name)
this.id = id;
this.name = name;

Now when the testMethod() is run what do you expect the output to be? Since the strong reference to key is discarded, we assume that the entry from the map would be removed, and map would be empty after a full GC.
But that does not happen though.

Let us see what was the put operation on the WeakHashMap.

cache.put(name, new ComplexDO("1", name));
Here the value ComplexDO was holding the key name. This would mean that the value always strongly refers to the key, and hence the key would never be garbage collected. The entry would always remain the map.

This is what WeakHashMap API says -
"The value objects in a WeakHashMap are held by ordinary strong references. Thus care should be taken to ensure that value objects do not strongly refer to their own keys, either directly or indirectly, since that will prevent the keys from being discarded."


Coralie said...

This is great info to know.

Anonymous said...

miley cyrus nude [url=http://crystal-liu.com/forums/index.php?showuser=1113]miley cyrus nude[/url] miley cyrus sex tape [url=http://forum.ondertitel.com/index.php?showuser=74503]miley cyrus sex tape[/url] miley cyrus nude [url=http://stabilo.forumsunlimited.com/index.php?showuser=799]miley cyrus nude[/url] miley cyrus nude [url=http://ragga-jungle.com/user/15036-vebsterd]miley cyrus nude[/url] kim kardashian nude [url=http://forums.quark.com/members/vebsterd.aspx]kim kardashian nude[/url]

Anonymous said...

die Ausgezeichnete Antwort, ist wacker:) levitra vs viagra dosage levitra preis [url=http//t7-isis.org]cialis preis[/url]

Anonymous said...

used car melbourne fl
used car bozeman
used car kooskia mt
ricart used car factory
car hire online
pimp your car online
custom your car online
custom bluid your car online

Joshua Smith said...

It was amazing to receive many good info in this survey. To improve the business it's suggested to use software outsourcing development to appear on IT markets. Software development allows to automate business processes and gain more profits.

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