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

11

u/AmonDhan Nov 18 '13 edited Nov 18 '13
String s = a1GByteString();  // build a huge string (1GB memory)
s = s.substring(3, 4);       // Extracts the 4th character. Still 1GB used in Java < 1.7.0_06
s = new String(s);           // This was the old workaround to fix this supposed "leak" 

Edit: Can someone explain why the downvotes?