Memory footprint of multi-dimensional arrays

BCS BCS at pathlink.com
Fri Apr 18 09:15:45 PDT 2008


Ben Mitchell wrote:
> So, I was watching my D program eat up ~1GB of ram doing something my back of the envelope calculation said should only be taking a few hundred MB max, and I started playing around.  Turns out that if I do:
> 
> ubyte[][] b = new ubyte[][](3, 10485760);
> 
> It takes about 30MB of main memory, as would be expected.  If, on the other hand, I reverse the order of the indexing and do:
> 
> ubyte[][] b = new ubyte[][](10485760, 3);
> 
> It takes about 250MB of main memory.  This seems totally absurd to me; I can only assume this is because it's an array of (potentially arbitrary size) dynamic arrays, and there's ~220MB of bookkeeping information being stored.   Is there any way around this?  Can I somehow tell the compiler it's a symmetric array?  Can I make a dynamic array of static arrays of size 3?  I can reverse the indexing if I have to, but it would make the rest of my code very counter-intuitive, and it just doesn't seem like it should make an order of magnitude difference in memory usage.
> 
>    -  Ben

one (kinda ugly) solution would be to allocate the whole thing as a 
single huge ubyte[] array and slice out the 1M 3 byte arrays:


auto tmp = new ubyte[](3*10485760 +  5);

ubyte[][] b;
b.length = 10485760;
foreach(uint i, ref bp; b) bp = tmp[i*3.. (i+1)*3];

A little template magic might be able to do this recursively for nested 
dynamic arrays



More information about the Digitalmars-d mailing list