About weak references in C #

Source: Internet
Author: User

Previous part of this article from: http://www.cnblogs.com/mokey/archive/2011/11/24/2261605.html

After dividing the line for the author to supplement the section.

One: What is a weak reference

Before you learn about weak references, find out what a strong reference is

For example: Object Obj=new object (); is a strong reference, memory allocation of a space to store the object data, the memory has a first address, that is, obj saved data, memory allocation of space not only holds object object information, but also holds itself (the object itself) is referenced by the number of times.

When an object is created in a strongly referenced form, the number of references itself is already 1.

then object o=obj; After this code is executed, the storage space of the object that obj points to has been referenced 2 times, so the referenced value of object holds 2.

Summary: The end result of a strong reference is the number of references to the referenced object +1;

The opposite weak reference does not have any effect on the referenced number of referenced objects.

Second: What is the role of weak references

Prevent memory leaks.

Object Obj=new object ();

When you access the resources above the network asynchronously, the time required may be longer, before the data is returned, the user is likely to turn to other pages, if the asynchronous Access object (obj) is a strong reference to a local object (object), then the asynchronous Access object (obj) Before being released, that is, the referenced object is not destroyed until the data is called back, which causes the memory to be occupied.

WeakReference weakobj=new WeakReference (Object);

You can now use weak references, weak reference objects (Weakobj) to make asynchronous requests, and before callbacks, if the user is going to go to another page, the referenced object (object) can be freed, so there is no memory that is always occupied.

Three: How to use weak references

Weakly quoted classes: WeakReference//constructors with two overloads

WeakReference weakobj=new WeakReference (Object);//Weak reference mode

The IsAlive property is to determine whether the object referenced by this weak reference object still exists and exists: isalive=true;

The target property is the value that sets the data object referenced by the weak reference object

------------------------------------------------------------------------------

When you learn the C # garbage collector, you see a summary of the GC as follows:

Summary of garbage collection:

1, theCLR provides a generational, flag-cleared GC, using the tag-clearing algorithm to garbage collection and memory compression of different age-old objects, to ensure the efficiency of operations and optimization of execution.

2, an object is not referenced by any other object, this object is considered recyclable

3, it is best not to call the GC. Collection to enforce garbage collection

4, garbage objects are not immediately executed memory cleanup,GC can do garbage collection at any time

5.consider using weak references to "fat" objects to improve new energy

The weak Reference object test code is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace weakreferencetest{class Program {static void Main (string[] args) {Weakreferenc            Etestclass Xiaobao = new Weakreferencetestclass ("Xiaobao");            Weakreferencetestclass Xiaoyu = new Weakreferencetestclass ("Xiaoyu");            WeakReference weakreference = new WeakReference (Xiaobao);            Weakreferencetestclass result = ((Weakreferencetestclass) weakreference.target); Console.Write (result. Name + "This year" + result. Age + "Old!"            "); Target: Gets or sets the current system.            Object weakreference.target = Xiaoyu applied by reference object;            result = ((Weakreferencetestclass) weakreference.target); Console.Write (result. Name + "This year" + result. Age + "Old!"            "); IsAlive:System.Reference whether the object applied by the weak reference object has been reclaimed--whether the object referenced by the weak reference object still exists true: there is a false: bool weakrefereceisalive = WEA            kreference.isalive;Console.Write ("The object referenced by the System.reference weak reference object is reclaimed by garbage collection period:" + (weakrefereceisalive? ")            Has not been recycled ":" has been recycled "));        Console.ReadLine ();        }} public class Weakreferencetestclass {public int: age {get; set;}        public string Name {get; set;} Public Weakreferencetestclass (Int., string name) {this.            Age = age; This.        name = name; }    }}

  

About weak references in C #

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.