A brief discussion on the difference between C stack and heap

  • 2020-05-09 19:05:31
  • OfStack

Understanding the heap and stack is a great aid in understanding memory management, garbage collection, errors and exceptions, debugging, and logging in.NET. The garbage collection mechanism frees programmers from complex memory management. Although most C# programs do not require programmers to manually manage memory, this does not mean that programmers do not need to know how allocated objects are recycled. In some special cases, programmers still need to manually manage memory.

On a 32-bit processor, each process has 4GB of virtual memory, and.NET allocates three blocks of memory in the 4GB memory block as a stack, managed heap, and unmanaged heap

Heap (heap) :

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

Stack (stack) :

The stack is filled from top to bottom, that is, from the high memory address to the low memory address, and the memory allocation is continuous. All the references of the value type and reference type in C# are allocated on the stack. According to the principle of last in, first out, the stack allocates and releases memory objects in turn.

Object memory allocation and destruction:

When an instance object of a class is created, different members of the object are allocated to different memory regions by category, Pointers of value types and reference types are allocated to the stack, instance objects of reference types are allocated to the managed heap, and static members are allocated to the global data region. A pointer on the stack points to an object on the heap. When the object is used up, the connection between the reference and the actual object is broken, causing the object to hibernate. Since the stack is self-maintainable, its memory management can be accomplished by the operating system, and the hibernating object on the heap needs to be recovered by the garbage collector (GC) using a fixed algorithm to free the memory occupied by the object.

Deep and shallow copies in C#

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

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

Note: neither the deep copy nor the shallow copy will copy the members of the global data area. Because the members of the global data area are static members, they belong to a certain class and do not belong to the instance object of the class, so they cannot be copied.

The deep copy in C# can be implemented by implementing the ICloneable interface, but type inheritance of the ICloneable interface should be avoided when it is not necessary to implement the ICloneable interface. Because doing so forces all subclasses to implement the ICloneable interface, otherwise the new member of the subclass will not be overridden by a deep copy of the type.


Related articles: