Hosted resources refer to resources that .NET can automatically recycle, mainly referring to memory resources allocated on the hosted heap. The recycling of managed resources does not require manual intervention. There is a .NET runtime library that is suitable for calling the garbage collector for recycling.
Unmanaged resources refer to resources that .NET does not know how to recycle. The most common type of unmanaged resources are objects that wrap operating system resources, such as files, windows, network connections, database connections, brushes, icons, etc. The garbage collector will call the () method when cleaning up this type of resource. By default, the method is empty, and for unmanaged objects, you need to write code to recycle unmanaged resources in this method so that the garbage collector recycles resources correctly.
In .NET, the () method cannot be overloaded. The compiler automatically generates the () method based on the class's destructor. Therefore, for classes containing unmanaged resources, the code that releases unmanaged resources can be placed in the destructor.
Note that managed resources cannot be released in the destructor, because the destructor has a garbage collector call. It may be that the managed resources contained in the class have been recycled before the destructor call, resulting in unpredictable results.
Originally, if the above practice is followed, unmanaged resources can also be recycled by the garbage collector, but unmanaged resources are generally limited and more valuable. The garbage collector is automatically called by the CRL, so it is impossible to guarantee the timely release of unmanaged resources. Therefore, a Dispose() method is defined to allow users to manually release unmanaged resources. The Dispose() method releases the class's managed resources and unmanaged resources. After the user manually calls this method, the garbage collector will not recycle this type of instance again. The Dispose() method is called by the user. When called, both the managed resources and the unmanaged resources of the class must have not been recycled, so both resources can be recycled at the same time.
Microsoft specifically defines an interface for the recycling of unmanaged resources: IDisposable, which only contains one Dispose() method. Any class that contains unmanaged resources should inherit this interface.
In a class containing unmanaged resources, the standard practice regarding resource release is:
(1) Inherit the IDisposable interface;
(2) Implement the Dispose() method, release managed resources and unmanaged resources, and remove the object itself from the garbage collector (the garbage collector is not recycling this resource);
(3) Implement the class destructor, where unmanaged resources are released.
When using it, the display calls the Dispose() method to release resources in a timely manner, and at the same time, the performance is improved by removing the execution of the Finalize() method; if the display calls the Dispose() method, the garbage collector can also release unmanaged resources through destructors. The garbage collector itself has the function of recycling managed resources, thereby ensuring the normal release of resources. However, recycling by the garbage collector will lead to waste of unmanaged resources being released in a timely manner.
In .NET, use destructors as little as possible to free resources. Objects without destructors are deleted from memory in a single processing by the garbage processor, but objects with destructors need to be called twice, the first time the destructor is called and the second time the object is deleted. Moreover, including a large amount of free resource code in the destructor, it will reduce the working efficiency of the garbage collector and affect performance. Therefore, for objects containing unmanaged resources, it is best to call the Dispose() method in time to recycle resources instead of relying on the garbage collector.
The above is the resource release mechanism for classes containing unmanaged resources in .NET. As long as you write code according to the steps required above, the class belongs to resource-safe classes.
The following is an example to summarize the .NET unmanaged resource recycling mechanism:
Public class BaseResource:IDisposable { PrivateIntPtr handle; // Handle, which belongs to an unmanaged resourcePrivateComponet comp; // Components, managed resourcesPrivateboo isDisposed = false; // Flag of whether the resource has been releasedPublicBaseResource { } //Implement the interface method//The user of the class displays the call outside and releases the class resourcesPublicvoid Dispose() { Dispose(true);// Free up managed and unmanaged resources //Remove the object from the garbage collector linked list,// Therefore, when the garbage collector works, only managed resources are released, and the destructor of this object is not executed(this); } //Called by the garbage collector to release unmanaged resources~BaseResource() { Dispose(false);// Release unmanaged resources} //Argument is true to release all resources and can only be called by the user//Argument is false to release unmanaged resources and can only be called automatically by the garbage collector//If the subclass has its own unmanaged resources, you can overload this function and add the release of its own unmanaged resources//But remember that overloading this function must ensure that the version of the base class is called to ensure that the resource of the base class is released normallyProtectedvirtual void Dispose(bool disposing) { If(!)// If the resource is not released, this judgment mainly uses to prevent the object from being released multiple times{ If(disposing) { ();// Release managed resources} closeHandle(handle);// Release unmanaged resourceshandle= ; } = true; // Identify that this object has been released} }
The destructor can only be called by the garbage collector.
The Despose() method can only be called by the class's users.
In C#, any class that inherits the IDisposable interface can use the using statement, so that the system automatically calls the Dispose() method after it exceeds the scope. A resource-safe class implements the IDisposable interface and destructor. Provides dual insurance for manually releasing resources and system automatically releasing resources.
Summary of distinguishing between managed resources and unmanaged resources
(1) Hosted resources generally refer to memory resources controlled by CLR. The management of these resources can be controlled by CLR, such as objects allocated in the program, variables within the scope, etc.
(2) Unmanaged resources are parts that cannot be controlled or managed by CLR. There are many of these resources, such as file flow, database connections, system window handles, printer resources, etc. These resources generally do not exist in Heap (where in memory is used to store object instances).
Hosted resources: From a text perspective, it is entrusted to others to manage, just like .NET's CLR and java's jvm