Usage of memory by arrays

unDEFER undefer at gmail.com
Thu Apr 5 20:58:32 UTC 2018


Hello!
Here very simple test program:
------------------->8--------------------
import std.conv;
import std.stdio;

import std.string;

int MemoryUsage()
{
     auto file = File("/proc/self/status");
     foreach (line; file.byLine())
     {
         if (line[0..6] == "VmRSS:")
         {
             return line[7..$-3].strip().to!(int);
         }
     }
     return 0;
}

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;
     foreach(i; 0..total)
     {
         foreach (j; 0..1000)
         {
             x ~= [0.01, 0.02, 0.03];
         }
     }

     int after = MemoryUsage();
     writefln("%dK * float[3] = %d Kbytes", total, (after-before));
}
-------------------8<--------------------

It prints:
$ ./memory
float = 4 bytes
float[3] = 12 bytes
100K * float[3] = 2356 Kbytes

Why not 1200 Kbytes?


More information about the Digitalmars-d mailing list