Deallocate array?

Matic Kukovec matic.kukovec at pametnidom.si
Wed May 8 02:31:06 PDT 2013


On Wednesday, 8 May 2013 at 08:59:57 UTC, Minas Mina wrote:
> On Tuesday, 7 May 2013 at 23:09:29 UTC, Matic Kukovec wrote:
>> Hi
>>
>> I'm running Windows Vista 64 with dmd 2.062.
>>
>> I have a simple program:
>>
>> import std.stdio, core.memory, std.cstream;
>> void main()
>> {
>> 	string[] temp_array;
>>
>> 	for(int i=0;i<5000000;i++)
>> 	{
>> 		++temp_array.length;
>> 		temp_array[temp_array.length - 1] = "aaaaaaaaaaaaaaaaaaaaa";
>> 	}
>> 	
>> 	temp_array = null;
>>
>> 	GC.collect();
>>        writeln("end");	
>> 	din.getc();
>> }
>>
>> When the program waits at "din.getc();", memory usage in the 
>> Task Manager is 150MB.
>>
>> Why isn't the memory deallocating?
>>
>> P.S.;
>> I tried temp_array.clear() and destroy(temp_array), but 
>> nothing changed.
>
> I guess the memory isn't freed because there is no need to do 
> so. A garbage collector kicks in when there is not enough 
> memory. In this case there is no need to.

I don't think that is the case, because if i copy/paste:
  	for(int i=0;i<5000000;i++)
  	{
  		++temp_array.length;
		temp_array[temp_array.length - 1] = "aaaaaaaaaaaaaaaaaaaaa";
  	}
  	
  	temp_array = null;

  	GC.collect();

15 times and run the code, i get a 
core.exception.OutOfMemoryError.
I don't know enough about D's garbage collection to figure out 
why the memory is not getting released/reused?


More information about the Digitalmars-d-learn mailing list