21147 C# ---Understanding
Managed Heap
-----------------------------------------------------------------------------------------
The managed Heap is an area in virtual memory that is available for your program, that Common Language Runtime     uses for storing instances of reference type
objects that are created . The original object is stored in the Managed Heap and the reference is stored in the stack. The Garbagge collector is the component
of CLR that is responsible for allocating memory on the heap for the object created and also for releasing that memory when the referece no longer exists in the
stack.There is a process called compaction that happens once the memory is freed and the heap is then arranged as a continuous chunk.


Static Data and Constants are stored on the Heap
-----------------------------------------------------------------------------------------
Static Data and constants are stored on a specific section of the Heap which is not Garbagge Collected Heap.Since both the data types exists through out the life
time of the program they need not be garbagge collected.They are stored in the Loader Heap. For Static Data specifically it is stored in the High frequency Heap. on of the Loader Heap on the AppDomain.


Memory allocation on Heap for a object and reference of objects in stack
----------------------------------------------------------------------------------------------------------------------------
Lets consider the example below
        Person person = new person("Joseph","Josephine")

Since the person object is created with the new operator,it is allocated on the heap. The reference of the object Person is created on stack , but the memory
for the object is allocated on the heap.i.e we are creating and instantiating a person object on the stack.. In the same way the value typed are also stored
on the stack but the memory is allocated on heap if an only if they exists within another object.Object reference can also be on heap if they are contained within other objects.


Value types are stored on the Heap
----------------------------------------------------------------------------------------------------------------------------
Lets consider the below example

public class Person {
        int age;
        int salary;
        string first_name ; //reference type
        string last_name ;  //reference type
       
        public Person(string first_name_args , string last_name_args)
        {
        first_name=first_name_args;
        last_name=last_name_args
        }
         }   

Public class Main{
    public static void Main()
    {
    Person person1=new Person("Joseph","Albert"); //creation of new object with first name "Joseph" and second name ="Albert"
}



In the above example the object person1 is allocated a memory in heap.Normally value types like 'age','salary' are created on the stack.
but since they are encapsulated within an object which is created using the new operator. so the space for the is allocated within heap
but a  reference pointer is maintained in the stack for the object.

Memory for Objects created on a stack is allocated when a method is called or when the object is instantiated.The memory is released when
the method that instantiated the object exits

Memory for Object created on a heap is allocated when the object is instantiated and managed by CLR garbagger collector , which frees memory periodically


|-------------------------|                                       |-----------------------|
|                         |                                       |                       |
|                         |                                       |                       |
|                         |                                       |      H E A P    |
|                         |                                       |                       |
|       Stack         |                                       |-----------------------|
|-------------------------|   reference to object      |    "Joseph"      |
|    person1        |  ---------------->                   |----------------------|
|-------------------------|                                       |      "Al bert"   |
|                         |                                       |-----------------------|
|                         |                                       |    age =24      |
|                         |                                       |-----------------------|
|                         |                                       |salary=10000|                                                                   |-----------------------|                        |        
|-------------------------|





Deep Copy and Shallow Copy
-------------------------------------------------------------------------------------
Shallow Copy - In shallow copy all the data members are copied byte by byte of the original object.But if a parent object contains another object then the copied
object will contain a reference to the object instead of a new sub object.Here the problem is both the objects(copied from and copied to) are being referred to
the same sub object. So is the sub object is changed , then that gets reflected in all the top level object referring it.

Deep Copy
But in case deep copy a new sub copy is created and is referred by the copied object.


Deep Copy representation
------------------------------------------------------------------------------------------------------------


Visual representation for Deep Copy

Shallow Copy  
------------------------------------------------------------------------------------------------

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6