This post originated from an RSS feed registered with Java Buzz
by Markus Kohler.
Original Post: An interesting leak when using WeakHashmaps
Feed Title: Java Performance blog
Feed URL: http://feeds.feedburner.com/JavaPerformanceBlog
Feed Description: My blogs about Java Performance related topics
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.
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.