C # weak references

Source: Internet
Author: User

1. 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.

2. The weak reference still allows garbage collection of the object while referencing the object.

3. An object actually exists. It can be used as n objects.

4. The following content in msdn:

The "Garbage Collection" mechanism of the Common Language Runtime Library recycles the inaccessible ("inaccessible") memory allocated to the object. If all references to an object become invalid (for example, setting those references to null references (nothing in Visual Basic), the object becomes inaccessible.

Garbage collection will call the Finalize method of the object (that is, execute "final"). If such a method exists, it will instruct you to do so. After the termination, if you re-obtain and reuse the garbage collection object in some way, unpredictable results will occur.

The directly or indirectly referenced object is "reachable" and cannot be recycled by the garbage collector. References to reachable objects are called "strong references ".

"Weak reference" also references reachable objects (called "targets "). You can assign the target attribute value to a variable to obtain a strong reference to the target. However, if there is no strong reference to the target, the target will become eligible for garbage collection, even if it still has weak references.

There may be a delay between the time when the object changes to meet the garbage collection condition and the time when it is recycled. If you try to retrieve the target after it has been recycled, only null references will be retrieved. If the target has not been recycled, a valid reference will be retrieved.

The object can be used (that is, "living"); it becomes unused and meets the garbage collection condition (that is, "dead "); then it is used again ("resurrected "). However, access to the Resurrected object after the end will lead to unpredictable behavior.

The weakreference object can specify whether to retain the reference to its target after the end; that is, whether the weak reference should track the target's resurrection. Weak references that do not trace the resurrection are called "short weak references", and weak references that follow the resurrection are called "Long weak references ".

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.