Print RAM used by arrays

Steven Schveighoffer schveiguy at gmail.com
Thu Dec 13 22:14:39 UTC 2018


On 12/13/18 4:32 PM, Giovanni Di Maria wrote:
> Hi.
> How can I know the amount of RAM allocated by a vector?
> 
> For example:
> 
> string[8][1000] array;
> for(int i=0;i<1000;i++) {
> array[i]=["1111","2222","3333","4444","5555","6666","7777","8888"];
> }
> 
> how can I know the amount of bytes of above matrix?

array.sizeof. BUT I would caution that you have used fixed-sized arrays 
so they will NOT be allocated on the heap, but rather on the stack (or 
thread-local storage if it's a global).

For a variable-sized array, such as string[8][], the .sizeof property is 
always going to be 2 words.

For that case, you need to use the GC to ask for the block size:

writeln(GC.sizeof(array.ptr));

> Can I clean the memory ofter his use, without use GC?

It depends on where you put it. But generally D does not give back any 
memory to the OS unless asked to do so.

But maybe that's not your question?

-Steve


More information about the Digitalmars-d-learn mailing list