DOTNET study notes six - three free resources recycling three

zhaozj2021-02-11  187

This article will follow the last time, continue to discuss any other topics that useless resources recycled.

l Weakreference (weak reference)

We usually use the strong references for objects. If there is a strong reference existence, the GC will not reclaim objects. Can we keep the reference to an object at the same time, and can you recover this object when GC needs? WeakReference is available in .NET. Weak references are very simple, look at the following code:

Code 1

Object obj = new object (); weakreference wref = new weakreference (obj); obj = NULL;

The first line of code has created a new object, which is called it object A. OBJ is a strong reference to object A. The second line of code will then create a weak reference object. The parameter is the strong reference of the object A, and the third row code releases the strong reference to Object A. At this time, if the GC is recycled, the object A will be reclaimed.

How to get the strong reference to the object A? Very simple, please see code 2:

Code 2 Object Obj2 = Wref.Target; if (Obj2! = Null) {... // Do what you want to do. } Else {... // Object has been reclaimed, if you need to create a new one. }

As long as the display is displayed, the Target attribute value of the weak reference will receive a strong reference to the object representative of the object. However, it is checked to check it before using the object because it may have been reclaimed. If you get null (VB.NET is nothing), indicating that the object has been recycled, can no longer be used, need to be reassigned. If it is not null, you can use it with confidence.

Let's see another version of WeakReference, please see code 3:

Code 3 // Pub

li

C weakreference

// Object Target,

// Bool TrackResurRecion

//); object obj1 = new object (); object obj2 = new object (); weakreference wref1 = new weakreference (obj1, false); weakreference Wref2 = new weakreference (Obj2, true);

Another version of WeakReference has two parameters, the first parameter, and the version we used in front. The second parameters let's take a look at his prototype, Bool TrackResurRection, tracking resurrection, is a BOOL type, is whether it is tracking resurrection. I mentioned the need for Fina in front of the article.

li

ZE's object will have a resurrection before the final release, and we can probably guess the meaning of the second parameter. If we give false, this weak reference is a short weak reference. When GC reclaims, it is found that there is no reference to this object in the root, and it is considered that this object is useless. At this time, the shortage reference is The track of this object is to this, and the weak reference TARGET is set to NULL. The constructor version of the previous parameter is newly constructed for weak references. If the second parameter is given to True, this weak reference is a long Weak Reference. Fina in the object

li

The Target is available until the ZE method is not performed. However, this is some member variables of the object may have been recycled, so use it to be careful.

Let us now look at how weakReference is implemented. Obviously WeakReference cannot directly reference the target object, the get / set of WeakReference's target attribute is two functions, and returns from somewhere to the target object, instead of our most often written directly or set a private variable. GC maintains two lists to track the target objects of two weak references. When you create a WeakReference object, find a location in the corresponding list, put the reference to the target object, is clear, these two lists are not root a part of. When the GC is recovered, if you want to reclaim an object, you will check the list of weak references. If you save this object, set it to NULL. l Control GC behavior

.NET provides the System.gc class to control the behavior of the GC, GC only provides a static method, without having to (GC's constructor is made private) creates its instance.

The most important way to provide by the GC class is Collect, which makes it possible to control memory recovery. There are two versions of the Collect method, Void Collect (); and Void Collect (int) ;. The second version of Collect provides a parameter that allows you to choose to recover the generation and the object of the young person, that is, gc.collect (0) only reclaims the 0th generation of objects, and gc.collect ( 1) It is to recover the 0th generation and the first generation of objects. Collect () is a recovery of all objects, equivalent to gc.collection (gc.maxgeneration). MaxGeneration is the only attribute of GC, which gives the highest generation of GC.

The GC class provides another way to get the generation of an object, getGeneration. Code 4 gives an example code that allows us to better understand the two methods provided by Generation and GC. Please see code 4:

Code 4 Class GcDemoclass

{

~ Gcdemoclass ()

{

Console.writeline ("Demo Class Fina

li

ZING ... ");

}

}

Static void

Main

(String [] ARGS)

{

GCDemoclass Instance ();

Console.Writeline ("Generation of Demo Object: {0}", gc.get generative (inst));

Gc.collect ();

Console.Writeline ("Generation of Demo Object: {0}", gc.get generative (inst));

Gc.collect ();

Console.Writeline ("Generation of Demo Object: {0}", gc.get generative (inst));

INST = NULL;

Gc.collect (0);

Console.writeline ("After Collect Generation 0 ...");

Gc.collect (1);

Console.writeline ("After Collect Generation 1 ...");

Gc.collect (2);

Console.writeline ("After Collect Generation 2 ...");

Console.readline ();

}

GCDemoClass implements a destructor, according to the previous article, the compiler will change it to Finali

The ZE method is called before the memory of the instance of this class is officially recovered. We call new gcdemoclass () called object a, then it is the 0th generation object, because INST saves a strong reference, so the first two Collect will not recycle object A, and with Collect, The generation of the object A also increases, and after the second Collect, the object A becomes a second-generation object. Next, INST = NULL abandon the strong reference to the object A, but since Collect (0) and Collect (1) are not recycled because of the second-generation object. Finally, collect (2) is executed, the object A does not recycle, and its FINA

li

The ZE method is also called. The results of this code execution are as follows:

GC also provides some other ways, this is no longer discussed, you can go see MSDN.

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

New Post(0)