Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is happening when I give MyObject obj = new MyObject()

MyObject obj = new MyObject();

I know that 'new' keyword will call the constructor and initialize the object in managed heap.

My question is how the CLR doing the below.

  1. How the above line is executed by CLR?
  2. How the memeory is allocated for the object by CLR?
  3. How CLR determine the size of the object?
  4. How CLR will come know it, if there is no space to allocate memory for the object from the heap?
like image 381
Syed Avatar asked Nov 03 '22 22:11

Syed


1 Answers

When a reference type is created (class , delegate, interface, string, or object), it's allocated onto the heap..Net has four different heaps: (gen0, gen1, gen2)(Small object Heap), and LOH (Large Object Heap). Everything that's 85k or smaller goes on one of the first three heaps, depending on creation time (moved from gen0 to gen1 to gen2, etc). Objects larger than 85k get placed on the LOH. The LOH is never compacted, so eventually, allocations of the type I'm doing will eventually cause an OOM error as objects get scattered about that memory space. These are known as managed heaps.

To create an object, all you need to do is use the new keyword; .NET will take care of creating, initializing and placing the object on the right heap, and reserving any extra memory necessary. After that you can pretty much forget about that object, because you don't have to delete it when you're finished with it.

when you create a reference type object using new keyword it is placed on heap and its reference is used mostly in current running stack. there are other possible sources where your object could be used as refrence:

  1. global/static object references
  2. CPU registers
  3. object finalization references (more later)
  4. Interop references (.NET objects passed to COM/API calls)
  5. stack references(mostly used here)

These 5 are actually GC root nodes from where object reference hierarchy is formed.Imagine the classic Customer class, which usually has a collection storing Order classes. When an Order is added to the order collection, the collection itself then holds a reference to the added order. If the instance of the Customer class had a stack reference to it as well.

this is how hierarchy is formed for a complex object, this is how GC see the references.

e:g. stack-reference for Customer object -> reference to List object of orders -> reference individual orders.

anything that loose reference from these 5 roots is prone to GC.

how memory is allocated to objects is bit complex and it usally grow by time as MKK as specified http://msdn.microsoft.com/en-us/magazine/cc163791.aspx

a simple example could be:

class MyClass 
{ 
    string Test="Hello world Wazzup!"; 
    byte[] data=new byte[86000];  
}

It's easy to assume that the size of MyClass when allocated includes:

• 19 characters

• 86,000 bytes.

In fact, the object's size will only include general class stuff, and the memory required to store the object pointers to the string and the byte array (class level variables), which are then separately allocated onto the heaps. The string will be allocated on the SOH, and its object reference held by the instance of the class; the byte array will be allocated onto the LOH, as it's bigger than 85 KB.

SOH can be compacted and there will be no fragmentation in it, while LOH can have fragmentation in memory.

Regards.

like image 176
Shoaib Shaikh Avatar answered Nov 15 '22 00:11

Shoaib Shaikh