7

I know the knowledge below:

A weak reference permits the garbage collector to collect the object while still allowing the application to access the object.

So if the object has been reclaimed, you have to create it again when necessary.

Then, what's the difference between short weak reference and long weak reference? I think of it as below:(according to the msdn)

short weak reference: if GC reclaim the object, the object is really released.

long weak reference: if GC reclaim the object, the object is still existed (as it is cached).

So can someone tell me more detail?

roast_soul
  • 3,554
  • 7
  • 36
  • 73

1 Answers1

4

Short

The target of a short weak reference becomes null when the object is reclaimed by garbage collection. The weak reference is itself a managed object, and is subject to garbage collection just like any other managed object. A short weak reference is the default constructor for WeakReference.

Long

A long weak reference is retained after the object's Finalize method has been called. This allows the object to be recreated, but the state of the object remains unpredictable. To use a long reference, specify true in the WeakReference constructor.

If the object's type does not have a Finalize method, the short weak reference functionality applies and the weak reference is valid only until the target is collected, which can occur anytime after the finalizer is run.

To establish a strong reference and use the object again, cast the Target property of a WeakReference to the type of the object. If the Target property returns null, the object was collected; otherwise, you can continue to use the object because the application has regained a strong reference to it.

Guidelines for Using Weak References

Use long weak references only when necessary as the state of the object is unpredictable after finalization. Avoid using weak references to small objects because the pointer itself may be as large or larger.

Avoid using weak references as an automatic solution to memory management problems. Instead, develop an effective caching policy for handling your application's objects.

Reference

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
Amit
  • 15,217
  • 8
  • 46
  • 68
  • I already known this article,but my question is if GC reclaim the object, the object is still existed ??? – roast_soul Jul 09 '13 at 08:20
  • 2
    I wonder why that document so poorly describes what's actually going on? A short weak reference will be invalidated either when its target becomes eligible for immediate finalization or ceases to exist, or when the weak reference *itself* becomes eligible for finalization. A long weak reference will be invalidated when its target ceases to exist, or when the weak reference itself becomes eligible for finalization. Note that weak references may be invalidated even while their targets are live--a point not mentioned in the documentation! – supercat Sep 19 '13 at 22:45
  • @supercat So a short AND a long weak refenrece both, have the same conditions to get invalidated: when their target ceases to exist or the weak reference itself gets collected! What's the difference? – Shahryar Saljoughi Jul 13 '20 at 07:53
  • @ShahryarSaljoughi: I just described the difference. An object which becomes eligible for immediate finalization will need to continue to exist until that finalization is complete, but short weak references to it will be invalidated immediately. – supercat Jul 13 '20 at 10:45
  • What if object has no finalizer? What's the difference between short and long WeakReference then? – kaalus Jun 07 '23 at 22:14