Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the memory overhead of a .NET Object

Tags:

c#

.net

What is the memory overhead of an Object in .NET? I'm talking about an arbitrary bare-bones object.... the overhead of the internal .NET workings or references:

var obj = new System.Object(); 

How much space does obj occupy in the heap?

like image 243
reach4thelasers Avatar asked May 18 '12 15:05

reach4thelasers


People also ask

How much memory is allocated to an object 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...

How are C# objects stored in memory?

In C# there are two places where an object can be stored -- the heap and the stack. Objects allocated on the stack are available only inside of a stack frame (execution of a method), while objects allocated on the heap can be accessed from anywhere.


1 Answers

I talk about this in a blog post "Of memory and strings". It's implementation-specific, but for the Microsoft .NET CLR v4, the x86 CLR has a per-object overhead of 8 bytes, and the x64 CLR has a per-object overhead of 16 bytes.

However, there are minimum sizes of 12 and 24 bytes respectively - it's just that you get the first 4 or 8 bytes "free" when you start storing useful information :)

(See the blog post for more information.)

like image 117
Jon Skeet Avatar answered Sep 25 '22 12:09

Jon Skeet