Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the .NET memory allocation method?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The knowledge of this article "what is the .NET memory allocation method" is not quite understood by most people, so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this ".NET memory allocation method is what" article.

When analyzing memory allocation, you should first understand the differences about the stack

The allocation of the heap extends to the high address, while the allocation of the stack extends to the low address.

I. memory allocation

With regard to the allocation of memory, you should first understand where the allocation is. There are three main areas of memory managed by CLR, which are:

The stack of threads that are used to allocate instances of value types. The stack is mainly managed by the operating system and is not controlled by the garbage collector, and its storage unit is automatically released when the method in which the value type instance is located ends. The stack has high execution efficiency, but the storage capacity is limited.

GC heap, which is 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 on the GC heap, and the garbage collector may compress the GC heap when there is memory allocation or collection, as described later.

Public class VIPUser:User {/ / allocate 1Byte public bool isVip; public bool IsVipUser () {return isVip;} static void Main (string [] args) {/ / allocate memory space and initialize operation VIPUser aUser / / assign the object reference to the aUser variable and establish the association between aUser and VIPUser aUser = new VIPUser (); / / Q: the number of bytes allocated to the type? / / 15Byte is required for this category. However, the total number of bytes occupied by the instance object plus the number of bytes required for the additional members of the object, including the additional members TypeHandle and SyncBlockIndex, total 8 bytes. The total number of bytes allocated on the managed heap is 23 bytes, and the memory blocks on the heap are always allocated according to multiples of 4Byte, so this class will allocate 24 bytes of address space / / call the object constructor to initialize the object and complete the creation / / construction process / / a. Construct a Type object of type VIPUser, including static fields, method tables, implemented interfaces, and so on, and assign them to the Loader Heap of the managed heap mentioned above. / / b. Initialize two additional members of aUser: TypeHandle and SyncBlockIndex. Pointing the TypeHandle pointer to the MethodTable,CLR on the Loader Heap will locate the specific Type; and point the SyncBlockIndex pointer to the memory block of the Synchronization Block based on the Type;, which is used for synchronizing instance objects in a multithreaded environment. / / c. Call the constructor of VIPUser to initialize the instance field. When the instance is initialized, the parent class initialization is performed recursively until the initialization of the System.Object type is completed, and then the initialization of the subclass is returned until the VIPUser class is executed. In this example, the initialization process is to execute the System.Object class first, and then the User class. * is the VIPUser class. Finally, the memory address of the managed heap allocated by newobj is passed to the this parameter of VIPUser and its reference is passed to the aUser declared on the stack. AUser.isVip = true; Console.WriteLine (aUser.IsVipUser ()); / / the above process basically completes the whole process of reference type creation, memory allocation and initialization}} public class UserInfo {/ / allocate 4 bytes private Int32 age =-1 / / assign 2 bytes private char level ='A';} public class User {/ / assign 4byte private Int32 id / / saved UserInfo reference occupies 4Byte / / is only a reference (pointer), saved on the thread's stack, takes up 4Byte's memory space to hold the valid address of the user object, and now throws a NullReferenceException private UserInfo user;} for any attempt to user.

LOH (Large Object Heap) heap, which is used to allocate large object instances. If the instance size of the reference type object is not less than 85000 bytes, the instance will be assigned to the LOH heap, while the LOH heap will not be compressed and will only be reclaimed when the GC is fully recycled.

Before you learn about memory allocation, take a look at three concepts:

TypeHandle, the type handle, points to the method table of the corresponding instance, and each object is created with this additional member and takes up 4 bytes of memory space. We know that each type corresponds to a method table, which is created at compile time and mainly contains the characteristic information of the type, the number of interfaces implemented, the number of slot of the method table, and so on.

SyncBlockIndex, used for thread synchronization, also contains this additional member when each object is created, which points to a block of memory called Synchronization Block, which is used to manage object synchronization and also takes up 4 bytes of memory space.

NextObjPtr, a pointer maintained by the managed heap to identify the location in the managed heap when the next newly created object is allocated. When CLR is initialized, the NextObjPtr is at the base address of the managed heap.

Second, the theory of inheriting essence

/ / Bird bird creates a reference to an object, while new Bird () creates a Bird object, allocates memory and initializes operations, and then assigns the object reference to the bird variable, that is, the association Bird bird = new Bird () between resume bird and Bird; / / 2. Analyze how CLR executes the object creation process from the point of view of inheritance / / 2.1.First of all, the storage order of the field creation fields is arranged from top to bottom, and the fields of the layer class are ranked first. / / 2.2.The method table is created when the class is loaded into AppDomain * * times, and only points its additional member TypeHandle to the address on the method list Loader Heap when the object is created. Associates an object with its dynamic method list, so the method represents what exists before the object. Chicken ch = new Chicken (); above is the content of this article on "what is the .NET memory allocation method?" I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report