Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much memory does a C# reference consume? [duplicate]

Tags:

c#

.net

reference

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

like image 442
Louie Almeda Avatar asked Oct 26 '13 11:10

Louie Almeda


People also ask

How much memory can ac program use?

This is data for which memory is being allocated while executing the program. In C++, this allocation is usually performed by the malloc function or new operator. In 32-bit programs the size of dynamically allocated memory is restricted to 2 Gbytes, in 64-bit programs to 8 Tbytes.

How many GB should I allocate for C?

-- We suggest that you set around 120 to 200 GB for the C drive. even if you install a lot of heavy games, it would be sufficient. -- Once you have set the size for the C drive, the disk management tool will start partitioning the drive.

How much memory does a pointer take C++?

Pointers take up the space needed to hold an address, which is 4 bytes on a 32-bit machine and 8 bytes on a 64-bit machine. In C++, every value is stored somewhere in memory and can therefore be identified with that address. Such addresses are called pointers.

How much memory does an object take in C#?

reference variables (refs to other objects) take 4 or 8 bytes (32/64 bit OS ?) int16, Int32, Int64 take 2,4, or 8 bytes, respectively... etc.


2 Answers

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

like image 87
Guffa Avatar answered Sep 19 '22 17:09

Guffa


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.

like image 35
Soner Gönül Avatar answered Sep 18 '22 17:09

Soner Gönül