.NET 中的 WeakReference 实现

发布于 2024-07-26 13:03:27 字数 377 浏览 1 评论 0原文

我理解并欣赏 System.WeakReference 类的用处.NET 框架,但我对实现细节感到好奇。

WeakReference 在 .NET 中是如何实现的? MSDN 详细讨论了 WeakReference 的用法,但几乎没有我所看到的有关其幕后工作原理的细节。

CLR 如何跟踪引用并知道在收集目标时清空内部句柄,而不阻止 GC? 它是否需要 CLR 本身进行特殊处理?

我主要关心的是使用弱引用(尤其是使用许多弱引用时)是否会对性能产生与使用标准对象引用不同的影响。

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

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.

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴者 2024-08-02 13:03:27

WeakReference 类将其对象引用交给 GC 并取回句柄。 每当您获取引用或检查引用是否处于活动状态时,都会使用句柄向 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.

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.

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.

尘曦 2024-08-02 13:03:27

你提到了MSDN; 你已经看过这篇文章了吗?

http://msdn.microsoft.com/en-us/magazine/bb985011.aspx

另请参阅同一作者 (Jeffrey Richter) 所著的《应用 Microsoft .NET Framework 编程》中的第 19 章。 本章介绍垃圾收集,并有一节介绍 WeakReference 的内部结构。

一般来说,如果您在 WeakReferences 中访问大量目标,那么性能会受到影响,因为 WeakRef 在返回目标之前做了一些工作(主要是为了线程安全)。 这显然不如直接使用对象引用便宜。 另一方面,在存储对大对象的引用时,您可以获得一些性能,因为当出现内存问题时,垃圾收集器有更多选项。

我从未尝试过量化这种权衡,也不知道这里有任何参考资料。 显然,它根据应用程序的不同而有所不同。

You mentioned MSDN; have you already seen this article?

http://msdn.microsoft.com/en-us/magazine/bb985011.aspx

Also check out chapter 19 in "Applied Microsoft .NET Framework Programming" by the same author (Jeffrey Richter). The chapter is on garbage collection and has a section on WeakReference internals.

In general, if you're accessing a lot of Targets within WeakReferences, then there's a performance hit simply because the WeakRef does some work (mostly for thread safety) before returning the target. This is obviously not as cheap as using the object reference directly. On the other hand, you gain some performance when storing references to large objects, since the garbage collector has more options when memory considerations arise.

I've never tried to quantify this trade off, or know of any references here. Obviously it varies somewhat depending on the application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文