C # difference between heap and stack

Source: Internet
Author: User

Understanding heap and stack is helpful for understanding memory management, garbage collection, errors and exceptions, debugging and logs in. net. The garbage collection mechanism frees programmers from complicated memory management. Although the vast majority of C # programs do not require programmers to manually manage the memory, however, this does not mean that programmers do not need to know how the allocated objects are recycled. In some special cases, programmers still need to manually manage the memory.

On a 32-bit processor, the virtual memory of each process is 4 GB ,. net will open three blocks of memory in these 4 GB memory blocks, respectively as stack, managed stack, and unmanaged Stack

Heap ):

The heap is allocated from the bottom up, so the space used is in free space. All referenced objects in C # are allocated on the managed heap, And the managed heap is allocated continuously in the memory, in addition, the release of memory objects is managed by the garbage collection mechanism, which is much less efficient than the stack.

STACK ):

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, C # All reference values and reference types are allocated to the stack. The stack allocates and releases memory objects in sequence based on the principle of "first-in-first-out.

Allocation and destruction of object memory:

When an instance object of a class is created, different Members of this object are allocated to different memory regions by category, and pointer of value type and reference type are allocated to the stack, instance objects of the reference type are allocated to the managed stack, and static members are allocated to the Global Data zone. The pointer on the stack points to the object on the stack. After an object is used up, the reference is disconnected from the actual object, causing the object to hibernate. Because the stack is self-maintained, its memory management can be completed through the operating system. At this time, the hibernation objects on the stack need to be recycled through the garbage collector (GC) using certain algorithms, releases the memory occupied by the object.

Deep copy and shortest copy in C #

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

Shortest copy: Also known as Shadow Clone, only copies the reference of all non-static value type members and all reference type members in the original object, that is, the original object and the new object share the object instances of all reference type members. (That is, only the Members on the stack are copied.)

Note: No matter whether it is a deep copy or a small copy, members in the global data zone will not be copied because the Members in the global data zone are static members and belong to a certain class but not to instance objects of the class, therefore, data cannot be copied.

Deep copy in C # can be implemented through the icloneable interface, but avoid inheriting the icloneable interface by the type if the icloneable interface is not required. This will force all subclasses to implement the icloneable interface. Otherwise, new members of the subclass cannot be overwritten by the type of deep copy.

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.