Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Object Size Overhead

Tags:

c#

c#-4.0

I am working on optimization of memory consuming application. In relation to that I have question regarding C# reference type size overhead.

The C# object consumes as many bytes as its fields, plus some additional administrative overhead. I presume that administrative overhead can be different for different .NET versions and implementations.

Do you know what is the size (or maximum size if the overhead is variable) of the administrative overhead for C# objects (C# 4.0 and Windows 7 and 8 environment)?

Does the administrative overhead differs between 32- or 64-bit .NET runtime?

like image 675
Tom Avatar asked Jan 11 '13 20:01

Tom


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

There is an article online with the title "The Truth About .NET Objects And Sharing Them Between AppDomains" which shows some rotor source code and some results of experimenting with objects and sharing them between app domains via a plain pointer.

http://geekswithblogs.net/akraus1/archive/2012/07/25/150301.aspx

  • 12 bytes for all 32-bit versions of the CLR
  • 24 bytes for all 64-bit versions of the CLR

You can do test this quite easily by adding millions of objects (N) to an array. Since the pointer size is known you can calculate the object size by dividing the value by N.

var initial = GC.GetTotalMemory(true);
const int N = 10 * 1000 * 1000;
var arr = new object[N];
for (int i = 0; i < N; i++)
{
    arr[i] = new object();
}

var ObjSize = (GC.GetTotalMemory(false) - initial - N * IntPtr.Size) / N;

to get an approximate value on your .NET platform.

The object size is actually defined to allow the GC to make assumptions about the minimum object size.

\sscli20\clr\src\vm\object.h

//
// The generational GC requires that every object be at least 12 bytes
// in size.   
#define MIN_OBJECT_SIZE     (2*sizeof(BYTE*) + sizeof(ObjHeader))

For e.g. 32 bit this means that the minimum object size is 12 bytes which do leave a 4-byte hole. This hole is empty for an empty object but if you add e.g. int to your empty class then it is filled and the object size stays at 12 bytes.

like image 185
Alois Kraus Avatar answered Oct 13 '22 01:10

Alois Kraus