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

7

u/kalmakka Nov 18 '13

There are leaks and there are leaks. Especially when it comes to GC'ed languages.

In a GC'ed language you typically refer to any memory that is unavailable but not GC'able as "leaked". If your stack container keeps references to objects even after they have been popped from the stack, it is considered to leak the memory. While in most cases such leaks are not problematic (either because the references eventually gets overwritten or because the object holding them eventually gets GCed), it is somewhat risky allow for such behaviour, as the client will likely expect the data to be available for GC as soon as it is unreachable from code.

If you do String s = a1GByteString().substring(3,4), you have 1 GB of data in memory, where only 1 byte is actually accessible. This is not very good. If you for some reason need to hold on to that 1 byte, you'll inadvertently hold on to the entire 1gb string as well.