Reading experience-software designer-you must know about. NET (C # type storage method analysis)

Source: Internet
Author: User

It took some time to carefully read what you must know. NET, because there is not much time, please understand the content and turn it into your own experience when reading the book. The following is a simple book review.

 

This book introduces in detail the storage allocation of C # type, and describes the storage and conversion of value type and reference type in a large space, if you want more details, you have to go to the page. net framework. In fact, this storage allocation should not be C #, but should be the storage allocation method of the. net Framework. Other languages, such as VB. NET and VC ++. NET, are the same, because in this. NET Framework, any language is compiled into IL. The underlying common type language CTL is used to process the unified types of languages, that is, the different types of languages are unified into a type defined in CTL.

The book described the idea of object-oriented design very well. It is strongly recommended that you have a taste of it.

 

Here, I would like to add that the several books I have read about the storage of value and reference types are not specific enough, and the Principles are not covered in detail. It is easy to mislead readers when defining them. I will make a comprehensive analysis here. (If I have not mentioned any omissions, please leave a message to add them and study together)

 

"Value-type instances are stored in the stack space", "reference-type instances are stored in the managed heap and released by GC" are all one-sided and incorrect.

 

1. Start with the namespace. If no Namespace is specified in the source code, the code is in the Global Namespace of C. Only in all namespaces can the two value types of structure and enumeration be defined, and reference types such as class type, interface, and delegation be defined, that is, all declarations and schema definitions of the types you need (all types are a data structure ). The Code Editor restricts the syntax. C # programs are organized by namespaces. C # programs are enclosed in namespaces, whether in an internal organization system or in an external organization system.

 

2. Perform analysis according to step 1.

Types are defined in all namespaces, while data is encapsulated in a class. methods are defined, inherited, overwritten, and overloaded in a class. Therefore, all type instances, including value type instances, reference type instances, and pointer type instances, exist only in three places: fields of a class; local variables in static class methods and object dynamic methods; formal parameters of static class methods and object dynamic methods (that is, copy of value type and reference type, it is actually a variable in the method ). That is, the basic scope of the object. The storage type can be analyzed from the scope.

 

3. Perform analysis based on 2.

In this environment, you must first describe the reference type, because the object only exists in the class (the scope is in the class), and the class is the reference type.

When the program is running, CLR allocates a space in the memory to store all the types used, that is, the type-defined images. All types used in the class are copies of the types stored in this space. For example, int I = 9; an int image copy is created on the stack based on the int type definition in the image space, assign the value 9 to the variable on the stack. The advantage is that you do not need to search for the int type definition, and then create a memory space according to the definition, and then perform operations on the space. Instead, you can directly create a type copy based on the data structure of the image in the memory to save the object. This shortens the process of object creation and the resources consumed, but also occupies the memory space to store those types that are not used yet. Therefore, in VS, if the Assembly is not used in the program, it should be deleted from the reference of the Assembly, to reduce the storage of Assembly metadata stored in the Assembly (although the types in these assembly are not loaded, they are not loaded into the memory ). In VS2008, the "Remove unused using" Operation item exists in the right-click menu, but the removal of Assembly references must be manually processed.

3.1 static class fields and methods.

Static classes have no instances, and their fields and methods are static. Therefore, when a program is loaded, it has allocated a space on the stack to store its data structure. Therefore, static fields, whether of the value type or reference type, are static and dynamic, read-only or writable, all are stored on the Heap (load the Heap Loader Heap. Its method is mapped to the allocated virtual function table. When calling the method, CLR copies the prototype of the method to the memory for running. Static classes are more efficient than dynamic classes (objects.

3.2 dynamic class (object) fields and Methods

When an object is created, an image copy of the prototype is assigned to the stack based on the class definition prototype to create a data structure and initialize the copy, at the same time, allocate a space on the stack to save the reference to the copy. The fields in the object are stored in the heap (GC heap or large object heap. The object method is also mapped to the virtual function table. When an object is created, all its parent classes need to be traced back, and the image copies of its parent class must be copied to its own bucket. The method overwrites are determined based on the inheritance of the class, therefore, object creation is a complex process, which consumes a large amount of resources. Therefore, it is the reason why the creation of objects should be minimized in the program. Dependency injection changes the object initialization method, which is not described here.

3.3 local variables and Parameter Storage of class methods

Whether it is a static or dynamic class, the CLR allocates the corresponding type space for storing the parameters in its class and method when calling the method, in fact, it is also a local variable with the same name as the form parameter. For Class methods, the CLR copies the prototype code of this method to a copy in the memory for running during the call. Therefore, local variables during method running are allocated and stored on the stack. Therefore, calling methods involves the concepts of stack pressure and stack exit. For the value type, it is stored directly on the stack. For the reference type, allocate the data structure of the type on the stack and store the reference of the data structure on the stack. For the pointer type, it is also stored directly on the stack.

3.4 Conclusion

Fields in the class, because they are allocated to the stack, are controlled by GC (Large objects are stored on the Large Object Heap (Large Object Heap) and only controlled when GC is completely recycled ). Local variables in class methods are not controlled by GC because they are allocated to the stack, and are processed by the operating system (because of the function running mechanism ). For local variables, you do not need to manually release resources. For Class fields, the GC controls the release of resources. Of course, you cannot create too many data structures. Otherwise, insufficient memory resources will occur, resulting in low GC efficiency. If so, you will need to add the memory hardware.

 

4. Value-type memory storage analysis.

For example, int num = 1; object obj = num; here, an object type variable is created on the GC stack because obj is of the reference type, store a copy of The num value, that is, 1, the original num value will not change, and the obj variable will occupy the space on the stack, to store the address of the space allocated on the GC stack. Therefore, after packing, the obj operation is actually an operation on the GC stack. Instead of operating the variables on the stack in the value type. Therefore, the packing operation will consume system resources (create a heap object ), in addition, the operation on this object consumes more resources than the variable on the operation stack (the stack efficiency is relatively higher than the heap efficiency, because the stack directly corresponds to the content in the stack address for direct addressing acquisition, and the stack is obtained by referencing indirect addressing ).

5. Type ref and out memory storage analysis.

For the value type Parameter Function void Test (ref int it) {};, CLR creates an int variable it when calling the function, add the it variable to the stack, and the it variable stores the address corresponding to the real parameter int variable that actually calls the function in the stack. That is, use one stack space to store another address in the stack space, which stores the value of the called variable.

For the reference type Parameter Function void Test (ref object obj), the same uses another address in the storage stack of one stack space, however, this address stores a reference pointing to a variable on the GC stack, that is, a memory address in the GC stack.

The pointer type also uses a stack space to store the referenced address, but this address may be the stack space address or the address in the managed stack.

 

 

By analyzing and summarizing the above types of storage, you can know the advantages and disadvantages of the Storage Based on the Value Type and reference type during system design, thus laying a foundation for improving the system's storage and operation performance.

Note: The above analysis is based on the verification in VS2008, and no code is provided here. Please verify your code by yourself to deepen your impression. Ref and out must use fixed variable addresses and cannot be verified using pointer types. Therefore, they can only be analyzed using code in IL. Similarly, because the address in the managed heap cannot be obtained, the storage problem of the object is inferred indirectly based on the pointer type.

 

Ps. Due to my limited level, if you disagree with the above content, please leave a message to correct it. Thank you very much.

Related Article

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.