Java moves to copying for substrings

Jonathan M Davis jmdavisProg at gmx.com
Tue Nov 19 02:37:49 PST 2013


On Tuesday, November 19, 2013 06:38:12 Jesse Phillips wrote:
> Somewhat interesting, Java has chosen to make substring result in
> a copy of the string data rather than returning a window of the
> underlying chars.
> 
> http://www.reddit.com/r/programming/comments/1qw73v/til_oracle_changed_the_i
> nternal_string/
> 
> "reduce the size of String instances. [...] This was the trigger."
> 
> "avoid memory leakage caused by retained substrings holding the
> entire character array."
> 
> So apparently substrings were considered a common cause of memory
> leaks. I got the impression most of the comments agreed the
> result is good, but changing the complexity is bad.
> 
> I'm not advocating such a change for D.

Yikes. Maybe that's a good idea for Java for some reason, but I'd consider 
slicing strings to be a _huge_ strength of D. Still, Java's situation is 
rather different, because all of the slicing stuff is more of an implementation 
detail than a core feature like it is in D.

It _is_ true however that if you're not careful about it, you can end up with 
a lot of slices that keep whole blocks of memory from being collected when 
they don't really need to refer to that memory anymore. So, depending on what 
profiling shows, some applications may need to make adjustments to avoid having 
slices keep too much extraneous memory from being collected. So, it's 
something to keep in mind, but I defintely don't think that we should be 
changing our approach at all.

- Jonathan M Davis


More information about the Digitalmars-d mailing list