How to free memory ater use of "new" to allocate it.
    Nick Treleaven 
    nick at geany.org
       
    Mon Jul 17 16:39:51 UTC 2023
    
    
  
On Sunday, 16 July 2023 at 18:18:08 UTC, Alain De Vos wrote:
>   12   │     ~this(){
>   13   │         writeln("Free heap");
>   14   │         import object: destroy;
>   15   │         import core.memory: GC;
>   16   │         i=null; // But How to force GC free ?
Firstly, be careful with class destructors and GC managed 
objects. The memory referenced by `i` may already have been freed 
by the GC when the class destructor is called - see:
https://dlang.org/spec/class.html#destructors
If you want to free GC memory when you know the allocation is 
still live, you can call __delete:
https://dlang.org/phobos/core_memory.html#.__delete
import std.stdio: writeln;
```d
void main()
{
     int[] i=null;
     writeln("Allocate heap");
     i=new int[10000];
     i[9000]=5;
     import core.memory;
     const p = i.ptr;
     assert(GC.addrOf(p) !is null);
     writeln("Free heap");
     __delete(i);
     assert(GC.addrOf(p) is null);
}
```
    
    
More information about the Digitalmars-d-learn
mailing list