On Monday, 30 December 2013 at 06:52:20 UTC, Ilya Yaroshenko wrote:
case 1:
 delete ar;
case 2:
 ar.destroy();
case 3:
 GC.free(ar.ptr);
case 4:
ar = null;// (assumed that ar is only one pointer to the same array)

What is the difference?

How to free memory to the system immediately?

It depends on how it is allocated. The `delete` operator is deprecated, so it is never appropriate.

For GC memory (memory allocated with `new`, the concatenation operators or array literals) then you should just `null` any reference that is no longer used, leaving it to the GC to collect the memory when it sees fit. This guarantees memory safety, which is the primary purpose of a memory garbage collector.

If you really, really must free a chunk of GC memory *right now*, you can use GC.free, but you lose any guarantee of memory safety. If you can't think of a reason your code needs to do this, then your code probably doesn't need it.

If the array is allocated from a non-GC heap, such as the C heap, use the corresponding freeing function (C heap using `malloc` + `free`).

`destroy` is for running destructors on, and then invalidating, instances of user-defined types (UDTs). For generic code purposes, you can use it on non-UDTs, in which case it will just invalidate them. Values invalidated with `destroy` must not be used after invalidation - it is a logic error. What it means by invalidation is up to the implementation, but it guarantees that it won't free memory. Currently, it zero-blasts the value (which may be different from the value's default initializer).

Reply via email to