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);
}
```
      • Re: How... Alain De Vos via Digitalmars-d-learn
        • Re:... Alain De Vos via Digitalmars-d-learn
          • ... Steven Schveighoffer via Digitalmars-d-learn
        • Re:... Steven Schveighoffer via Digitalmars-d-learn
      • Re: How... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
        • Re:... Alain De Vos via Digitalmars-d-learn
          • ... drug007 via Digitalmars-d-learn
            • ... Alain De Vos via Digitalmars-d-learn
              • ... drug007 via Digitalmars-d-learn
            • ... Alain De Vos via Digitalmars-d-learn
  • Re: How to free ... Nick Treleaven via Digitalmars-d-learn

Reply via email to