Memory footprint of multi-dimensional arrays

Ben Mitchell Arisian at gmail.com
Fri Apr 18 09:04:17 PDT 2008


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



More information about the Digitalmars-d mailing list