Memory allocation faile on string concat

Steven Schveighoffer schveiguy at yahoo.com
Wed Nov 10 10:33:11 PST 2010


On Wed, 10 Nov 2010 11:33:45 -0500, Xie <xiemargl at gmail.com> wrote:

> Can't run a simple program. What's wrong, GC?
>
> import std.stdio;
> import std.date;
>
> void f0()
> {
> 	wstring a[];
>
> 	foreach(i; 0 .. 100_000_000)
> 	{
>   		a ~= " "w;
>   	}
> }
>
> void main()
> {
> 	auto r = benchmark!(f0)(1);
> 	writeln(r, "ms");
> }

The results on my machine with 1G of memory is that it consumes 2G of  
memory and the system starts thrashing.  I changed the value to  
10_000_000, and it runs in a couple seconds, I change it to 50_000_000 and  
it runs in 200 seconds.

Something is definitely amiss here, because the following doesn't help (it  
still consumes 1.2GB of memory):

void f0()
{
	wstring a[];
	a.reserve(100_000_000);

	foreach(i; 0 .. 100_000_000)
	{
   		a ~= " "w;
   	}
}

This should take the explosive nature of appending out of the equation,  
because a reallocation should never occur.

I'll look into it.

-Steve


More information about the Digitalmars-d-learn mailing list