DOTNET Learning Note 5 - III for Recycling

zhaozj2021-02-11  154

l Fina

li

ZE

In the previous article I assigned a total of five steps, we already know how GC is released in useless objects. But how do it achieve the fourth step to empty a resource usage, release some non-memory system resources that use it? .NET introduced Fina

li

ZE to complete this task.

GC once found an object when it is recycled in the unused unit, FINA

li

The ZE method is called. So our Fina

li

The ZE method must do things as little as possible to improve the speed of memory recovery. In addition, in Fina

li

Do not have any thread synchronization or the like in the ZE method to prevent the GC thread from hang.

We can use two ways to write your own Fina

li

ZE method. One is the implementation of the display, such as the following code:

Code 1 PUB

li

C class someclass {pub

li

c someclass () {}

Protected Override Void Fina

li

Ze () {console.writeline ("Fina

li

zing ... ");}}

Pay attention to it when using this method, .NET does not help you make a foundation class with Fina

li

ZE method. If you need to call the foundation of the FINA

li

The ZE method needs to be displayed. Such as follows:

Code 2 PUB

li

C class someclass {pub

li

c someclass () {}

Protected Override Void Fina

li

Ze () {console.writeline ("Fina

li

ZING ... "); base.fina

li

Ze (); // Call the base class FINA

li

ZE method}}

Another method is the destructor. The destructor in C # is different from C . Let's look at the following code:

Code 3 PUB

li

C class someclass {pub

li

c someclass () {}

~ SomeClass () {Console.WriteLine ("Fina

li

zing ... ");}}

It is equivalent to code 2.

Use Fina

li

The ZE method should be particularly careful. Because using Fina

li

The object of the ZE method is more than the ordinary object. The GC also spends more time to recover. And the CLR does not guarantee calling Fina

li

The order of ZE, so if the inter-object is related (such as a member variable first by Fina

li

Ze, if in Fina

li

It is more troublesome in the ZE method.

How does GC implement FINA

li

What about ZE? GC maintains two queues, FINA

li

ZATION queue and freselhable queue. When the hosted heap is assigned, the GC finds a FINA if this object is found.

li

ZE method, add it to FINA

li

ZATION queue. Figure:

When the memory of the hosted stack is insufficient, the GC starts recycling the heap. GC reclaims a subject, check Fina first

li

Is there a pointer for this object in the ZATION queue, if any, put it in the Freachable queue. The FREACHABLE queue is considered part of the root, so the GC will not be recycled. After the GC is first recovered, the pile is as follows:

Object G and object E are not within the range of roots, is reclaimed. Object f and object C Due to Fina

li

Ze is placed in the Freachable queue, which is considered part of the root, so this is the object F and the object C is resurrected, and is not recycled by GC. Finali for the object in the Freachable queue

The ZE method is performed by a special thread. This thread is in a non-active state, once the Freachable queue is no longer empty, it will wake up, perform Fina in this queue in this queue.

li

ZE method. After the execution is as follows:

At this time, the object f and object C are no longer part of the root. If the GC is reclaimed at this time, it will be considered to be recycled with useless objects. After the recovery is as follows:

It is simple to describe Fina

li

ZE role and its internal work. Let's take a look at the generation.

l generation

Searching throughout the entire pair, compression is very time consuming. Microsoft summed up some phenomena in the past development, one of which is, more new objects, and the fastest is up quickly and no longer used. Microsoft introduced the concept of Generation in memory recycling in memory recycling, and I temporarily translated into a generation. When the hosted pile starts, it is empty. The program starts to assign objects in it. At this time, the object is the 0th generation 0 object. As shown below:

Next, the hosted stack space is insufficient, the GC has been recovered for the first time, and the remaining objects that are not recycled will rise to the first generation, and then the newly allocated object is the 0th generation (A). After the GC re-recovered, only the 0th generation of the 0th generation was first generation, the original first-generation upgrade was the first generation (B).

GC default generation (Generation) is 2, and upgrade to the second generation will not be upgraded. When is the GC recycling first, the second generation? When the GC recovered ended 0th generation, it was found that the memory space was not enough, and the first generation will be recovered, the first generation of recovery is not enough, and the second generation is recycled.

This article has also written a lot, so the next one will continue, the next article writes WeakReference and how to control the Action of GC in their own code.

转载请注明原文地址:https://www.9cbs.com/read-5799.html

New Post(0)