Memory allocation faile on string concat

Steven Schveighoffer schveiguy at
Wed Nov 10 12:07:04 PST 2010

On Wed, 10 Nov 2010 14:38:02 -0500, Xie <Xiemargl at> wrote:

> Sorry, it a mistypo (i began from wchar[], later changed to wstring)
> Real problem can be seen here
> import std.stdio;
> import;
> void f0()
> {
> 	wstring a;
> 	foreach(i; 0 .. 100_000_000)
> 	{
>   		a ~= " "w;
>   	}
> }
> void main()
> {
> 	auto r = benchmark!(f0)(10);
> 	writeln(r, "ms");
> }

OK, this actually makes sense to me.

It's a manifestation of this issue:

In essence, in order to aid performance, there is an 8-element cache of  
arrays that are being appended.  At the moment, the append code relies on  
this cache keeping the array from being collected to remain sane.   
However, as I wrote in the last message on that bug report, I think I can  
fix it so the cache is not considered a pointer to the memory, and it  
should be collected.  But I haven't done that yet.

So what's happening in your code is the first 8 times executing that  
function, the memory is not collected.  This results in you allocating  
actually 800 million wchars, or 1.6GB before anything can be collected.

So in the meantime, jump on the CC for that bug, and I hope to get to it  
sometime in the near future (maybe by 2.051-2.052).


More information about the Digitalmars-d-learn mailing list