Java String.split memory leak?

2020-02-10 06:00发布

问题:

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.

http://nflath.com/2009/07/the-dangers-of-stringsubstring/

回答1:

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 that String.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 instance

String[] parts = orig.split(";");
//String mySubstring = parts[i];               // keeps orig from being GC'd
String mySubstring = new String(parts[i]);     // creates a new string.

or, perhaps more direct

String mySubstring = new String(orig.split(";")[i]);

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.)