Continue to translate today. Haha, I read what you must know before. net has learned about some GC recovery mechanisms. By translating this article, I have learned more about GC recovery. Thank you for choosing the correct solution :-)
Original article:
Http://www.codeproject.com/KB/aspnet/DONETBestPracticeNo2.aspx#Conclusion%20about%20generations
Use finalize/dispose to improve GC Performance
Is this article worth further reading?
Introduction and purpose
Assume that
Thanks to Mr. Jeffrey Richter and Peter Sollich
GC-unknown hero
"Generation" algorithm-today, yesterday, and the day before yesterday
How "Generation" improves optimization performance
Summary of "Generation"
Using "finalize/destructor" will increase the number of objects in the first and second generations.
Use Dispose to replace the destructor
If the programmer forgets to call the dispose method?
Summary
Is this article worth further reading?
Through this article, you can learn how to use the finalize/dispose method to improve GC performance. The goal of this article is displayed.
Introduction and purpose
What is the best way for any programmer to release unmanaged resources? 70% of people will talk about destructor. Although the Destructor looks like
A good place to release resources, but it will lead to significant performance and memory consumption. Writing the release resource code in the Destructor will cause two accesses to GC and affect the performance multiple times.
To verify the above theory, we will first begin with some theoretical knowledge, and then explain how the Destructor affect the performance of the GC algorithm. Therefore, we should first understand the concept of "Generation,
Then let's look at the finalize/dispose method.
I am sure this article will change your views on destructor, dispose, and finalize.
Assume that
This article uses CLR profiler to detect GC. If you have never understood CLR profiler, read the previous article first. Continue reading this article.
Thanks to Mr. Jeffrey Richter and Peter Sollich
Before starting this article, we should first rewrite Mr. Jeffrey Richter to thoroughly end how the GC algorithm runs. He has written two brilliant articles on GC.
I wanted to post the two articles in the MSDN magazine, but for some reason I could not find them in the MSDN. Therefore, I posted another unofficial address. You can
Download pdf documentation: http://www.cs.inf.ethz.ch/ssw/files/GC_in_NET.pdf at this address
Colleagues thanked the CLR performance optimization architect Mr. Peter Sollich for writing such a detailed CLR profiler help document. If you have installed CLR profiler, do not forget
Read this detailed document. In this article, we will use CLR profiler to check how GC is affected by finalize.
Thank you very much. I cannot complete this article without reading your article. If you are passing by, please leave some comments.
GC-unknown hero
As mentioned in the introduction just now, clearing Resources in the Destructor will cause two accesses to GC. Many programmers may argue: "Do we really need to worry about running GC in the background ?".
Yes, in fact, we don't have to worry about GC, if our code is correctly written. GC has the best algorithm to ensure that your application is not affected. But most of the time
In the code and code, allocating/clearing memory resources often affects the GC algorithm. Sometimes, this has a bad impact on the GC performance and also affects the performance of your application.
Let's first understand the difference between gc memory allocation and memory recovery.
Suppose we have three classes. Class A references Class B, and Class B references Class C"
When you run the program for the first time, the application will first apply for a memory address in the memory. After the program creates these three objects, they use the memory address to point to the memory heap. You can see
Memory Allocation before and after object creation. If you create another D object, it will be allocated to the end of the C object.
An object graph is maintained in the GC to determine whether the object is reachable. All objects belong to the root object of the main program. The root object also maintains the address to which the object is allocated.
Therefore, if an object contains another object, the object stores the address of another object. For example, object A contains object B, so object A also saves the address of object B.
Now, if A is removed from the memory, the memory address of A is allocated to B, while that of B is allocated to C. In this case, the internal memory allocation is shown in:
Since the object address is updated, GC must also ensure that its internal object graph is updated to the latest memory address. Therefore, the object is changed to the following. GC now requires a lot of work to ensure
Removes and updates the addresses of existing objects in the object tree from the object graph.
A program's object graph not only has its custom object, but also has a. net internal object. These object addresses also need to be updated .. NET runtime has a large number of objects. For example, the following shows a simple
The number of objects in the "hello world" console application. There are about 1000 objects. updating pointers to traverse these objects is a large-scale task.
"Generation" algorithm-today, yesterday and the day before yesterday
GC uses the concept of generation to improve performance. The concept of generation is based on human psychology to solve problems. The following lists how humans handle problems as well as GC:
If you decide to do something this year, it may be done in the end.
If something was not completed yesterday, it may not be important. It can also be postponed.
If something is left behind the day before yesterday, it may be permanently postponed.
GC also thinks so, so there are the following assumptions:
If a new object is created, its lifecycle is short.
If the old object is used, it may take a longer life cycle.
Therefore, GC supports three generations (Generation 0, Generation 1, and Generation 2)
All new objects are created in generation 0th. When a program creates an object, these objects are first placed in The 0th generation. When 0th with resources are used up, GC will release some memory resources.
Therefore, GC starts to create a generation graph and then clears objects that are no longer used by the program. If GC cannot clear The 0th generation object, GC will move it to the 1st generation. If not
If the object is cleared from generation 1st, it will be moved to generation 2nd .. Net can only support up to 2 generations.
It displays the generation object through CLR profiler. If you have never used CLR profiler, you can read the previous article.
How "Generation" improves optimization performance
Since all objects are included in the generation relationship, GC can decide which generation of objects to clear. I don't know whether you remember it. As we mentioned earlier, GC clears objects by their age.
GC assumes that the lifecycle of all new objects is very short. That is to say, GC mostly traverses 0th generation objects, rather than other generation objects.
If the 0th generation resource is not enough, it will continue to traverse the 1st generation and other generation objects. This algorithm greatly improves GC performance.
Summary of "Generation"
If many objects exist in the 1st and 2nd generations, the memory allocation is not optimized.
Increasing the content of 1st and 2nd generations will lead to a reduction in GC performance.
Using "finalize/destructor" will increase the number of objects in the first and second generations.
C # the compiler will rename the destructor to Finalize. If you use IDASM to view the IL code of the program, you will find that the Destructor is renamed as Finalize.
So let's take a look at why the introduction of destructor will lead to more objects in gen 1 and gen 2. The specific procedure is as follows:
After the new object is created, it will be placed in the 0th generation.
When the 0-generation object is full, GC starts to try to clear some memory.
If the object is no longer used and there is no destructor, clear the object directly.
If the object has the finalize method, put these objects in the "finalization" queue.
If the object is reachable, it will be moved to the "Freachable" queue. If the object is inaccessible, the memory is reset.
GC is completed in this iteration.
The next GC will first traverse the Freachable object to check whether all objects can be reached. If the objects cannot be reached, the Freachable memory will be recycled.
In other words, if an object has a destructor, it will wait for more time in the memory.
Let's take a look at the following example. Below is a simple class with destructor.
Class clsMyClass
{
Public clsMyClass ()
{
}
~ ClsMyClass ()
{
}
}
We create 100*10000 objects in a loop and use CLR profiler for monitoring.
For (int I = 0; I <100*10000; I ++)
{
ClsMyClass obj = new clsMyClass ();
}
If you view the CLR profiler's memory address report, you can see that many objects are in gen 1.
Now we will remove the Destructor and also upload the 100*10000 objects.
Class clsMyClass
{
Public clsMyClass ()
{
}
}
You can see that the number of 0th generations has increased by a certain amount, and the number of 1st and 2nd generations has become fewer.
If you look at the comparison chart, you can see the following picture.
Use Dispose to replace the destructor
We can clean up the code by implementing the Dispose method in the IDisposable interface, instead of using the Destructor and writing the code in the Dispose method,
Call the SuppressFinalize method at the same time, as shown in the following code segment. The 'ssressfinalize' method tells the GC not to call the Finalize method. Therefore, GC will not be called repeatedly.
Class clsMyClass: IDisposable
{
Public clsMyClass ()
{
}
~ ClsMyClass ()
{
}
Public void Dispose ()
{
GC. SuppressFinalize (this );
}
}
The client must make sure that the Dispose method is called as follows:
For (int I = 0; I <100; I ++)
{
ClsMyClass obj = new clsMyClass ();
Obj. Dispose ();
}
The following comparison graph shows the use of the Destructor and Dispose functions. From the identifier, we can see that gen 0 has better memory allocation.
If the programmer forgets to call the dispose method? (The first time I forgot to write the demo code, I forgot to call Dispose ,:-))
The fact is not perfect. We cannot ensure that the client can call the Dispose method correctly. Therefore, we can use the Finalize/Dispose mode to explain the content of the following section.
For more detailed implementation modes visit: http://msdn.microsoft.com/en-us/library/b1yfkh5e (VS.71). aspx
The following describes how to use the finalize/dispose mode:
Class clsMyClass: IDisposable
{
Public clsMyClass ()
{
}
~ ClsMyClass ()
{
// In case the client forgets to call
// Dispose, destructor will be invoked
Dispose (false );
}
Protected virtual void Dispose (bool disposing)
{
If (disposing)
{
// Free managed objects.
}
// Free unmanaged objects
}
Public void Dispose ()
{
Dispose (true );
// Ensure that the destructor is not called
GC. SuppressFinalize (this );
}
}
Code parsing:
We have defined a Dispose method with the bool parameter. this parameter is used to determine whether to call the function through Dispose or destructor. If it is called through the "Dispose" Method
You can release hosted and unmanaged resources.
If it is called through the destructor, we only release the unmanaged resources.
In the Dispose method, we call the SuppressFinalize method and call the Dispose method using true.
In destructor, we call the Dispose method with false. In other words, we assume that GC processes managed resources, and the Destructor is used to process unmanaged resources.
In other words, if the client forgets to call the Dispose method, The Destructor will clear the tasks of unmanaged resources.
Summary
Do not use empty destructor in your class
If you need to clear resources in finalize/dispose mode, you must call the SupressFinalize method.
If the Dispose method is exposed in the class, the client must make sure to call this method.
More objects in the program should exist in Gen 0, instead of Gen 1 and Gen 2. If more objects are in Gen 1 and Gen 2, the GC execution algorithm is very bad.