14

How much memory does a C# reference consume? Does References consumes memory as much as the object itself?

0

3 Answers 3

26

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

3
  • I was going to answer this but I stopped when I started to wonder that there surely must be some additional overhead memory involved in the creation and maintenance of a reference beyond the pointer itself.
    – Ashigore
    Commented Oct 26, 2013 at 12:03
  • 1
    @Ashigore: There is actually no overhead at all in a reference, it's just a pointer. Any overhead is associated with the object, like type information and garbage collection references.
    – Guffa
    Commented Oct 26, 2013 at 12:05
  • 3
    Downvoter: Why the downvote? If you don't explain what it is that you think is wrong, it can't improve the answer.
    – Guffa
    Commented Oct 26, 2013 at 12:09
15

A reference consumes the native word size of the platform it is running on.

That is, 32-bit: 32 bits. 64-bit: 64 bits.

So no.. your object can be variable in size.. the reference will always be as above.

7

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 precise overhead is intrinsically private to the implementation of the .NET runtime, but at minimum the overhead is eight bytes, used to store a key to the object’s type, as well as temporary information such as its lock state for multithreading and a flag to indicate whether it has been fixed from movement by the garbage collector. 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.

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