William Stacey [MVP] <st***********@ mvps.org> wrote:
Yeh. However is it not true that because of alignment, the smallest thing
you can allocate is a 4 byte int. So I "think", internally, it is an int.
Not sure, but thought I came across this once. Please correct if in error
here. Cheers!
Well, I would certainly hope that an array of bools (or bytes) wouldn't
end up being an array of ints - everything which had to load an array
of bytes would end up being 4 times as large as it needed to be!
Bytes and bools at least are also packed, rather than aligned. This
program demonstrates that for bytes - just find and replace byte with
bool to see the same for bools. (They're only packed to one per byte
though, not one per bit!)
using System;
public class Test
{
byte b0;
byte b1;
byte b2;
byte b3;
byte b4;
byte b5;
byte b6;
byte b7;
byte b8;
byte b9;
byte b10;
byte b11;
byte b12;
byte b13;
byte b14;
byte b15;
byte b16;
byte b17;
byte b18;
byte b19;
byte b20;
byte b21;
byte b22;
byte b23;
byte b24;
byte b25;
byte b26;
byte b27;
byte b28;
byte b29;
byte b30;
byte b31;
byte b32;
byte b33;
byte b34;
byte b35;
byte b36;
byte b37;
byte b38;
byte b39;
byte b40;
byte b41;
byte b42;
byte b43;
byte b44;
byte b45;
byte b46;
byte b47;
byte b48;
byte b49;
static void Main()
{
Test[] ta = new Test[100000];
long orig = GC.GetTotalMemo ry(true);
for (int i=0; i < ta.Length; i++)
ta[i]=new Test();
long final = GC.GetTotalMemo ry(true);
Console.WriteLi ne ("Total use={0}. Per object={1}", final-orig,
(final-orig)/ta.Length);
GC.KeepAlive(ta );
}
}
The usage is 60 bytes per object which is what I'd expect: the space
for the bytes is 50, plus 8 for the object header, rounded up to the
nearest alignment.
--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too