www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - delete a dynamic array

reply tsalm <tsalm free.fr> writes:
Hello,

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 b=
y  =

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 =3D new int[100000000];

  // delete the array
  t =3D 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
Oct 11 2008
next sibling parent reply "Chris R. Miller" <lordsauronthegreat gmail.com> writes:
tsalm wrote:
 Hello,
 
 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

delete t;
Oct 11 2008
parent tsalm <tsalm free.fr> writes:
  /* ------------------------------- */
 import tango.time.Time;
 import tango.io.Stdout;
 import tango.stdc.stdio;
 import tango.core.Memory;
  void main()
 {
  // Create a big array
  int[] t =3D new int[100000000];
   // delete the array
  t =3D 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 ?


 delete t;

It doesn't work too :-( Here is what I do (phobos version, tested on dmd.2.019) : */ -------------------------------- */ // import tango.io.Stdout; // import tango.stdc.stdio; import std.stdio; // import tango.core.Memory; import GC =3D std.gc ; void main() { // Create a big array int[] t =3D new int[100000000]; // delete the array delete t; // Clean the memory // GC.collect(); GC.fullCollect; GC.minimize(); // No, it's not free :-( writeln("Now, the memory must be free...");getchar; }
Oct 12 2008
prev sibling parent reply Michal Minich <michal.minich gmail.com> writes:
Hello tsalm,

 Hello,
 
 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);
Oct 12 2008
parent reply tsalm <tsalm free.fr> writes:
  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 use=


 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 =3D new int[100000000];
 // delete the array
 t =3D 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; // =3D new int[100000000]; t.length =3D 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; }
Oct 12 2008
parent reply Michal Minich <michal.minich gmail.com> writes:
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

collect dynamically allocated arrays! (But collects when you use t.lenght) This worked for me: import tango.core.Memory; GC.free (t.ptr);

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 }
Oct 12 2008
parent tsalm <tsalm free.fr> writes:
 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 =3D new int[100000000];
 // delete the array
 t =3D 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

collect dynamically allocated arrays! (But collects when you use t.lenght) This worked for me: import tango.core.Memory; GC.free (t.ptr);

Here is my code : /*---------------------------------------*/ import tango.io.Stdout; import tango.stdc.stdio; import tango.core.Memory; void main() { // Create a big array int[] t; // =3D new int[100000000]; t.length =3D 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 =3D new int[size]; } } void main () { X x =3D new X(); x.resize (100000000); // do something x.resize (0); // the memory should be free at this point }

Oops, I didn't understand that GC free the memory for itself, and not fo= r = the system ^_^ In your case, can "delete" replace "GC.free" ? All in all, "delete" works on mine : the "old" memory space can be re-u= se = by another array allocation. But why GC.minimize doesn't free the memory to the system ? Does it come= = from Windows Vista ? Or is it the wrong command ?
Oct 12 2008