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:
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 specified http://msdn.microsoft.com/en-us/magazine/cc163791.aspx
a simple example could be:
• 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.
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:
- global/static object references
- CPU registers
- object finalization references (more later)
- Interop references (.NET objects passed to COM/API calls)
- stack references(mostly used here)
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 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];  }
• 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.
 
No comments:
Post a Comment