The difference between memory stack and heap C# C# heap and stack

C# heap and stack

Understanding heap and stack is very helpful for understanding memory management, garbage collection, errors and exceptions, debugging and logging in .NET. The mechanism of garbage collection frees programmers from complex memory management. Although most C# programs do not require programmers to manage memory manually, this does not mean that programmers do not need to understand how allocated objects are collected. , in some special occasions, the programmer still needs to perform memory management manually.

On a 32-bit processor, the virtual memory of each process is 4GB, and .NET will open up 3 memory blocks in this 4GB memory block, which are used as stack, managed heap, and unmanaged heap respectively.

heap:

The heap is allocated from bottom to top, so the used space is below the free space. All reference type objects in C# are allocated on the managed heap. The managed heap is allocated continuously in memory, and the release of memory objects is subject to the garbage collection mechanism. The management efficiency is much lower than that of the stack.

stack:

The stack is filled from top to bottom, that is, the high memory address points to the low memory address, and the memory allocation is continuous. All the references of value types and reference types in C# are allocated on the stack, and the stack is based on the principle of last in, first out. , which in turn allocate and free memory objects.

Object memory allocation and destruction:

When an instance object of a class is created, different members of the object are allocated to different memory areas by category, pointers of value types and reference types are allocated to the stack, and instance objects of reference types are allocated to the managed heap. Static members are allocated to the global data area. At this point, the pointer on the stack will point to the object on the heap. When the object is used up, the reference is disconnected from the actual object, causing the object to hibernate. Because the stack is self-maintaining, its memory management can be done by the operating system. At this time, the hibernating objects on the heap need to be recycled by the garbage collector (GC) using a certain algorithm to release the memory occupied by the objects.

Deep copy and shallow copy in C#

Deep copy : Also known as deep clone, it is completely new object generation, not only copying all non-static value type members, but also copying all reference type members of the actual object. ( that is, the members on the stack and the heap are copied )

Shallow copy : also known as shadow clone, only copies all non-static value type members and references of all reference type members in the original object, that is, the original object and the new object share all the object instances of the reference type members. ( ie only copy the members on the stack )

Note : No matter whether it is a deep copy or a shallow copy, the members of the global data area will not be copied, because the members of the global data area are static members, which belong to a certain class and do not belong to the instance objects of the class, so they cannot be copied.

Deep copying in C# can be achieved by implementing the ICloneable interface, but in cases where it is not necessary to implement the ICloneable interface, type inheritance of the ICloneable interface should be avoided. Because doing so will force all subclasses to implement the ICloneable interface, otherwise new members of the subclass will not be overridden by deep copies of the type.

Understanding heap and stack is very helpful for understanding memory management, garbage collection, errors and exceptions, debugging and logging in .NET. The mechanism of garbage collection frees programmers from complex memory management. Although most C# programs do not require programmers to manage memory manually, this does not mean that programmers do not need to understand how allocated objects are collected. , in some special occasions, the programmer still needs to perform memory management manually.

On a 32-bit processor, the virtual memory of each process is 4GB, and .NET will open up 3 memory blocks in this 4GB memory block, which are used as stack, managed heap, and unmanaged heap respectively.

heap:

The heap is allocated from bottom to top, so the used space is below the free space. All reference type objects in C# are allocated on the managed heap. The managed heap is allocated continuously in memory, and the release of memory objects is subject to the garbage collection mechanism. The management efficiency is much lower than that of the stack.

stack:

The stack is filled from top to bottom, that is, the high memory address points to the low memory address, and the memory allocation is continuous. All the references of value types and reference types in C# are allocated on the stack, and the stack is based on the principle of last in, first out. , which in turn allocate and free memory objects.

Object memory allocation and destruction:

When an instance object of a class is created, different members of the object are allocated to different memory areas by category, pointers of value types and reference types are allocated to the stack, and instance objects of reference types are allocated to the managed heap. Static members are allocated to the global data area. At this point, the pointer on the stack will point to the object on the heap. When the object is used up, the reference is disconnected from the actual object, causing the object to hibernate. Because the stack is self-maintaining, its memory management can be done by the operating system. At this time, the hibernating objects on the heap need to be recycled by the garbage collector (GC) using a certain algorithm to release the memory occupied by the objects.

Deep copy and shallow copy in C#

Deep copy : Also known as deep clone, it is completely new object generation, not only copying all non-static value type members, but also copying all reference type members of the actual object. ( that is, the members on the stack and the heap are copied )

Shallow copy : also known as shadow clone, only copies all non-static value type members and references of all reference type members in the original object, that is, the original object and the new object share all the object instances of the reference type members. ( ie only copy the members on the stack )

Note : No matter whether it is a deep copy or a shallow copy, the members of the global data area will not be copied, because the members of the global data area are static members, which belong to a certain class and do not belong to the instance objects of the class, so they cannot be copied.

Deep copying in C# can be achieved by implementing the ICloneable interface, but in cases where it is not necessary to implement the ICloneable interface, type inheritance of the ICloneable interface should be avoided. Because doing so will force all subclasses to implement the ICloneable interface, otherwise new members of the subclass will not be overridden by deep copies of the type.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325117344&siteId=291194637