

JDK-4513622: keeping a substring of a field prevents GC for object - luu
http://bugs.java.com/view_bug.do?bug_id=4513622

======
pimlottc
This needs a 2001 tag, particular since it is no longer the case as others
have pointed out.

~~~
masklinn
It is probably the case in other languages though (I believe — though I'm open
to being wrong — it is in Go, string slicing is implemented through array
slicing, so a substring will share the parent string's storage and keep it
alive).

------
eranation
A friend of mine was actually asked this in an interview (how does substring
works). The initial idea of taking an advantage of the immutability of an
object was nice, but it turns out to be a big GC issue if your original string
is extremely large.

~~~
jeffdavis
This is why I find rust intriguing. It tracks the lifetimes using the type
system, so it would be obvious to the programmer that the large strings would
need to live as long as the list holding the small substrings.

I'm not a rust expert, but I believe that it's basically impossible to write
something like the code in the bug report without getting a compile time error
in rust. You'd either need to use a version of substring that makes a copy, or
explicitly keep the large strings around.

~~~
pcwalton
Yeah, in Rust you'd have to go out of your way to use Rc to reference count
the large strings.

------
ddispaltro
This is really old, they changed the way it worked to be a copy from jdk5 to
jdk6 for exactly this reason.

------
ephemeral007
fixed in 6 (and backported in 5, 4) see
[http://bugs.java.com/view_bug.do?bug_id=6924259](http://bugs.java.com/view_bug.do?bug_id=6924259)

------
strictfp
The view technique was actually great. You could write performant parsing and
matching code without having to keep track of positions in the string. The
current situation is much worse. You get crazy amounts of allocation and GC
for code that used to produce almost none.

------
hiringcat2
Even better - installing java on the mac install the ask toolbar that actually
adds garbage into my search results, let alone garbage collection. Howzabout
then?

