I found that using String.substring
is known for memory issues related to String.split
.
Is there a memory leak in using String.split
?
If yes what is the work-around for it?
Following link show correct usage of substring in Java.
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4513622
One more blog which talk about possible MLK in substring.
Update: Behavior has changed in 1.7.0_06: See this article: Changes to String internal representation made in Java 1.7.0_06 at java-performance.info.
As pointed out by @finnw there is indeed kind of a memory leak lurking around when using
String.substring
. The reason is thatString.substring
only returns a view of a portion of the given string, i.e., the underlying string is still kept in memory.To force the creation of a new string, unrelated to the source, you'll have to use the
new
keyword. I.e., you'll have to do for instanceor, perhaps more direct
I must say that this behavior seems "unnecessary" to me. It should be solvable using weak references or some other technique. (Especially considering that
String
is already a special class, part of the Java language specification.)