The difference between C # stack and heap talking about _c# tutorial

Source: Internet
Author: User
Tags garbage collection shallow copy

Understanding heap and stack for understanding. NET has great help in memory management, garbage collection, errors and exceptions, debugging, and logging. The mechanism of garbage collection 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 programmers do not need to know how the allocated objects are recycled, and that programmers are still required to manually manage memory on special occasions.

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

Heap (heap):

The heap is distributed from bottom to top, so the space that has been used is under free space, in C # all referenced types of objects are allocated on the managed heap, the managed heap is continuously allocated on memory, and the release of memory objects is managed by the garbage collection mechanism, which is less efficient than Yu Silai.

Stacks (Stack):

The stack is populated from the top down, that is, the high memory address to the low memory address, and the memory allocation is contiguous, C # All value types and reference types of references are allocated on the stack, the stack based on the LIFO principle, the allocation and release of memory objects.

Allocation and destruction of object memory:

When an instance object of a class is created, different members of this object are assigned to different memory regions by category, and pointers to value types and reference types are assigned to the stack, and instance objects of reference types 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, the connection between the reference and the actual object 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 object on the heap needs to use a certain algorithm to recycle the object's occupied memory through the garbage collector (GC).

Deep copy and shallow copy in C #

Deep copy: Also known as deep cloning, it is entirely the creation of new objects, not only copy all Non-static value type members, but also copy all reference type members of the actual object. (That is, the members on the stack and on the heap replicate)

Shallow copy: Also known as Shadow cloning, only copies 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 an object instance of all reference type members. (That is, only members on the stack are copied)

Note: Members of the global data area are not replicated in both deep and shallow copies, because the members of the global data area are static members and belong to a class that is not part of the class's instance object and cannot be replicated.

Deep copies in C # can be implemented by implementing the ICloneable interface, but you should avoid type inheritance icloneable interfaces without having to implement the ICloneable interface. Because doing so will force all subclasses to implement the ICloneable interface, the new members of the subclass will not be overwritten by a deep copy of the type.

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.