. Net Memory Allocation Analysis

Source: Internet
Author: User

This article comes from: http://www.lmwlove.com/ac/ID904

When analyzing memory allocation, you should first understand the difference between Stack
Heap allocation is extended to the high address, while stack allocation is extended to the low address.

II. Memory Allocation

For memory allocation, you should first know where the allocation is. The CLR manages three areas of memory:
· 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 less than 85000 bytes, the instance will be allocated to the GC stack. When memory is allocated or recycled, the Garbage Collector may compress the GC stack, for more information, see the following description.
 
The most important information of Loader heap is the metadata-related information, that is, the type object. Each type is represented as a method table on loader heap, while the method table records the stored metadata information, for example, the base type, static field, implemented interface, and all methods. Loader heap is not controlled by GC, and its lifecycle starts from creation to appdomain uninstallation.

The sample code is as follows:

Public class vipuser: User
{
// Allocate 1 byte
Public bool isvip;
Public bool isvipuser ()
{
Return isvip;
}
Static void main (string [] ARGs)
{
// Allocate memory and perform Initialization
Vipuser auser;
// Assign the object reference to the auser variable to establish the association between auser and vipuser
Auser = new vipuser ();
// Q: number of bytes allocated for the type?
// This class requires 15 bytes. However, the total number of bytes occupied by the instance object must be added with the number of bytes required for appending members to the object, including 8 bytes in total for appending members typehandle and syncblockindex. The total number of bytes allocated on the managed stack is 23 bytes, and the memory block on the stack is always allocated in multiples of 4 bytes. Therefore, a 24-byte address space will be allocated in this class.

// Call the object constructor to initialize the object and complete the creation.

// Construction Process
// A. Construct a type object of the vipuser type, including static fields, method tables, and Implemented interfaces, and allocate it to The loader heap that hosts the heap mentioned above.

// B. initialize two additional members of auser: typehandle and syncblockindex. Point the typehandle pointer to the methodtable on the loader heap. CLR will locate the specific type based on the typehandle, and direct the syncblockindex pointer to the memory block of the synchronization block, it is used to synchronize instance objects in a multi-threaded environment.

// C. Call the user constructor to initialize the instance fields. During instance initialization, the parent class initialization is performed recursively up until the initialization of the system. Object type is completed, and then the initialization of the execution subclass is returned until the vipuser class is executed. In this example, the initialization process is to first execute the system. object class, then execute the user class, and finally the vipuser class. In the end, the memory address of the managed heap allocated by newobj is passed to the this parameter of vipuser and referenced to the auser declared on the stack.

Auser. isvip = true;
Console. writeline (auser. isvipuser ());
// The above process basically completes the entire process of creating a reference type, allocating memory, and initializing
}
}
Public class userinfo
{
// Allocate 4 bytes
Private int32 age =-1;
// Allocate 2 bytes
Private char level = 'a ';
}
Public class user
{
// Allocate 4 bytes
Private int32 ID;
// Saves the reference of userinfo to occupy 4 bytes
// Only one reference (pointer) is saved on the thread stack, occupying 4 bytes of memory space used to save the valid address of the user object. Now any operation on the user will throw nullreferenceexception
Private userinfo user;
}

· LOH (large object heap) heap is used to allocate 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, and the LOH heap will not be compressed and will only be recycled during full GC.

Before learning about memory allocation, we should first understand three concepts:
· Typehandle: Type handle pointing to the method table of the corresponding instance. Each object is created with this additional member and occupies 4 bytes of memory space. We know that each type corresponds to a method table. When a method table is created for compilation, it mainly contains the feature information of the type, the number of Implemented interfaces, and the number of slots in the method table.

· Syncblockindex, used for thread synchronization. Each object also contains this additional member when it is created. It points to a memory block called synchronization block for Object synchronization management, it also occupies 4 bytes of memory space.

· Nextobjptr, a pointer maintained by the managed heap, used to identify the location of the next newly created object allocated in the managed heap. During CLR initialization, nextobjptr is located at the base address of the managed heap.

3. Inheritance theory

The method table is created when the class is loaded to appdomain for the first time. When an object is created, the append member typehandle is associated with the address on the loader heap and Its Dynamic Method list, therefore, the method indicates that it exists before the object.

// Bird creates an object reference, while new bird () creates a bird object, allocates memory and initializes the object, and then assigns the object reference to the bird variable, that is, the association between the resume bird and the bird
Bird = new bird ();
// 2. analyze how the CLR executes the object creation process during runtime from the inheritance perspective
// 2.1 The first step is to sort the storage order of field creation fields from top to bottom, and the top class fields are listed at the top.
// 2.2 method table creation is completed when the class is loaded to appdomain for the first time. When an object is created, the append member typehandle is directed to the address on the loader heap in the method list, associate an object with its dynamic method list, so the method indicates that it exists before the object.
Chicken CH = new chicken ();

This article comes from: http://www.lmwlove.com/ac/ID904

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.