C # base: DESCRIPTION characteristics and differences in the stack and heap .NET

I. Introduction

.NET provides garbage collection mechanism, the programmer is freed from memory management. But this does not mean that programmers need to understand how the object allocation is being recycled. More importantly, some unmanaged resources still need programmers careful allocation and recovery.

Understand the basis for understanding the heap and stack memory management. Each .NET programs will eventually run on an operating system process, it is assumed that the operating system is the traditional 32-bit .NET program that each can have a 4GB of virtual memory. .NET will open up as a stack of three memory respectively, by the managed and unmanaged stack in the heap memory block of 4GB.

Two, .NET stack in

.NET objects stored in the stack to a value type and reference type reference object, stack allocation is continuous, in the .NET program is always stored in a special pointer to the tail of the stack, so that a stack memory allocation begin dispensing downwardly from the pointer to the memory location. The figure below shows the distribution of .NET stack.

As shown above, the address on the stack from the high to low memory allocation, a .NET only the stack pointer stored at a memory address of memory unallocated. For all objects to be assigned, sequentially allocated to the stack, release the stack also in strict accordance with the logic, de-stacked sequentially. Here the reference to "order" means in accordance with the scope of the variable. Consider the following code:

ClassA a = new ClassA (); 
a.intA = 1 ; 
a.intB = 2 ;

ClassA is assumed here that a reference type, then the need to turn the stack is allocated to a reference, a.intA and a.intB. When the end of a scope, these three variables from the stack and then click Exit: a.intB, a.intA, then is a.

Three, .NET in managed heap

Next we look at the managed heap. .NET reference type object is managed heap allocated. Usually we say in .NET stack, refers to the managed heap. And stack as the managed heap is an area of ​​process memory space. But the managed heap and stack memory allocation, but there is a big difference. Benefit from the .NET memory management, managed heap allocation is also continuous, but there is a heap can not be allocated but has been temporarily useless object memory block. When a reference type object is initialized, it will be assigned a contiguous memory space available by pointing a pointer to the heap, then this points to a block of memory heap references on the stack. The following figure shows the heap allocation.

As shown above, the program to find the object instance in the managed heap allocated by reference distribution in the stack. When referring to the stack area out of scope, it is broken reference only and the actual object links. When the managed heap memory is not enough, .NET begin garbage collection. Garbage collection is a very complex process, which involves not only releases the managed heap objects, but also need to move the merger managed heap memory block. When garbage collection, objects are not used in the heap will be partially released, but before that, they are in a heap is temporarily unavailable.

Four, .NET unmanaged heap

.NET program also includes unmanaged heap, all unmanaged resources need to be allocated heap memory will be assigned to the unmanaged heap. Unmanaged heap programmers need to manually assign pointer and manually release, .NET garbage collection and memory management system does not apply to unmanaged heap.

Fifth, stack, heap and unmanaged hosting compare heap

Stack, heap and managed heap allocated unmanaged own characteristics. Stack memory is allocated consecutively, followed by distribution and release in accordance with the scope. Stack mechanism is very simple, .NET relies on a stack pointer memory operation can be carried out, an object allocation and release most of the operations of an object is to increment or decrement the stack pointer. Value type and reference type of the object in the object reference .NET is allocated within the stack.

While the managed heap memory allocation is continuous, but it is much more complicated than the stack. A heap memory allocation need to involve internal operations of many .NET memory management mechanism, and the other when the heap memory is not enough, execution cost of garbage collection is very large. Stack it with respect, much lower heap allocation efficiency. .NET reference type object is managed heap allocated, these objects to be accessed by reference allocated on the stack.

非托管堆和托管堆的区别在于非托管堆不受.NET的管理。非托管堆的内存是由程序员手动分配和释放的,垃圾回收机制不适用于非托管堆,内存块也不会被合并移动,所以非托管堆的内存分配按块的,不连续的。

六、总结

.NET程序在进程内存中分配出堆栈、托管堆和非托管堆。所有的值类型对象和引用类型对象的引用都分配在堆栈上,堆栈根据对象的生存周期来依次分配和释放,堆栈根据一个指向栈尾的指针来分配内存,效率很高。

.NET所有的引用类型对象分配在托管堆上,托管堆连续分配内存,并且受.NET的垃圾收集机制管理,受托管堆的内存分配和释放涉及复杂的内存管理,效率相对于堆栈来说低得多。

需要分配堆内存的非托管类型将被分配在非托管堆上,非托管堆不受.NET垃圾收集机制管理,内存块完全由程序员手动申请和释放。

Guess you like

Origin www.cnblogs.com/dotnet261010/p/12330112.html