弱引用理解 [英] WeakReference understanding

查看:34
本文介绍了弱引用理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建所有 ViewModel 的字典.

I want to create the dictionary of all the ViewModels.

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

像这样添加

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

并像这样调用所需的方法..

And calling the required method like this..

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

我需要将它维护为 WeakReference 吗?如果我不将它维护为 WeakReference 会产生什么后果.

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

推荐答案

不使用 WeakReference 的唯一后果是字典中的引用将阻止 View Model 实例被垃圾收集.WeakReference 允许垃圾收集(假设其他地方没有其他可靠的引用).

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

当一个项目没有对它的引用时,它就有资格进行垃圾回收.WeakReference 不会创建可数"引用,因此您可以保留对它的某种引用,但如果您的 WeakReference 是唯一的,仍然让它符合条件左看.

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.

您是否需要它实际上取决于您的视图模型具有什么样的生命周期.如果他们需要处理或以其他方式放手",那么您可能需要使用 WeakReference 或公开一种从字典中删除引用的方法.

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.

正如我在评论中提到的.我倾向于错误地使用 WeakReference 而不是明确处理相关对象的生命周期.也就是说,当您根本不了解相关点的生命周期时,它们很有用.我认为在您的情况下,您应该具有必要的可见性,因为这些都可能在 UI 层中,因此应该尝试使用它们.

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:

指南摘自上述 MSDN 链接:

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.

这篇关于弱引用理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆