Studied
5
Day's garbage collector, read Wang Yonggang's article,
Code project
On
2
Articles and
Msdn,
Finally, I understood a little bit. In
. Net
In a programming environment, system resources are divided into managed resources and unmanaged resources.
For the recovery of managed resources, manual intervention is not required, and you cannot interfere with their recovery. All you can do is to understand
. Net CLR
How to perform these operations. That is to say, for most objects created by your application, you can rely on
. NET Framework
The garbage collector implicitly executes all necessary memory management tasks.
For unmanaged resources, you must release these resources after they are used in the application. For example
System. Io. streamreader
A file object that must be displayed
Close ()
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. Okay
. Net
Framework
Provided
Finalize ()
Method, which allows the Garbage Collector to clear unmanaged resources properly when it recycles such resources. If
Msdn Library
Searching
Finalize
You will find many similar topics. Here we will 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!
Don't talk about hosting resources.
Int, String, float, datetime
Wait,
. Net
Over
80%
Are all managed resources.
How to release unmanaged Resources,
. NET Framework
Provide
Object. Finalize
Method, which allows the object to properly clear its unmanaged resources when the Garbage Collector recycles the memory used by the object. By default,
Finalize
Method. By default,
Finalize
Method
Do not perform any operations
. If you want the Garbage Collector to clear the object before it recycles the object's memory, you must overwrite it in the class.
Finalize
Method. However, you can find that in actual programming
Override
Method
Finalize (),
In
C #
, You can use
Destructor
Automatic Generation
Finalize
Methods and
Finalize
Method 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, in programming
Override
Method
Finalize ()
Because the implementation
Finalize
Method or destructor may have a negative impact on performance. A simple reason is as follows:
Finalize
Method: reclaim the memory used by the object.
At least twice
Garbage Collection: When the Garbage Collector recycles, it only recycles non-Terminator.
(Finalize
Method
)
In this case, it cannot recycle the memory with Terminator
(Finalize
Method
)
Memory that cannot be accessed. It removes the items of these objects from the termination queue and places them in the list of objects marked as "ready to terminate". The items in this list point
Managed heap
To be called to terminate the code. The next time the Garbage Collector recycles the code, it recycles and releases the memory.
Of course there are other influences not to be used
For the finalize () reason, refer to the subsequent articles!