sbyte, int16, in32 all the same?

  • Thread starter Thread starter Steve - DND
  • Start date Start date
S

Steve - DND

After downloading and using two different memory profiling applications
today, it seems that sbyte, int16, and int32 are all stored using 12b of
memory. Someone please tell me I'm reading these memory profilers wrong! If
I'm really reading it correctly, then what's the point of using anything
other than a 32 bit integer? If they're all going to be stored the same way,
then I can't achieve any memory savings(which I was really hoping for) by
using a short and an sbyte to store my necessary key/value pairs. Is there
any way to have these values stored as true 1b, 2b, or 4b datatypes?

Thanks,
Steve
 
The minimum size of an object is 12 bytes, so you are right, both the key and value pair will occupy 12 bytes each irrespective the
type used. Note however that a minimum size object can hold 4 sbytes or 2 shorts or 2 bytes and a short.

Willy.
 
Thanks for the response, but I'm a little confused by the last sentence. Are
you saying that if I instantiate 4 sbytes(or bytes), that they will be
stored in the same 12 byte block of memory? If so does .Net do some kind of
automatic array creation behind the scenes to store these?

Thanks,
Steve


Willy Denoyette said:
The minimum size of an object is 12 bytes, so you are right, both the key
and value pair will occupy 12 bytes each irrespective the
 
No the CLR (current version !!) will lay-out the object as four bytes stored into a single 4 byte word (an int on 32 bit machines).

So an instance of the class:
class Foo {
sbyte b1;
sbyte b2;
sbyte b3;
sbyte b4;
}

will occupy 12 bytes on the heap;
- an int used as sync block index,
- an int used as Method table pointer
- and the real object state, an int holding the four sbytes.

Willy.
 
Back
Top