We know:C # is a virtual machine language. C # The Compiler first sets C #CodeCompile the code into Il and runProgramWhen CLR (Common Language Runtime,By calling JIT (Just-in-time Compiler,Real-time compiler) to dynamically compile the Il generation into executable machine code.GC (Garbage Collector, Garbage Collector) automatically allocates and releases memory management for our applications (for details, see:Measure the test taker's understanding about the. NET memory management mechanism.), In an efficient way to remove junk objects in the memory, but no matter how efficient, it will always take time to allocate and destroy objects on the stack.
If we create too many referenced objects in a method, the performance of the application will be seriously affected. Therefore, we should observe the following rules to minimize the GC workload.
Reading directory:
1. Promote common local variables to member variables
2. provide static objects for common type instances
3. Provide a mutable object for an unchangeable type
Section
1. Promote common local variables to member variables
All reference types, including those local variables, are allocated to the stack. After the function exits, all local variables in the function will immediately become junk objects.. So we can draw a conclusion:
If a local variable of a reference type (value type does not matter) is used in a frequently called routine, it should be upgraded to a member variable. This helps reduce the GC burden and improve the program running efficiency.
A common error in Gui programming is: creating a GDI (Graphics Device Interface,Graphical Device Interface) Object, as follows:
1 Protected Override Void Onpaint (painteventargs E) 2 { 3 Using (Font myfont = New Font ( " Arial " , 10.0f )) 4 { 5 E. Graphics. drawstring (datetime. Now. tostring (), myfont, brushes. Black, New Point ( 0 , 0 )); 6 } 7 Base . Onpaint (E ); 8 }
Onpaint ()It will be called very frequently, and a font object will be created each time it is called, and the Content contained is exactly the same as the previous one. Therefore, GC needs to clean the garbage for you every time, seriously affecting the efficiency of the application. In fact, we can fully promote the font object to a member variable. This font object can be reused every time the window is painted:
1 Private Readonly Font myfont = New Font ( " Arial " , 10.0f ); 2 3 Protected Override Void Onpaint (painteventargs E) 4 { 5 E. Graphics. drawstring (datetime. Now. tostring (), myfont, brushes. Black, New Point ( 0 , 0 )); 6 7 Base . Onpaint (E ); 8 }
After a common local variable is upgraded to a member variable, the program does not need to generate garbage objects each time it is re-painted, which reduces the GC burden and improves the program efficiency. But here is a smallRestrictions: To promote the local variable that implements the idisposable interface to the member variable, the dependent class of this member variable also needs to implement the idisposable interface.
2. provide static objects for common type instances
Static member variables allow the reference type to be shared among instances of the class.. We can provide a class to store the singleton object of a common instance type, so as to avoid creating repeated objects .. Net Framework class libraries have many such practices:BrushesClass contains a series of static brush objects, each containing a common color. Their brief implementation is as follows:
1 Private Static Brush blackbrush; 2 Public Static Brush black 3 { 4 Get 5 { 6 If (Blackbrush = Null ) 7 Blackbrush = New Solidbrush (color. Black ); 8 Return Blackbrush; 9 } 10 }
When you first request a black image brush, brushes will create an instance. The brushes class will retain the reference of this instance and return the same handle directly in subsequent requests. That is to say, we only create a black image brush and reuse this object all the time. For example, if the application does not use this resource, the object will not be allocated .. NET provides this method to create as few objects as possible while meeting the requirements. We should also do this in our own applications.
3. Provide a mutable object for an unchangeable type
System. StringType: the content of a string object cannot be modified. When you modify a string, A New String object is created, and the previous String object becomes garbage. See the following code:
1 // How are you? 2 String MSG = " How " ; 3 MSG + = " Are " ; 4 MSG + = " You " ; 5 MSG + = " ? " ;
The string + = Operator creates a new String object and returns the result. This type of concatenation string should be handed over to a more appropriateString. Format ()Method:
1 StringMSG =String. Format ("{0} {1} {2} {3}","How","Are","You","?");
You can useStringbuilderClass, which is a variable string used to create an immutable String object. Using the stringbuilder object to replace stirng objects that frequently change is a good choice --When a design requires an immutable type, we should consider providing a creation object that is specifically responsible for step-by-step construction of the final object (for example, the string object to the stringbuilder object ). In this way, you can create the final object step by step, and ensure the immutability of the object..
Section
GC can efficiently manage the memory used by applications, but it still takes time to create and destroy objects on the stack. Therefore, avoid creating too many objects and avoid creating unnecessary objects, do not create too many referenced objects in local methods. You can consider promoting common local variables to member variables or providing static objects for the most common type instances, in addition, you can also consider providing a mutable object for an unchangeable type.