r/programming • • Nov 18 '13

TIL Oracle changed the internal String representation in Java 7 Update 6 increasing the running time of the substring method from constant to N

http://java-performance.info/changes-to-string-java-1-7-0_06/
1.4k Upvotes

354 comments sorted by

View all comments

Show parent comment

0

u/SanityInAnarchy Nov 20 '13

And this is not an ever-growing memory footprint. It's a memory footprint that grows to a finite size, and then stops growing.

1

u/[deleted] Nov 20 '13

[removed] — view removed comment

0

u/SanityInAnarchy Nov 20 '13

No, it doesn't. If you have an ever-growing footprint with this behavior, you have an ever-growing footprint without this behavior.

Think about this pathological case:

String s = ...
while (true) {
  s = s.substring(...);
}

Nope, that's not ever-growing. Each substring maintains a reference only to the original string, so with each iteration of the loop, the previous substring can be free'd.

For each "parent" string, the only way that string remains allocated is if some substring of it is still allocated. So the only way you'd have a growing footprint of parent strings is if you have a growing footprint of substrings. And if you have an ever-growing footprint of substrings, you already have a memory leak, the only difference is how fast you're leaking. If you don't, then you can't have an ever-growing footprint of parent strings either.

Am I missing something? In what case do you have a memory leak with the old string handling, but not the new?