This article describes how C# cleans up unmanaged objects. Share it for your reference, as follows:
How is Finalize implemented within .net?
When the GC (Garbage Collector) starts working, it first removes garbage objects without a terminal from memory, and all objects with a terminal are added to a terminated queue. GC will call a new thread to execute the finalizer for these objects. When the finalizer is executed, these objects are removed from the queue. At this time, since these objects are not released when they are detected for the first time, they will enter the first generation object. Until GC detects that the 0th generation object and the 1st generation object are filled again, GC will release the objects just now, so objects with a finalizer will be retained in memory for longer than those without.
Tip: The garbage collector divides the objects in the managed heap into 3 generations, namely 0, 1, and 2. The general allocation is: 0 generation is about 256K, 1 generation is about 2MB, and the second generation is about MB. The higher the generation age, the greater the capacity, and obviously the lower the efficiency. The objects added to the managed heap are first designated as the 0th generation. When the 0th generation is full, garbage collection will be performed, and the unrecycled objects will be upgraded to 1 generation.
For the above reasons, you should avoid using Finalize only to release unmanaged resources.
Dispose mode: Implement the IDispose interface in a custom class, and release unmanaged resources in the Dispose method in the interface. Less gossip, add the code
public class MyResourceRelease: IDisposable { /// Ensure that resources are only released once private bool _alreadyDisposed = false; /// Used to determine the categories of resources (hosted and unmanaged) protected virtual void Dispose(bool isDisposing) { if(_alreadyDisposed) { return; } if(isDisposing) { //Release managed resources } //Release unmanaged resources _alreadyDisposed = true; } public void Dispose() { Dispose(true); } }
The above code is a method of releasing resources using the Dispose method. Because the above custom Dispose (bool isDisposing) method is virtual, you can also override it in the derived class.
public class MyDerivedResource: MyResourceRelease { private bool _disposed = false; protected override void Dispose(bool isDisposing) { if(_disposed) { return; } try { if(isDisposing) { //Release managed resources } //Release unmanaged resources _disposed = true; } finally { (isDisposing); } } }
This ensures that all objects on the inheritance chain are released and the Dispose pattern is propagated throughout the inheritance hierarchy
For more information about C# related content, please check out the topic of this site:C# data structure and algorithm tutorial》、《Tutorial on the usage of common C# controls》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming》
I hope this article will be helpful to everyone's C# programming.