Thursday, 13 June 2019

Core Java | 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 the above code, the string.substring(3,10) will return the substring which points to an original string array and the substring will not allow the garbage collection for the old string (char value[]).
But when we add the empty string to offset, the new string will form in the constant pool with new char value[] array and we can overcome a problem of garbage collection of an old string array.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...