I studied the 5-day garbage collector, read Wang Yonggang's article, two articles on Code project and msdn, and finally understood a little bit. In the. NET programming environment, system resources are divided into managed resources and unmanaged resources.
For the collection of managed resources, manual intervention is not required, and you cannot intervene in the collection. All you can do is to understand how. Net CLR performs these operations. That is to say, for most objects created by your application, you can use the. NET Framework Garbage Collector to implicitly execute all necessary memory management tasks.
For unmanaged resources, you must release these resources after they are used in the application, such as system. io. A file object of streamreader must be closed by calling the close () method of the object to be displayed. Otherwise, it will occupy the system memory and resources, and unexpected errors may occur.
Here, I want to know what is managed resources and what is non-managed resources?
The most common type of unmanaged resources is the objects that encapsulate operating system resources, such as files, windows, or network connections. For such resources, although the garbage collector can track the lifetime of objects that encapsulate unmanaged resources, however, it does not know how to clear these resources. Fortunately, the. NET Framework provides the finalize () method, which allows you to clear unmanaged resources when the Garbage Collector recycles such resources. If you search for finalize in the msdn library, you will find many similar topics. Here we list several common unmanaged resources: applicationcontext, brush, component, componentdesigner, container, context, cursor, filestream, font, icon, image, matrix, object, odbcdatareader, oledbdatareader
, Pen, RegEx, socket, streamwriter, timer, tooltip, and other resources. It is possible that many of them did not notice it during use!
About managed resources, we don't need to talk about it, such as simple int, String, float, datetime, etc. Over 80% of. net resources are managed resources.
How to release unmanaged resources. NET Framework provides the object. Finalize method, which allows the object to properly clean up its unmanaged resources when the Garbage Collector recycles the memory used by the object. By default, the Finalize method does not perform any operations. By default, the Finalize method does not perform any operations. If you want the Garbage Collector to clear the object before it recycles the object's memory, you must override the Finalize method in the class. However, you can find that the override method finalize () cannot be used in actual programming. In C #, The Destructor can be used to automatically generateFinalizeMethods andFinalizeMethod call.
For example:
~ Myclass ()
{
// Perform some cleanup operations here.
}
This code is implicitly translated into the following code.
Protected override void finalize ()
{
Try
{
// Perform some cleanup operations here.
}
Finally
{
Base. Finalize ();
}
}
However, the override method finalize () is not recommended in programming, because the implementation of the Finalize method or destructor may have a negative impact on the performance. A simple reason is as follows: using the Finalize method to recycle the memory used by the object requires at least two garbage collections. When the Garbage Collector recycles, it only recycles the Finalize method) in this case, the unaccessible memory with the Finalize method cannot be recycled. Instead, it removes the items of these objects from the termination queue and places them in the list of objects marked as "prepared for termination, the items in this list point to the objects in the managed heap that are preparing to be called to terminate the code. The Garbage Collector recycles and releases the memory the next time it recycles.
Of course, there are other reasons for not using finalize (). Refer to the subsequent articles!