Wednesday, 28 October 2015

How to resolve the memory leak problem of substring() method in JDK 1.6?

Resolve the memory leak in JDK 1.6

subString = string.substring(3, 10) + "";

In above code, the string.substring(3, 10) will return the substring which point to original string array and the substring will not allow the garbage collection for old string (char value[]).

But when we add the empty string to offset, new string will form in constant pool with new char value[] array and we can overcome the problem of garbage collection of old string array.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...