Disclaimer: This blog post is translated from: http://tipsandtricks.runicsoft.com/CSharp/WeakReferences.html
Due to the level (technical level + English comprehension ability) Limited/insufficient, certainly will have the omission/the mistake, please correct in time.
In daily development, it is common to encounter some large object processing. These large objects are usually used multiple times throughout the program. For example: Large file object, large dictionary class. Typically we will use the following method:
Built-in local variables as a method;
exists as a field of a class;
Neither of these approaches is very good. As a field of a class, an instance of the class will always hold the large object and consume a lot of memory; As a local variable of a method, when the method executes, the large object leaves the scope, but it does not have to be recycled by the GC at this time. Cause unnecessary memory consumption. And each time the method is called, a new large object is recreated, increasing the memory consumption of the program.
If creating an object consumes resources, and we want to avoid creating the same object multiple times. You can use the field methods of the class.
Typically, when an object is out of scope or is set to null. We will not be able to access it. NET provides a WeakReference class that can solve this problem perfectly.
The WeakReference object will hold a reference to an object even though the object has already left the scope or is set to NULL, this object can also be accessed through WeakReference. The WeakReference object cannot access the object at this time unless it is reclaimed by GC.
The use of WeakReference is very simple,
New Myhugeobject (); WeakReference W = new weakreference (Hugeobject);
The associated object can be obtained through the target property of the WeakReference.
Myhugeobject = W.target as Myhugeclass;
If the object still exists, it can be obtained by using the target property. If the object has been reclaimed by GC, Target gets a value of NULL
StaticvoidFunc () {Myhugeclass myhugeobject; if(W = =NULL) || ((Myhugeobject=w.target asMyhugeclass) = =NULL) ) {Myhugeobject=NewMyhugeclass (); W=NewWeakReference (Myhugeobject); } //Work with Myhugeobject}
WeakReference provides us with great benefits: it will keep the Myhugeobject reference and ensure that it can be retrieved without being reclaimed by GC, without worrying that the re-acquired object is incomplete or corrupted.
PS: Using WeakReference does not guarantee the performance of the program, and in most cases performance improves compared to using local local variables. But do not make any warranties. In some cases, there will be more program performance consumption, such as a large object associated with many small objects, these small objects will also be transferred to weak Reference. There is a loss of GC memory recovery.
Reference Links:
Https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/weak-references
Http://tipsandtricks.runicsoft.com/CSharp/WeakReferences.html
Https://msdn.microsoft.com/en-us/library/system.weakreference (v=vs.110). aspx
[. NET] Use of WeakReference