Markus Kohler
Posts: 74
Nickname: kohlerm
Registered: Jun, 2008
|
Markus Kohler is an software architect at SAP
|
|
|
|
An interesting leak when using WeakHashmaps
|
Posted: Jun 20, 2008 9:28 AM
|
|
Bharath Ganesh desribes in the blog post Thoughts around Java, Web Services, IT: The interesting leak
a very interesting leak, were WeakHashmap doesn't seem to release entries that don't seem to be referenced anymore. Actually when interned String literals are used the entries stay in the WeakHashmap even after all hard references seem to be removed.
I say "seemed to be removed" because there actually is still a reference from the Class, that is still loaded, to the interned String literal.
Unfortunately using a heap dump do find this out, does not work, because this implicit reference is (currently) not written to the heap dump file.
Changing the example to :
import java.io.IOException; import java.util.Map; import java.util.WeakHashMap; import junit.framework.*;
public class TestWeakHashMap extends TestCase { private String str1 = new String("newString1"); private String str2 = "literalString2"; private String str3 = "literalString3"; private String str4 = new String("newString4"); private String str5 = (str4+str1).intern();
private Map map = new WeakHashMap();
public void testGC() throws IOException { map.put(str1, new Object()); map.put(str2, new Object()); map.put(str3, new Object()); map.put(str4, new Object()); map.put(str5, new Object());
/** * Discard the strong reference to all the keys */ str1 = null; str2 = null; str3 = null; str4 = null; str5 = null;
while (true) { System.gc(); /** * Verify Full GC with the -verbose:gc option * We expect the map to be emptied as the strong references to * all the keys are discarded. */ System.out.println("map.size(); = " + map.size() + " " + map); } } }
You will find that str5 will be reclaimed by the Garbage Collector.
Conclusion Using String literals defined in your Classes for keys in a WeakHashmap might not do what you want, but using interned Strings in general as keys for WeakHashmap is safe.
Read: An interesting leak when using WeakHashmaps
|
|