C # hosting and unmanaged resources,
Reprinted from: http://blog.jobbole.com/89064/
Managed resources refer to. NET resources that can be automatically recycled, mainly the memory resources allocated on the managed stack. The collection of managed resources does not require manual intervention. Some. NET runtime Libraries call the Garbage Collector to recycle the resources.
Unmanaged resources refer. NET does not know how to recycle resources. The most common type of unmanaged resources are the objects that encapsulate operating system resources, such as files, windows, network connections, database connections, image brushes, and icons. This type of resource, the garbage collector will call the Object. Finalize () method during cleaning. By default, the method is empty. For unmanaged objects, you need to write code to recycle unmanaged resources in this method so that the garbage collector can correctly Recycle resources.
In. NET, Object. the Finalize () method cannot be overloaded. The Compiler automatically generates an Object based on the class destructor. the Finalize () method. Therefore, for classes that contain unmanaged resources, you can place the code that releases the unmanaged Resources in the destructor.
Note: The managed resources cannot be released in the destructor. Because the Destructor are called by the garbage collector, it may be before the Destructor is called, the managed resources contained in the class have been recycled, leading to unpredictable results.
If we follow the above practice, the unmanaged resources can also be recycled by the garbage collector, but the unmanaged resources are generally limited and valuable, and the garbage collector is automatically called by the CRL, in this way, the release of unmanaged resources cannot be guaranteed in a timely manner. Therefore, a Dispose () method is defined to allow users to manually release the unmanaged resources. The Dispose () method releases the managed and unmanaged resources of the class. After you manually call this method, the garbage collector does not recycle such instances. The Dispose () method is called by the user. During the call, the managed and unmanaged resources of the class are certainly not recycled, so the two types of resources can be recycled at the same time.
Microsoft specifically defines an interface for the collection of unmanaged resources: IDisposable, which contains only one Dispose () method. Any class that contains unmanaged resources should inherit this interface.
In a class that contains unmanaged resources, the standard practices for resource release are as follows:
(1) inherit the IDisposable interface;
(2) Implement the Dispose () method, release managed and unmanaged resources, and remove the object from the garbage collector (the garbage collector does not recycle this resource );
(3) Implement class destructor to release unmanaged resources.
In use, the call of the Dispose () method is displayed to release resources in time, and the execution of the Finalize () method is removed to Improve the Performance. If the call of the Dispose () method is not displayed, the garbage collector can also use destructor to release unmanaged resources. The Garbage Collector itself has the function of recycling managed resources to ensure normal resource release, however, recycling by the garbage collector will lead to the waste of unmanaged resources not released in time.
In. NET, you should use the destructor to release resources as little as possible. Objects without destructor are deleted from the memory during one processing by the spam processor, but the objects with destructor need to be called twice. The first time the Destructor is called, the second time the object is deleted. In addition, a large amount of resource code is included in the destructor, which will reduce the efficiency of the garbage collector and affect the performance. Therefore, for objects that contain unmanaged resources, it is best to call the Dispose () method in time to recycle resources, rather than relying on the garbage collector.
The above is the resource release mechanism for Classes containing unmanaged resources in. NET. As long as the code is written according to the steps required above, the class is a class of resource security.
The following uses an example to summarize the. NET unmanaged resource recovery mechanism:
Public class BaseResource: IDisposable {PrivateIntPtr handle; // handle, belonging to the unmanaged resources PrivateComponet comp; // component, hosting resources Privateboo isDisposed = false; // indicates whether resources have been released. PublicBaseResource {} // interface method implementation // the user of the class displays the call externally and releases the class resource Publicvoid Dispose () {Dispose (true); // release managed and unmanaged resources // remove the object from the linked list of the garbage collector, // only the managed resources are released when the garbage collector is working, instead of executing the Destructor GC for this object. suppressFinalize (this);} // called by the garbage collector to release unmanaged resources ~ BaseResource () {Dispose (false); // release an unmanaged resource} // If the parameter is true, all resources are released, users can only call this function. If the value of // is false, it indicates that unmanaged resources are released and can only be automatically called by the garbage collector. // If the subclass has its own unmanaged resources, you can reload this function, add your own unmanaged resource release // But remember to reload this function must ensure that the base class version is called to ensure that the base class resources are released normally Protectedvirtual void Dispose (bool disposing) {If (! This. disposed) // If the resource is not released, this judgment mainly uses to prevent objects from being released multiple times {If (disposing) {Comp. dispose (); // release managed resources} closeHandle (handle); // release the unmanaged resource handle = IntPtr. zero;} this. disposed = true; // identifies the released object }}
The Destructor can only be called by the garbage collector.
The Despose () method can only be called by the class user.
In C #, all classes that inherit the IDisposable interface can use the using statement, so that the system automatically calls the Dispose () method after the scope is exceeded. A resource security class implements the IDisposable interface and destructor. Provides double insurance for manually releasing resources and automatically releasing resources by the system.