How Much Memory Does a C#/.Net Object Use

How much memory does a C#/.NET object use?

You could use a memory profiler like

.NET Memory Profiler (http://memprofiler.com/)

or

CLR Profiler (free) (http://clrprofiler.codeplex.com/)

How much memory does a C# reference consume?

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

What is the memory overhead of a .NET Object

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

Size of A Class (object) in .NET

The size of a class instance is determined by:

  • The amount of data actually stored in the instance
  • The padding needed between the values
  • Some extra internal data used by the memory management

So, typically a class containing a string property needs (on a 32 bit system):

  • 8 bytes for internal data
  • 4 bytes for the string reference
  • 4 bytes of unused space (to get to the minimum 16 bytes that the memory manager can handle)

And typically a class containing an integer property needs:

  • 8 bytes for internal data
  • 4 bytes for the integer value
  • 4 bytes of unused space (to get to the minimum 16 bytes that the memory manager can handle)

As you see, the string and integer properties take up the same space in the class, so in your first example they will use the same amount of memory.

The value of the string property is of course a different matter, as it might point to a string object on the heap, but that is a separate object and not part of the class pointing to it.

For more complicated classes, padding comes into play. A class containing a boolean and a string property would for example use:

  • 8 bytes for internal data
  • 1 byte for the boolean value
  • 3 bytes of padding to get on an even 4-byte boundary
  • 4 bytes for the string reference

Note that these are examples of memory layouts for classes. The exact layout varies depending on the version of the framework, the implementation of the CLR, and whether it's a 32-bit or 64-bit application. As a program can be run on either a 32-bit or 64-bit system, the memory layout is not even known to the compiler, it's decided when the code is JIT:ed before execution.

How much memory required when Value Type is Boxed into Reference Type in C#?

First to help explain the "why", from the C# 5 Specification: Section 4.3.1 Boxing conversions

The actual process of boxing a value of a non-nullable-value-type is
best explained by imagining the existence of a generic boxing class,
which behaves as if it were declared as follows:

sealed class Box<T>: System.ValueType
{
T value;
public Box(T t) {
value = t;
}
}

Boxing of a value v of type T now consists of executing the expression
new Box(v), and returning the resulting instance as a value of type
object. Thus, the statements

int i = 123;
object box = i;

conceptually correspond to

int i = 123;
object box = new Box<int>(i);

To answer if it is stored on the stack or the heap, the answer is both. See Boxing and Unboxing on the MSDN.

Consider the following declaration of a value-type variable:

int i = 123;

The following statement implicitly applies the boxing operation on the
variable i:

// Boxing copies the value of i into object o. 
object o = i;

The result of this statement is creating an object reference o, on the
stack, that references a value of the type int, on the heap. This
value is a copy of the value-type value assigned to the variable i.
The difference between the two variables, i and o, is illustrated in
the following figure. Sample Image

So on the stack there are sizeof(Object) bytes stored, and on the heap there are sizeof(int) + Class overhead stored.

I could not find any good documentation on how large that overhead is, it is most likely 8 to 16 bytes in size.

C# vb: System.Func takes how much memory?

Func<> is a delegate type, an instance takes 32 bytes of storage in the x86 jitter:

  • 8 bytes for the object header
  • 4 bytes for the Delegate._methodBase field
  • 4 bytes for the Delegate._methodPtr field
  • 4 bytes for the Delegate._methodPtrAux field
  • 4 bytes for the Delegate._target field
  • 4 bytes for the MulticastDelegate._invocationCount field
  • 4 bytes for the MulticastDelegate._invocationList field

The target method size is not relevant, the delegate object only stores the address of the method.

How much RAM C# application can use?

64 bit application, including managed ones, can access large memory.

32 bit application have a virtual address space of maximum 4GB. In practaice they get 2GB by default. /LARGEADRESSAWARE applicaitons can get 3GB with /3gb in boot.ini or 4GB when running on WoW64.

Managed application, even on x64 cannot allocate any single object larger than 2GB. Including arrays.

But no matter the amount of VA available at your disposal: manipulating a +2GB DataTable object is just not going to work. Use a storage engine capable of handling and manipulating large amounts of data fast, and capable of intelligent paging. Redis, Cassandra or even traditional RDBMSs are more suited for the job.

Even if you decide to manipulate the data directly in memory, you need a smarter format than DataTable.

How big is an object reference in .NET?

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.



Related Topics



Leave a reply



Submit