WeakReference (weak reference) let the GC recycle objects when needed _ practical tips

Source: Internet
Author: User
We usually use a strong reference to the object, and if a strong reference exists, the GC does not reclaim the object. Can we keep a reference to the object at the same time and let the GC recycle the object when it's needed? NET provides the weakreference to implement. A weak reference allows you to keep a reference to an object while allowing the GC to free up objects and reclaim memory if necessary. Consider using weak references for those who create cheap but memory-intensive objects that want to keep the object, and to use it when the application needs it, and you want the GC to recycle when necessary. Weak references are simple to use, look at the following code:
Copy Code code as follows:

Object obj = new Object ();
WeakReference wref = new WeakReference (obj);
obj = null;

The first line of code creates a new object, which is called the object a,obj is a strong reference to object A. Then the second line of code creates a new weak reference object, which is the strong reference to object A, and the third line of code releases a strong reference to object A. At this point, if the GC is recycled, object A is reclaimed.
How do you get a strong reference to object a? Quite simply, see Code 2:
Copy Code code as follows:

Object obj2 = wref. Target;
if (obj2!= null)
{
Do what you want to do.
}
Else
{
Object has been reclaimed, if you want to use a new one must be created.
}

A strong reference to the object represented by the weak reference is provided as long as the value appended to the target attribute of the weak reference is displayed. However, the usability of an object is checked before it is used because it may have been reclaimed. If you get a null (vb.net under Nothing), it means that the object has been reclaimed, no longer available, and needs to be reassigned. If not NULL, you can be assured of bold use.
Next, let's look at another version of WeakReference, see Code 3:
Copy Code code 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 version of WeakReference has two parameters, the first one being the same as the version we used earlier. Second argument let's take a look at his prototype, BOOL Trackresurrection, tracking the resurrection, is a bool type, or whether to follow the resurrection. In the previous article I mentioned that a Finalize object would be resurrected before the final release, and we could probably guess the meaning of the second argument. If our second argument is false, this weak reference is a short weak reference (weak reference), and when GC is recycled, it finds that the object is not referenced in the root, which is considered useless, when the short weak reference ends with the trace of the object, and the weakly referenced Target is set to null. The new weak reference for the constructor version of one of the preceding arguments is a short weak reference. If the second argument gives true, the weak reference is a long weak reference (the weak reference). Target is available until the Finalize method of the object has not been executed. However, some of the object's member variables may have been recycled, so use it to be careful.

Now let's see how WeakReference is implemented. Obviously WeakReference cannot directly refer to the target object, and the WeakReference target attribute Get/set is two functions, where a reference to the target object is returned, rather than being returned directly or set a private variable as we write most often. The GC maintains two lists to track the target object of the two weak references, when a WeakReference object is created, it finds a location in the corresponding list, puts the reference of the target object, and obviously, the two lists are not part of the root. When a GC makes a memory recycle, if you want to recycle an object, it checks the list of weak references and sets it to null if the object's reference is saved.
Copy Code code 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 (this) (new WeakReference);
}
}
}

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.