What is the size of an object reference in .NET? Does it vary between x86, x64, and/or AnyCPU compilation?
If it makes a difference, I'm interested in C#.
References have a typical size of 4 bytes on 32-bit platforms and on 64-bits platforms with heap boundary less than 32Gb (-Xmx32G), and 8 bytes for this boundary above 32Gb.
A reference consumes the native word size of the platform it is running on. That is, 32-bit: 32 bits. 64-bit: 64 bits.
An object reference is basically a pointer to the memory that contains the object's attributes. As such the reference is one processor word in length - 32 bits on 32 bit platforms and 64 bits on x64. Thanks for contributing an answer to Stack Overflow!
Each reference to an object requires an extra four or eight bytes, depending on whether the .NET runtime is running on a 32- or 64-bit platform.
A reference is implemented as a pointer, so in an application that runs in x86 mode (32 bit), a reference is four bytes, and in x64 mode (64 bit), a reference is eight bytes. As the reference is just a pointer to an object, the reference is the same size regardless of what it is pointing to, or even if it doesn't point to anything at all (null).
From C# 5.0 in a Nutshell: The Definitive Reference in page 22; Reference types require separate allocations of memory for the reference and object. The object consumes as many bytes as its fields, plus additional administrative overhead.
The reference itself is basically a pointer. 32 bits on a 32 bit OS, 64 bits on a 64 bit OS.
The size of the object that's referenced is more complicated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With