www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to delete dynamic array ?

reply "Ilya Yaroshenko" <ilyayaroshenko gmail.com> writes:
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?

___________________________
Thank You & Best Regards,
Ilya
Dec 29 2013
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Ilya Yaroshenko:

 case 1:
  delete ar;
It's deprecated.
 case 4:
  ar = null;// (assumed that ar is only one pointer to the same 
 array)
It's the simpler way.
 How to free memory to the system immediately?
What is your use case? Bye, bearophile
Dec 30 2013
prev sibling next sibling parent "Dicebot" <public dicebot.lv> writes:
Answer will make more sense if ar is assumed to be any 
heap-allocated object, not just dynamic array.

On Monday, 30 December 2013 at 06:52:20 UTC, Ilya Yaroshenko 
wrote:
 case 1:
  delete ar;
Deprecated. Used to call destructor and GC.free after that
 case 2:
  ar.destroy();
Current solution to free resources in deterministic way. Calls destructor and sets `ar` to init state so that it can be collected by GC at some unknown point later.
 case 3:
  GC.free(ar.ptr);
Marks memory as freed in GC. Unsafe and discouraged because there can still be references pointing at it.
 case 4:
  ar = null;// (assumed that ar is only one pointer to the same 
 array)
Simply cleans the reference to an object. If it was the only reference, it will be destroyed and freed upon next GC cycle.
 What is the difference?

 How to free memory to the system immediately?
GC.free However, if you want deterministic deallocation it makes more sense to use C malloc or custom allocators as GC tends to run faster if it is aware of less memory.
Dec 30 2013
prev sibling parent "Jakob Ovrum" <jakobovrum gmail.com> writes:
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).
Dec 30 2013