WeakReference (weak reference) allows GC to recycle objects as needed

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 the object and use it when the application needs it. If you want GC to recycle it when necessary, you can consider using weak references. Weak references are easy to use. See the following code:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
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:
Copy codeThe Code is as follows:
// 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.
Copy codeThe Code is as follows:
Public class AspPage: Page
{
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 ));
}
}
}

Related Article

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.