Referenced from:Http://www.cnblogs.com/anytao/category/155694.html
Author:Anytao
This article introduces the following:
-
- Object creation process
- Memory Allocation Analysis
-
- Research on memory Layout
1.Introduction
Understanding. NetThe memory management mechanism should begin with memory allocation, that is, the process of object creation. Object creation is a complex process, mainly including memory allocation and initialization. For example, the object creation process can be expressed:
FilestreamFS =New Filestream(@ "C:" temp.txt",Filemode. Create );
PassNewKeyword operation.FilestreamThe creation process of the type object, behind this seemingly simple operation, has experienced a very complex process and twists and turns.
The full text of this article is a detailed discussion of the process behind this operation.. NetHow is the memory allocation implemented?
2.Memory Allocation
For memory allocation, you should first know where the allocation is.CLRThe memory management area consists of three parts:
·Thread stack, used to allocate value-type instances. The stack is mainly managed by the operating system and is not controlled by the garbage collector. When the method of the value-type instance ends, its storage unit is automatically released. Stack execution efficiency is high, but the storage capacity is limited.
· GC heap for allocating small object instances. If the instance size of the reference type object is smaller than 85000 bytes, the instance is allocated to the GC stack. When memory is allocated or recycled, the Garbage Collector may compress the GC heap. For more information, see the following description.
· LOH ( large object heap ) heap for allocating large object instances. If the instance size of the reference type object is no less than 85000 bytes, the instance will be allocated to the LOH stack, the LOH heap is not compressed, it is recycled only when GC is completely recycled.
The focus of this article is. NetThe memory allocation mechanism.GCThe allocation on the stack is shown as an example. For more information about the value type and reference type, see[Back 8: taste type---Value Type and reference type (top)-memory rational].
After learning about the memory allocation area, let's see which operations will lead to object creation and memory allocation.IlCommand Parsing, mainly including:
·NewobjTo create a reference type object.
·LdstrUsed to createStringType object.
·NewarrTo assign a new array object.
·BoxWhen the value type is converted to a reference type object, copy the value type field to the memory allocation that occurs on the managed stack.
Based on the above discussion, we will discuss the memory allocation of stacks and the memory allocation of managed stacks separately.. NetMemory Allocation Mechanism.
2.1Stack memory allocation mechanism
Value types are generally created on the thread stack. However, not all value types are created on the thread stack. For example, when a field of the class is used, the value type as part of the Instance member is also created on the managed stack. When a packing occurs, the Value Type field is also copied to the managed stack.
For local variables distributed on the stack, the operating system maintains a stack pointer pointing to the next free space address, and the memory address of the stack is filled down from the high to the low. For example:
Public Static Void Main ()
{
Int X = 100;
Char C = 'a ';
}
assume that the initialization address of the thread stack is 50000 , therefore, the stack pointer first points to the 50000 address space. Code is executed by the main entry function, first, the scope is the integer local variable x , it allocates 4 bytes of memory on the stack, therefore, the stack pointer moves down 4 bytes, the value 100 is saved in 49997 ~ 50000 , and the next free space address represented by the stack pointer is 49996 ,:
enter the next line of code, the memory space of C is allocated for the variable 2 bytes , stack pointer moves down 2 bytes to 49994 unit, the value 'A' is stored in 49995 ~ 49996 unit, Address Allocation
last, execute to the right brackets of the main method, and the execution of the method body ends, the scope of variable x and C also ends, you need to delete the values of x and C in the stack memory, the release process and allocation process are the opposite: First Delete the memory of C , the stack pointer is incremented by 2 bytes, delete the x memory, the stack pointer continues to increase upwards by 4 bytes, And the Program execution ends, the memory status is:
other complicated allocation processes may vary in scope and allocation size, but the basic process is similar. The memory allocation on the stack is highly efficient, but the memory capacity is not large. At the same time, the life cycle of the variable disappears as the method ends.
To be continued: Memory Allocation Mechanism for hosting heapAndNecessary supplementsNote: Please pay attention to the recent release.
References
(USA)Joe Duffy, mongosinal. NET Framework 2.0
(USA)Don box, essiential. net
( Msdn ) Hanu kommalapati and Tom Christian, drill into. NET Framework Internals to see how the CLR creates runtime objects, http://msdn.microsoft.com/msdnmag/issues/05/05/JITCompiler/default.aspx