Wednesday, June 4, 2008

The interesting leak

This again is one of those interesting observations I ran into during some memory optimizations CXF. Coincidently here also its a WeakHashMap.
Consider the code snippet below.
public class TestWeakHashMap
{
private String str1 = new String("newString1");
private String str2 = "literalString2";
private String str3 = "literalString3";
private String str4 = new String("newString4");
private Map map = new WeakHashMap();

private void testGC() throws IOException
{
map.put(str1, new Object());
map.put(str2, new Object());
map.put(str3, new Object());
map.put(str4, new Object());

/**
* Discard the strong reference to all the keys
*/
str1 = null;
str2 = null;
str3 = null;
str4 = 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);
}
}
}
What do we expect the size of the map to be after full GC? I initially thought it should be empty. But it turned out to be 2.

Look at the way the four Strings are initialized. Two of them are defined using the 'new' operator, whereas the other two are defined as literals. The Strings defined using the 'new' operator would be allocated in the Java heap, but the Strings defined defined as literals would be in the literal pool.
The Strings allocated in the literal pool (Perm Space) would never be garbage collected.
This would mean that String 'str2' and 'str3' would always be strongly referenced and the corresponding entry would never be removed from the WeakHashMap.

So next time you create a 'new String()' , put it as a key in a WeakHashMap, and later intern() the String, beware - Your key will always be strongly referenced. [Invoking intern() method on a String will add your String to the literal pool if some other String equal to this String does not exist in the pool]

11 comments:

Markus Kohler said...

Hi,
Good post!
I was first a little bit surprised about this.
Check my blog at
http://kohlerm.blogspot.com/2008/06/interesting-leak-when-using.html

Regards,
Markus

Markus Kohler said...

my analysis

Anonymous said...

miley cyrus nude [url=http://www.ipetitions.com/petition/mileycyrus]miley cyrus nude[/url] paris hilton nude [url=http://www.ipetitions.com/petition/parishilt]paris hilton nude[/url] kim kardashian nude [url=http://www.ipetitions.com/petition/kimkardashian45]kim kardashian nude[/url] kim kardashian nude [url=http://www.ipetitions.com/petition/celebst]kim kardashian nude[/url]

cheap viagra said...

hey what is leak, I was reading but I can't understand it!!please explain more

Anonymous said...

Hotel reservations

Anonymous said...

Good Day! Lewis Garza . payday loans

buy cialis online said...

Good Day! Lewis Garza
buy cialis online

Joshua Smith said...

It was amazing to read many good advice in this review. To improve the business it suggested to use outsourcing software development services to release on IT markets. Software development allows to automate business processes and earn more profit funds.

Joshua Smith said...

A lot of thanks for this great review. Casino partners always look for best casino affiliate programs to increase their revenue income from best casinos or poker rooms.

Joshua Smith said...

Thanks for mentioning this great info. For those who is looking for new cars, compare autoquotes from top car insurers.

Joshua Smith said...

Its interesting. Let me tell you something about cheap homeowners insurance provided by top home insurers.