This article describes the following:
Object creation Process
Memory allocation analysis
Memory Layout Research
Back to [18th: object Creation (above)], continue to discuss the topic of object creation >>>
2.2 Memory allocation mechanism for the managed heap
Instances of reference types are assigned to the managed heap, while line stacks is where the object life cycle begins. For a 32-bit processor, after the application completes the process initialization, the CLR allocates a reserved address space on the process's available address space, which is a memory area on the available address space in the process (4GB per process), but does not correspond to any physical memory, the address space being the managed heap.
The managed heap is divided into multiple regions based on the different storage information, the most important of which is the garbage collection heap (GC Heap) and the load heap (Loader Heap), the GC Heap is used to store object instances, and is managed by GC; Loader Heap is also divided into high-frequency Heap , Low-frequency Heap and stub Heap, and different information is stored on different heaps. The most important information of Loader heap is metadata-related information, which is the type object, each of which is represented as a method table on the Loader heap, and the stored metadata information, such as the base type, Static fields, implemented interfaces, all methods, and so on. Loader Heap is not controlled by GC, and its lifecycle is from creation to AppDomain uninstall.
Before entering the actual memory allocation analysis, it is necessary to account for several basic concepts in order to better discuss them in the following analysis.
· Typehandle, type handle, point to the method table for the corresponding instance, each object is created with that additional member, and occupies 4 bytes of memory space. We know that each type corresponds to a method table, which is created at compile time, mainly including the type's characteristic information, the number of interfaces implemented, the number of slot of the method table, and so on.
· Syncblockindex, which is used for thread synchronization, also contains the additional member when each object is created, pointing to a block of memory called synchronization blocks, which is used to manage object synchronization and also occupies 4 bytes of memory space.
· Nextobjptr, a pointer maintained by the managed heap that identifies where the next new object is allocated in the managed heap. When CLR initializes, Nextobjptr is at the base address of the managed heap.
Therefore, we should have a basic understanding of the reference type allocation process, and because of the relatively complex inheritance relationship of the FileStream type in this example, this article implements a relatively simple type to illustrate:
//@ 2007 Anytao.com
//http://www.anytao.com
public class UserInfo
{
private Int32 age = -1;
private char level = 'A';
}
public class User
{
private Int32 id;
private UserInfo user;
}
public class VIPUser : User
{
public bool isVip;
public bool IsVipUser()
{
return isVip;
}
public static void Main()
{
VIPUser aUser;
aUser = new VIPUser();
aUser.isVip = true;
Console.WriteLine(aUser.IsVipUser());
}
}