News for hackers. Stuff that matters.
Re:JDK-4513622: keeping a substring of a field prevents GC f... by pimlottc on Sunday March 08, 2015 @02:08PM (#9166465)
This needs a 2001 tag, particular since it is no longer the case as others have pointed out.
Re:JDK-4513622: keeping a substring of a field prevents GC f... by masklinn on Sunday March 08, 2015 @03:05PM (#9166721)
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).
[deleted]
Re:JDK-4513622: keeping a substring of a field prevents GC f... by masklinn on Sunday March 08, 2015 @03:44PM (#9166910)
> substrings are mutable

Are you sure? I know that a byte slice []byte is mutable, but I thought slicing a string yielded a string (and strings are immutable), an explicit conversion (and copy) is needed to get the underlying byte slice.

Re:JDK-4513622: keeping a substring of a field prevents GC f... by pcwalton on Sunday March 08, 2015 @04:13PM (#9167031)
Oh yes, my mistake, they're immutable. I was thinking of []byte.
Re:JDK-4513622: keeping a substring of a field prevents GC f... by eranation on Sunday March 08, 2015 @01:56PM (#9166414)
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.
Re:JDK-4513622: keeping a substring of a field prevents GC f... by jeffdavis on Sunday March 08, 2015 @02:46PM (#9166634)
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.

Re:JDK-4513622: keeping a substring of a field prevents GC f... by pcwalton on Sunday March 08, 2015 @03:25PM (#9166822)
Yeah, in Rust you'd have to go out of your way to use Rc to reference count the large strings.
Re:JDK-4513622: keeping a substring of a field prevents GC f... by dfkf on Sunday March 08, 2015 @02:03PM (#9166441)
It is one of those cases where you can actually leak memory with a GC. But at least Java gives (gave?) you a choice: to create a new string a use the current one, in .NET substring always returns a new string.
Re:JDK-4513622: keeping a substring of a field prevents GC f... by ddispaltro on Sunday March 08, 2015 @02:01PM (#9166432)
This is really old, they changed the way it worked to be a copy from jdk5 to jdk6 for exactly this reason.
Re:JDK-4513622: keeping a substring of a field prevents GC f... by ephemeral007 on Sunday March 08, 2015 @02:01PM (#9166429)
fixed in 6 (and backported in 5, 4) see http://bugs.java.com/view_bug.do?bug_id=6924259
Re:JDK-4513622: keeping a substring of a field prevents GC f... by strictfp on Monday March 09, 2015 @03:38AM (#9169722)
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.
Re:JDK-4513622: keeping a substring of a field prevents GC f... by hiringcat2 on Sunday March 08, 2015 @02:53PM (#9166658)
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?