在.NET中的WeakReference实施 [英] WeakReference implementation in .NET

查看:156
本文介绍了在.NET中的WeakReference实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解和AP preciate的 System.WeakReference 类在.NET框架,但很好奇,实施细则。

I understand and appreciate the usefulness of the System.WeakReference class in the .NET framework, but am curious as to the implementation details.

如何WeakReference的在.NET中实现的? MSDN讨论了WeakReference的详细使用方法,但我已经看到了这是如何工作的引擎盖下的小细节。

How is WeakReference implemented in .NET? MSDN discusses the usage of WeakReference in detail, but has little details that I've seen on how this works under the hood.

如何在CLR跟踪参考,知道空出时,我们的目标是收集到的内部处理,而不preventing的GC?是否需要进行特殊处理的CLR本身?

How does the CLR track the reference and know to null out the internal handle when the Target is collected, without preventing the GC? Does it require special handling in the CLR itself?

我最关心的是是否有(使用其中许多,特别是如果),从那些使用标准的对象引用的不同,在WeakReferences使用性能的影响。

My main concern would be whether there are performance implications of using WeakReferences (especially if using many of them) that differ from those of using standard object references.

推荐答案

本的WeakReference类的手离开其对象引用到GC并得到回手柄。每当你得到的引用或检查的参考是活的,把手是问的GC进行参考。

The WeakReference class hands off its object reference to the GC and gets a handle back. Whenever you get the reference or check if the reference is alive, the handle is used to ask the GC for the reference.

这意味着,在GC保持所有弱引用,它具有当被收集对象以更新的列表。这也意味着,有一些开销,每次使用弱引用的时间。

This means that the GC keeps a list of all weak references, which it has to update when objects are collected. It also means that there is some overhead every time you use a weak reference.

所以,每一个弱引用意味着垃圾收集多一点的工作,但另一方面也是如此每个经常引用过,即使是较少的。当然,你应该会有点小心使用了很多弱引用,但是如果你需要得到的内存管理与你的对象正常工作,应该outweight小的开销,它的原因。

So, every weak reference means a little more work for the garbage collector, but on the other hand so does every regular reference too, even if it's less. You should of course be a bit careful about using a lot of weak references, but if you need that to get the memory management to work well with your objects, that should outweight the small overhead that it causes.

这篇关于在.NET中的WeakReference实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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