Memory allocation faile on string concat

Steven Schveighoffer schveiguy at yahoo.com
Wed Nov 10 10:50:51 PST 2010


On Wed, 10 Nov 2010 13:33:11 -0500, Steven Schveighoffer  
<schveiguy at yahoo.com> wrote:

> 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.

Waaaait a second, a is not a wstring, it's an *array* of wstrings.  That's  
completely different.

This all makes sense now.  It's not 200MB, it's 800MB your code is asking  
for.  This might be a bit much for a test, a 32-bit system only supports  
4GB of address space, and usually 1GB is reserved, so you are trying to  
allocate about 1/3 of the memory you could possibly allocate.

Is there any reason you expect this to perform well?

-Steve


More information about the Digitalmars-d-learn mailing list