The difference between a memory stack and a heap C #

Source: Internet
Author: User
Tags shallow copy

C # heap and Stack

Understand heaps and stacks for understanding. NET memory management, garbage collection, errors and exceptions, debugging and logging is of great help. The garbage collection mechanism frees programmers from complex memory management, although the vast majority of C # programs do not require programmers to manually manage memory, but this does not mean that the programmer does not need to know how the allocated objects are recycled, and in some special cases the programmer still needs to do the memory management manually.

On a 32-bit processor, the virtual memory for each process is 4GB. NET will open up 3 blocks of memory in this 4GB memory block, respectively, as stacks, managed heaps, and unmanaged heaps

Heap:

The heap is allocated from the bottom up, so the used space under free space, all objects of reference types in C # are allocated on the managed heap, the managed heap is continuously allocated in memory, and the release of the memory objects is managed by the garbage collection mechanism, which is much less efficient than Yu Silai.

Stacks (Stack):

The stack is populated from the top down, that is, the high memory address points to the low memory address, and the memory allocation is contiguous, all the value types and reference type references in C # are allocated on the stack, and the stack allocates and frees the memory objects in turn, based on the principle of LIFO.

Allocation and destruction of object memory:

When an instance object of a class is created, different members of the object are assigned to different memory regions by category, pointers to value types and reference types are allocated on the stack, instance objects of the reference type are assigned to the managed heap, and static members are assigned to the global data area. The pointer on the stack points to the object on the heap. When the object is used up, the reference and the actual object's contact is broken, thus hibernating the object. Because the stack is self-sustaining, its memory management can be done through the operating system, and the hibernating objects on the heap need to be reclaimed by the garbage collector (GC) using a certain algorithm to free up the memory occupied by the object.

Deep copy and shallow copy in C #

deep copy : Also known as deep cloning, it is a complete creation of new objects, not only copying all non-static value type members, but also copying the actual objects of all reference type members. (That is, the members on the stack and on the heap are copied )

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

Note : Both deep and shallow copies do not replicate the members of the global data area because the members of the global data zone are static members, belong to a class, do not belong to the class's instance object, and therefore cannot be copied.

Deep copies in C # can be implemented by implementing the ICloneable interface, but the type inheritance ICloneable interface should be avoided in cases where the ICloneable interface must not be implemented. Because doing so will force all subclasses to implement the ICloneable interface, the new members of the subclass will not be overwritten by the deep copy of the type.

The difference between a memory stack and a heap C #

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.