A managed resource refers to a. NET a resource that can be reclaimed automatically, mainly refers to the memory resources allocated on the managed heap. The collection of managed resources does not require human intervention, and the. NET runtime makes the appropriate call to the garbage collector for recycling.
Unmanaged resources refer to the. NET does not know how to reclaim the resources, the most common type of unmanaged resources is the packaging of operating system resources objects , such as files, Windows, network Connections, database connections, brushes, icons and so on . This type of resource, the garbage collector calls the Object.Finalize () method when scavenging. By default, the method is empty, and for unmanaged objects, code to recycle the unmanaged resource needs to be written in this method so that the garbage collector can reclaim the resource correctly.
In. NET, the Object.Finalize () method is not overloaded, and the compiler automatically generates the Object.Finalize () method based on the destructor of the class, so for classes that contain unmanaged resources, you can place code that frees unmanaged resources in the destructor.
The Dispose () method frees the managed and unmanaged resources of the class, and the garbage collector does not recycle such instances again after the user calls this method manually. The Dispose () method is invoked by the consumer, and when invoked, both the managed and unmanaged resources of the class are not reclaimed, so both resources can be reclaimed at the same time.
Microsoft specifically defines an interface for recycling of unmanaged resources: IDisposable, which contains only one Dispose () method in the interface. Any class that contains unmanaged resources should inherit this interface.
In a class that contains unmanaged resources, the standard practice for resource release is:
(1) Inheriting IDisposable interface;
(2) implements the Dispose () method, in which the managed and unmanaged resources are disposed, and the object itself is removed from the garbage collector (the garbage collector is not reclaiming the resource);
(3) Implements the class destructor, in which unmanaged resources are freed.
When used, the display calls the Dispose () method to release resources in a timely manner, while improving performance by removing the execution of the Finalize () method, or the garbage collector can release the unmanaged resources through a destructor if the call to Dispose () method is not displayed. The garbage collector itself has the ability to reclaim managed resources, thereby ensuring the normal release of resources, except that the garbage collector reclaims the waste that causes the unmanaged resources to be released in a timely manner.
You should release resources in. NET as sparingly as possible with destructors. Objects that do not have destructors are removed from memory in a single processing of the garbage processor, but the destructor object takes two times, the first time the destructor is called, and the object is deleted for the second time. And a large amount of freed resource code in the destructor will reduce the efficiency of the garbage collector and affect performance. So for objects that contain unmanaged resources, it is best to call the Dispose () method in time to reclaim resources rather than relying on the garbage collector.
It's up there. NET to the resource release mechanism for classes that contain unmanaged resources, the class belongs to a resource-safe class as long as the code is written according to the steps required above.
Here is an example to sum up. NET unmanaged resource recycling mechanism:
public class Baseresource:idisposable {Private IntPtr handle;//handle, belonging to unmanaged resources Managed resources Private BOOL isdisposed = false;
Has the flag of the resource been released public BaseResource () {}//Implementation interface method
By the consumer of the class, the call is displayed externally, releasing the class resource public void Dispose () { Dispose (true);//release managed and unmanaged Resources//Remove objects from the garbage collector list,//thereby work in the garbage collector When you do so, only the managed resources are released, not the destructor GC for this object.
SuppressFinalize (this);
///By the garbage collector, releasing unmanaged Resources ~baseresource () { Dispose (false); free unmanaged Resources}//parameter is true to release all resources, only the consumer call//parameter is False
Represents the release of unmanaged resources, which can only be automatically invoked by the garbage collector//If subclasses have their own unmanaged resources, you can overload this function to add the release of your own unmanaged resources However, remember that overloading this function must guarantee that the base class's version is invoked to ensure that the resource of the base class is properly released Protected virtual void Dispose (bool disposing)
{if (!this.disposed)//If the resource is not released this judgment is primarily used to prevent objects from being freed multiple times
If (disposing) { Release managed resources} CloseHandle (handle);/release non-TODO
Management Resources handle= IntPtr.Zero; } this.disposed= true; Identify this object has been disposed}}
Destructors can only be invoked by the garbage collector.
The Dispose () method can only be invoked by the consumer of the class.
In C #, any class that inherits the IDisposable interface can use a using statement, allowing the system to automatically invoke the Dispose () method after the scope is exceeded.
A class of resource security, all implements the IDisposable interface and the destructor function. Provides double insurance to manually release resources and system to automatically release resources. C # managed and unmanaged Resources
In a. NET programming environment, the system's resources are divided into managed and unmanaged resources. Managed Resources:
NET platform, the CLR provides programmers with a good memory management mechanism that allows programmers not to explicitly release the memory resources they use when writing code (these are required to be explicitly released by programmers themselves in the previous C and C + +). This management mechanism is called GC (garbage Collection). The role of GC is obvious, when the system memory resource is scarce, it will be fired, and then automatically release those unmanaged resources that are not being used (that is, the programmer does not explicitly release the object).
So hosting is the. NET Framework responsible for helping you manage memory and resource releases without having to control yourself, of course the object is for managed resources only (partial reference types) and does not reclaim unmanaged resources. Like arrays, user-defined classes, interfaces, delegates, object, strings, and other reference types, the stack holds an address just, when the stack released, even if the object has not been used, but the memory allocated on the heap is still, can only wait for GC collection can be truly released; but notice Int,string,float, Value types such as DateTime, the GC automatically frees up the memory they occupy and does not require GC to reclaim the release
So how do unmanaged resources release recycling? unmanaged Resources:
For unmanaged resources, a GC can track only the lifetime of an unmanaged resource without knowing how to release it. This will show that when resources are exhausted, you cannot provide services that resources can provide, and windows slows down. For example, when you link the database, you do not have to explicitly release the database resources, if it is still the application of database resources, then to a certain time the program will throw an exception.
So, when we encapsulate operations on unmanaged resources in a class, we need to explicitly or implicitly release these resources. There are 2 main ways to release unmanaged resources in. NET, and the Finalize and Dispose methods are the methods that are used separately in implicit and explicit operations, respectively. Dispose,finalize
For example, file flow, database connection, System window handle, printer resources, etc. , when you read the file, you need to dispose of various streams and other operations. For example, after reading the data SqlDataReader, need reader. Dispose ();
Finalize a class for the base class without the Close method or without dispose explicit methods, that is, in the finalize process we need to implicitly implement the release of the unmanaged resource, and then the system releases the managed resources itself after the finalize process completes. In. NET, the resources should be freed as little as possible with destructors, and there is a phrase on the MSDN2 that implementing a Finalize method or a destructor can negatively affect performance, so avoid using them unnecessarily. the memory used by a Finalize method to reclaim an object requires at least two garbage collections. So the destructor object takes two times, the first time the destructor is invoked, and the object is deleted for the second time. And a large amount of freed resource code in the destructor will reduce the efficiency of the garbage collector and affect performance. so for objects that contain unmanaged resources, it is best to call the Dispose () method in time to reclaim resources rather than relying on the garbage collector.