Weakreference in C)

Source: Internet
Author: User
We usually use strong references of objects. If strong references exist, GC will not recycle objects. Can we keep the reference to the object at the same time, and let the GC reclaim this object as needed ?. NET provides weakreference for implementation. Weak references allow you to retain references to objects, and allow GC to release objects and reclaim memory when necessary. For those objects that create cheap but consume a large amount of memory, that is, you want to keep this object, andProgramIf you want to recycle GC when necessary, you can use weak references. Weak references are easy to use. Code :
Object OBJ = new object ();
Weakreference Wref = new weakreference (OBJ );
OBJ = NULL;

The first line of code creates a new object called object A. obj is a strong reference to object. The second line of code creates a weak reference object. The parameter is the strong reference of object A, and the third line of code releases the strong reference to object. If GC is recycled, object A is recycled.
How can we obtain strong references from object? Very simple. Please refer to Code 2:

Object obj2 = Wref. target;
If (obj2! = NULL)
{
// Do what you want to do.
}
Else
{
// The object has been recycled. If you want to use it, you must create a new one.
}

As long as the displayed target attribute with a weak reference value is attached, a strong reference of the object represented by the weak reference will be obtained. However, before using an object, check its availability because it may have been recycled. If you get null (nothing in VB. NET), it indicates that the object has been recycled and cannot be reused. You need to allocate another object. If it is not null, you can use it with confidence.
Next, let's take a look at another version of weakreference. See Code 3:
// Public weakreference (
// Object target,
// Bool trackresurrection
//);

Object obj1 = new object ();

Object obj2 = new object ();

Weakreference wref1 = new weakreference (obj1, false );

Weakreference wref2 = new weakreference (obj2, true );

another weakreference version has two parameters. The first parameter is the same as the previous version. Let's take a look at its prototype, bool trackresurrection. Tracking and resurrection is a bool type, that is, whether to trace the resurrection. In the previous Article , I mentioned that objects requiring finalize will be revived once before they are finally released, we can probably guess the meaning of the second parameter. If the second parameter is set to false, this weak reference is a short weak reference (short weak reference). When GC is collected, it is found that this object is not referenced in the root, this object is considered useless. At this time, the trace of the short weak reference object ends, and the target of the weak reference is set to null. The weak reference created by the constructor version of the previous parameter is short and weak reference. If the second parameter is true, this weak reference is a long weak reference ). Target is available until the Finalize method of the object is not executed. However, some member variables of the object may have been recycled, so be careful when using them.
now let's see how weakreference is implemented. Obviously, weakreference cannot directly reference the target object. The get/set function of the Target attribute of weakreference is two functions, and the reference of the target object is returned from somewhere, instead of directly returning or setting a private variable as we usually write. GC maintains two column tables to track the target objects with two weak references. When a weakreference object is created, it finds a position in the corresponding list and places the reference of the target object, obviously, these two lists are not part of the root. When GC is used to recycle memory, if an object is to be recycled, the list of weak references will be checked. If the reference of this object is saved, it will be set to null.
public class asppage: page
{< br> Private Static arraylist _ enclist = new arraylist ();

[Debuggernonusercode]
Public asppage ()
{
Base. Load + = new eventhandler (this. page_load );
Arraylist list = _ enclist;
Lock (list)
{
_ Enclist. Add (New weakreference (this ));
}
}

}

Reprinted from: http://www.cnblogs.com/bayonetxxx/archive/2009/06/02/1494728.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.