Deallocate array?
Steven Schveighoffer
schveiguy at yahoo.com
Tue May 7 20:00:42 PDT 2013
On Tue, 07 May 2013 19:09:28 -0400, Matic Kukovec
<matic.kukovec at pametnidom.si> 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";
This is very inefficient, use temp_array ~= "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?
The GC does not return free memory to the OS, just to free memory
pools/free-lists. GC.minimize may or may not help.
But your code may not have freed that memory anyway. It is not really
possible to ensure that temp_array isn't referred to. For example, the
compiler could keep temp_array in a register.
> P.S.;
> I tried temp_array.clear() and destroy(temp_array), but nothing changed.
Neither of these will deallocate memory. All are equivalent to setting
temp_array = null.
If you want to ensure deallocation, you need to free it using
GC.free(temp_array.ptr). A very dangerous operation, use with caution,
make sure there are no other references to that data.
-Steve
More information about the Digitalmars-d-learn
mailing list