Java moves to copying for substrings

deadalnix deadalnix at gmail.com
Tue Nov 19 00:14:58 PST 2013


On Tuesday, 19 November 2013 at 07:36:29 UTC, monarch_dodra wrote:
> On Tuesday, 19 November 2013 at 05:38:14 UTC, Jesse Phillips
> wrote:
>> So apparently substrings were considered a common cause of 
>> memory leaks.
>
> I think it is pretty important to remember that slicing, while
> giving you a small view, still holds the entire array.
>
> I think there is nothing wrong with pipping a ".dup" every now
> and then, after slicing something.
>
> As a matter of fact, I've been playing around with transcoding
> strings (UTF-8/16/32). I start by allocating a large buffer to
> write into. When I'm done, I look at the buffer's useage, and if
> it's too low, I return a dup of the buffer slice, allowing the 
> GC
> to reclaim the original buffer. Not only does this take up less
> memory, but overall, I actually get better run-times too (!)

The problem with Java's string is that you don't have enough 
control to do this. Instead of giving extra control, because it 
is obviously necessary, they dumb down the thing even more.


More information about the Digitalmars-d mailing list