Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WeakReference understanding

I want to create the dictionary of all the ViewModels.

   public static Dictionary<string, WeakReference> vmCollection = new Dictionary<string, WeakReference>();

Adding it like this

 vmCollection.Add(name, new WeakReference(viewModel));

And calling the required method like this..

((vmCollection[viewModel].Target) as BaseViewModel).NewMessage(message);

Do I need maintain it as a WeakReference? What could be the consequences if I don't maintain it as a WeakReference.

like image 325
Learner Avatar asked Jun 07 '12 08:06

Learner


People also ask

What is the use of WeakReference?

A weak reference permits the garbage collector to collect the object while still allowing the application to access the object. A weak reference is valid only during the indeterminate amount of time until the object is collected when no strong references exist.

What is the use of WeakReference in Java?

Weak reference objects, which do not prevent their referents from being made finalizable, finalized, and then reclaimed. Weak references are most often used to implement canonicalizing mappings. Suppose that the garbage collector determines at a certain point in time that an object is weakly reachable.

What's the difference between SoftReference and WeakReference?

WeakReference: is used to hold an object which will become eligible for the garbage collection as soon as it is not reachable by the program. SoftReference: lives longer, it will only be garbage collected before an OutOfMemoryError is thrown.

What is a WeakReference Android?

WeakReference: a weak reference is a reference not strong enough to keep the object in memory. If we try to determine if the object is strongly referenced and it happened to be through WeakReferences, the object will be garbage-collected.


1 Answers

The only consequence of not using a WeakReference is that the reference in your dictionary will prevent the View Model instances from being garbage collected. A WeakReference allows garbage collection (assuming there are no other solid references elsewhere).

An item becomes eligible for garbage collection when it has no references to it. WeakReference does not create a "countable" reference, thus you can keep a sort-of-reference to it, but still let it be eligible if your WeakReference is the only thing left looking at it.

Whether you need it or not really depends on what sort of life-cycle your View Models have. If they need disposing or otherwise "letting go of", then you may need to use WeakReference or expose a way to remove the reference from the dictionary instead.

As I mention in the comments. I tend to err away from using WeakReference as opposed to handling the life-cycle of the relevant objects explicitly. That said, they are useful when you simply don't have visibility of the life-cycle at the relevant points. I think in your situation, you should have the necessary visibility, as these are all likely in the UI layer, and thus should try to not use them.

Here is a resource on the topic:

  • Weak References MSDN article

Guidelines extract from the above MSDN link:

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.

I believe the last guideline point applies to your situation.

like image 177
Adam Houldsworth Avatar answered Sep 20 '22 14:09

Adam Houldsworth