[You must know. Net] 18th back: Object creation beginning and end (on)

Source: Internet
Author: User
Blog Park CLR basic research team | CLR team excellent series | anytao technology blog

 

[You must know. Net]18th:Object creation beginning and end (on)

Released on: 2007.12.3 by anytao
2007 anytao.com: original works. Please refer to the author and source for the post.

 

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, used to allocate small object instances. If the instance size of the reference type object is smaller than 85000 bytes, the instance will be allocated in GC stacked, when memory is allocated or recycled, the Garbage Collector may GC the heap is compressed. For more information, see the following description.

· Loh ( Large Object heap ) Heap, used to allocate large object instances. If the instance size of the reference type object is not smaller 85000 Byte, the instance will be allocated Loh Stack, and Loh The heap is not compressed, and is only GC It is recycled.

This article focuses on . net memory allocation mechanism, therefore, the following is not described with GC the allocation on the heap is shown as an example. For more information about the value type and reference type, see [ Eighth: 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.

· ldstr , used to create string type 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 thread stack initialization address is 50000 Therefore, the stack pointer first points 50000 Address space.CodeBy the entry function Main Start execution. First, the integer local variable is entered into the scope. X , It will be allocated on the stack 4 byte So the stack pointer moves down 4 Bytes, then the value 100 Will be saved in 49997 ~ 50000 Unit, while the stack pointer indicates the next free space address is 49996 ,:

Next, enter the next line of code, which will be a variable of the variable type. C Allocate 2 byte Memory space, stack pointer move down 2 Bytes 49994 Unit, Value 'A' Will be saved in 49995 ~ 49996 Unit: Address Allocation

Finally, run Main The right parenthesis of the method. The method body is executed and the variable is X And C And the scope of the variable needs to be deleted. X And C The release process and allocation process of values in the stack memory are the opposite: Delete the values first. C Memory, stack pointer increments upwards 2 Bytes, and then delete X Memory, stack pointer continues to increase upwards 4 Bytes,ProgramThe execution is complete. The memory status is as follows:

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 duy, professinal. 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
 

Learn more

[Opening useful]
[First: resentment: Is and as]
[Second: Abstract programming: interfaces and abstract classes]
[Third: Historical entanglement: Characteristics and attributes]
[Forth: Back-to-Back: class and struct]
[Fifth: Let's look at the keywords-New]
[Sixth: Let's look at the keywords-base and this]
[7: taste type-starting from general type system]
[Eighth time: taste type-value type and reference type (top)-memory rational]
[Ninth: taste type-value type and reference type (medium)-unlimited Rules]
[10: taste type-value type and reference type (lower)-Application journey]
[11th: confusing parameters-art of transmission (I)]
[12th: confusing parameters-art of transmission (lower)]
[13th back: Starting from Hello, world to know Il]
[14th: Get to know the Il code-from the beginning to now]
[15th back: Inheriting the essence]
[16th]
[17th]

2007 anytao.com

For original works, please refer to the author and source for the post. Leave this information.

This document is provided as "the status quo" without any guarantee and does not grant any rights.
This posting is provided "as is" with no warranties, and confers no rights.

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.