.NET Framework automatic memory management mechanism in-depth analysis (C # analysis)

zhaozj2021-02-17  46

In .NET Framework, the resources in memory (ie all binary information are set) are divided into "managed resources" and "non-hosting resources". The hosted resource must accept the management of .NET Framework's CLR (General Language Runtime) (such as Memory Type Security Check), without hosting resources do not have to accept CLR management of .NET Framework. (For more distinction, see .NET Framework or C # advanced programming information)

The hosted resource is stored in two places in the .NET Framework: "Stack" and "House" (hereinafter referred to as "stack"); the rule is, all value types (including reference and object instance) and reference type reference All are stored in "stack", and all object instances represented by all references are saved in the heap.

In C #, the release managed resources are automatically completed through the "garbage collector" (note, "garbage collection" mechanism is the characteristics of .NET Framework, not C #, but specifically, there are still places to pay attention to. : 1. Type (including reference and object instance) and reference type references actually do not need "garbage collector" to release memory, because when they have a scope, they will automatically release the memory (because they all Save in "Stack", learn the data structure This is a first-forward structure); 2. Save the object instance pointed to the reference type to save in "Heap", and the stack is a free storage Space, so it didn't have a "stack" ("stack" element pop-up, representing the end of the survival period, but also released memory), and it is important to note that "garbage recovery" is only right. This area works; 3. "Garbage Recycling" may not be executed as soon as many people imagine (when the resources in the heap need to be released), but the reference type of reference is deleted and it is "pile "The object instance is deleted in the middle of the interval, why?

(Of course, user code can use method system.gc.collect () to enforce "garbage collector")

However, in most cases, we need to explicitly release managed resources without performing "garbage collector" (because only one part is required, but it is also very needed to release resources, but it is best not to call "garbage collector". Because "garbage collector" is too waste system resources), or need to release "non-hosting resources", what should we do? This is what we must consider when we write code ("garbage recovery" is the system Automated, general cases do not require user intervention), otherwise the Windows system will be exhausted because of memory ... Now, let me tell what to do, that is, using the class's Dispose () method release all types of resources and use destructive methods Release the unmanaged resource! 1.Dispose () method To release the resource via the Dispose () method, then "System.IDISPOSable" interface when the class definition is defined, and then the method must contain such a definition "Void Dispose () "(In the dispose () method is the code segment of the user's own release resource), so that the user will know that the resource can be released by artificially calling the Dispose () method. But it is necessary to pay attention to" garbage recycling " "Is not to release managed resources by calling the dispose () method!

2. The format of the destructor defined in the C # is "~ class_name ()". It is important to note that if there is no unmanaged resources in a class, then do not define the destructor, this is Because the object executes the destructor, "garbage collector" should first adjust the destructor before release the hosted resource, and then truly release the hosted resources second time, so that the cost of deleting the action is mostly one most. ! (However, even if you have defined the destructor in the class, there is still a way to "shield" it, which will be explained in the following code paradigm) In the desemerical method, it is the release of the non-hosting resource written by the user. The code segment.

The following uses a code to demonstrate how Dispose () methods and destructual methods are used:

Public class resourceholder: System.idisposable {PUBLIC VOID Dispose () {Dispose (TRUE); System.gc.suppressFinalize (this); // The above line of code is to prevent the "garbage recovery" from the method // " ~ ResourceHolder () "Why should you prevent it? Because if the user remembers the Dispose () method, then //" Garbage Recycling "does not have to" more this "and release it again" Non-hosting resources "// If Users don't remember to call, let "garbage recovery" help us "more this" ^ _ ^ // You don't understand what I said above, I still have more detailed explanation!}

Protected Virtual Void Dispose (Bool Disposing) {if (Disposing) {// Here is the user code segment of the Cleaning "Managed Resources"} // Here is the user code segment of the cleanup "non-hosting resource"}

~ Resourceholder () {Dispose (false);}}

The above code is a typical class definition with two DISPOSE methods. There are many system classes in .NET Framework, there are many system classes to define the Dispose () method, for example: MSDN, system.drawing.brush.dispose method This is defined: *************************************************** *************** * All resources used by this Brush object are released. * * Public Void Dispose () * * This member supports the .NET framework structure, so it does not apply to use directly from the code. * * Protected Virtual Void Dispose (Bool); * *************************************************** ********************

转载请注明原文地址:https://www.9cbs.com/read-28883.html

New Post(0)