Usage of memory by arrays

Steven Schveighoffer schveiguy at yahoo.com
Thu Apr 5 22:23:12 UTC 2018


On 4/5/18 5:44 PM, unDEFER wrote:
> OK, without reallocation:
> 
> ====================8<====================
> void main()
> {
>      float[3] f;
>      float[3][] x;
>      writefln("float = %s bytes", float.sizeof);
>      writefln("float[3] = %s bytes", f.sizeof);
> 
>      int before = MemoryUsage();
> 
>      int total = 100;
>      x = new float[3][total*1000];
> 
>      int after = MemoryUsage();
>      writefln("%dK * float[3] = %d Kbytes", total, (after-before));
> }
> ====================>8====================
> 
> $ ./memory
> float = 4 bytes
> float[3] = 12 bytes
> 100K * float[3] = 2300 Kbytes
> 
> Why this so?

1. Compare to C malloc-ing 1.2MB at once (GC uses C malloc underneath)
2. Have you examined smaller numbers for total? Does it scale linearly 
or is there a threshold? (my guess is the latter)

Note that the GC does keep some metadata, but it's 1/32 the size of the 
actual memory, so I'm not sure it's relevant here.

Is there an end goal for this exercise? That is, did you find a problem 
and are trying to diagnose it by using this test? Maybe if we know the 
real problem you are having, it can be explained differently.

-Steve


More information about the Digitalmars-d mailing list