delete a dynamic array

Michal Minich michal.minich at gmail.com
Sun Oct 12 08:03:17 PDT 2008


Hello tsalm,

>>> It's certainly a stupid question...
>>> I want to delete an dynamic array of the memory.
>>> I don't understand why the example above doesn't clear the memory
>>> uses
>>> by
>>> the array :
>>> /* ------------------------------- */
>>> import tango.time.Time;
>>> import tango.io.Stdout;
>>> import tango.stdc.stdio;
>>> import tango.core.Memory;
>>> void main()
>>> {
>>> // Create a big array
>>> int[] t = new int[100000000];
>>> // delete the array
>>> t = null;
>>> // Clean the memory
>>> GC.collect();
>>> GC.minimize();
>>> // No, it's not free :-(
>>> Stdout("Now, the memory must be free...")();getchar;
>>> }
>>> /* ------------------------------- */
>>> How can I totally clear this array of the memory ?
>>> Thanks in advance,
>>> TSalm
>> I had the same problem. It seems to me that Tango GC just doens't
>> collect dynamically allocated arrays! (But collects when you use
>> t.lenght)
>> 
>> This worked for me:
>> 
>> import tango.core.Memory;
>> 
>> GC.free (t.ptr);
>> 
> Not for me :(
> Here is my code :
> /*---------------------------------------*/
> import tango.io.Stdout;
> import tango.stdc.stdio;
> import tango.core.Memory;
> void main()
> {
> // Create a big array
> int[] t; // = new int[100000000];
> t.length = 100000000 ;
> // delete the array
> GC.free (t.ptr);
> // Clean the memory
> GC.collect();
> GC.minimize();
> // No, it's not free :-(
> Stdout("Now, the memory must be free...")();getchar;
> }

Ok, here's the simplified version of code I used:

class X {

  int[] t;

  void resize (int size) {

    GC.free (t.ptr);
    t = new int[size];
  }
}

void main () {

  X x = new X();
  x.resize (100000000);
  // do something
  x.resize (0);
  // the memory should be free at this point 
}




More information about the Digitalmars-d-learn mailing list