Bob said:
Hi,
I have a newbie question. What is the smallest type object size? I am
doing some calculation with individual bits and want to minimize memory
usage. I though bool might do it but it seems like each bool is a
byte, instead of a bit. Is bool the smallest possible type?
Thanks,
Bob
It depends on what you mean with "object" size, the smallest *Reference type* size type is
an int.
byte b = 1;
object o = b;
here, o will take 12 bytes (on X86), 8 bytes object header plus 4 bytes for the byte value.
If you mean primitive type size, then by definition the smallest type is a byte, but again
it highly depends on where and how they are allocated.
void Foo()
{
byte b;
int i;
...
In above sample, both b and i will be stored on the stack, both i and b will occupy a slot
on the stack( Int32 on x86).
void Foo()
{
byte b;
int i;
byte b2;
byte b3,
byte b4;
In above, the four bytes will occupy a single slot (4 bytes on X86) just like i. so here the
same amount of memory is used as the previous sample.
class Foo{
byte b;
int i;
byte b2;
byte b3,
byte b4;
}
class Bar {
byte b;
int i;
}
Same here, An instance of Foo or Bar will occupy the same amount of memory, that is, 8
bytes header plus 8 bytes for the data members.
Conclusion, it makes little sense to use a smaller value than an int if you can't combine a
number of these small integer values.
Willy.