The best way to get a substring in Android is to use (as @ user2503849 said) the TextUtlis.substring(CharSequence, int, int) method. I can explain why. If you look at the String.substring(int, int) method from android.jar (latest API 22), you will see:
public String substring(int start) { if (start == 0) { return this; } if (start >= 0 && start <= count) { return new String(offset + start, count - start, value); } throw indexAndLength(start); }
Well, than ... What do you think the private constructor of String(int, int, char[]) looks like?
String(int offset, int charCount, char[] chars) { this.value = chars; this.offset = offset; this.count = charCount; }
As we can see, it refers to the array of "old" value char[] . Therefore, the GC cannot release it.
In the latest Java, it was fixed:
String(int offset, int charCount, char[] chars) { this.value = Arrays.copyOfRange(chars, offset, offset + charCount); this.offset = offset; this.count = charCount; }
Arrays.copyOfRange(...) uses internal copying of the array internally.
What is it:)
Respectfully!
ddmytrenko Jul 22 '15 at 12:12 2015-07-22 12:12
source share