90

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#.

2

3 Answers 3

101

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.

19
  • 1
    I wonder why 64-bit .net would have used a pointer, rather than an scaled offset into an "objects" table? A 32-bit scaled offset should have been sufficient for most applications (applications which need more than four BILLION objects could use a special "superHumungous" memory model), and I would think that the small extra cost of using scaled displacement addressing would be more than made up for by the improved cache concurrency that would result from making object references half as big.
    – supercat
    Commented Dec 10, 2011 at 19:46
  • 5
    @supercat, .NET developers can choose to compiler their app to 32-bit mode and all pointers will be 32-bit. This is probably best for any application that doesn't need 64-bit pointers, which is the vast majority of applications. Commented Dec 11, 2011 at 2:55
  • 2
    @supercat, there are many conflicting posts about 32-bit vs 64-bit performance. Here's one that strongly favors 32-bit. blogs.msdn.com/b/ricom/archive/2009/06/10/… Commented Dec 11, 2011 at 5:22
  • 2
    @SamuelNeff: While the original question could be answered "4 bytes on x86; 8 bytes on x64", that question in and of itself wouldn't be very interesting or insightful. Knowing what the performance implications are of 8-byte object references seem somewhat more insightful.
    – supercat
    Commented Dec 11, 2011 at 16:36
  • 2
    @ChrisBordeman what makes you think that? The OP says object reference and not object. The OP marked this answer as correct. Commented Jul 9, 2017 at 23:33
23

For determining pointer size, you can use System.Runtime.InteropServices.Marshal.SizeOf(typeof(IntPtr)), or sizeof(IntPtr) in unsafe context.

Edit:

Or IntPtr.Size.

17

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.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.