In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the knowledge points of .NET memory allocation". The content of the explanation is simple and clear, and it is easy to learn and understand. Let's follow the editor's train of thought to study and learn what are the knowledge points of .NET memory allocation.
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.
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.
Let's take a look at three concepts before we learn about memory allocation.
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.
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. From the perspective of inheritance, this paper analyzes how CLR executes the process of object creation at runtime / / 2.1.First of all, the storage order of the created fields is arranged from top to bottom, and the fields of the layer class are at the top of the list / / 2.2.2.The creation of the method table is completed when the class is loaded into AppDomain for the first time. When an object is created, it simply points its additional member TypeHandle to the address on the method list Loader Heap, associating the object with its dynamic method list, so the method represents what exists before the object. Chicken ch = new Chicken (); thank you for reading, the above is the content of "what are the knowledge points of .NET memory allocation". After the study of this article, I believe you have a deeper understanding of the knowledge points of .NET memory allocation, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.