WeakReference 可以与 String 一起使用吗?

发布于 2024-08-05 05:05:22 字数 85 浏览 4 评论 0原文

在 .NET 3.5 中,WeakReference 是否可以与 String 一起使用,还是应该将其包装在一个小“类”中以使其与 String 一起使用?

In .NET 3.5, Does WeakReference work with String or shall I wrap it in a small "class" to make it work with it?

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

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

发布评论

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

评论(3

謸气贵蔟 2024-08-12 05:05:22

System.String(C# 中的string)是一种引用类型,因此没有理由它不能与 弱引用。忽略它在某些方面是一种特殊引用类型(首先是不可变的)这一事实 - CLR 将其视为引用类型。

话虽如此,另一个 StackOverflow 线程很好地说明了字符串引用在许多情况下都可以被“保留”,并且因此,没有观察到垃圾收集的“预期”行为。

System.String (string in C#) is a reference type, so there is no reason it should not work perfecctly well with WeakReference. Ignore the fact that it is in some ways a special reference type (immutable, for a start) - the CLR sees it as a reference type.

Saying this, this other StackOverflow thread makes a good point that string references can be 'interned' in many situations, and therefore the "expected" behaviour with regards to garbage collection is not observed.

对你而言 2024-08-12 05:05:22

这肯定可以与 WeakReference 一起使用,没有任何问题,因为 System.String 是一个简单的引用类型。看到您使用 Wea​​kReference 的用例会很有趣,因为它似乎不适合 WeakReference 的“正常”用法。

来自 MSDN 指南

使用长弱引用仅在必要时才使用,因为对象的状态在终结后是不可预测的。

避免对小对象使用弱引用,因为指针本身可能很大或更大。

避免使用弱引用作为内存管理问题的自动解决方案。相反,开发一个有效的缓存策略来处理应用程序的对象。

This will definitely work with WeakReference without any problems as System.String is a simple reference type. It would be interesting to see your use case for using a WeakReference as it doesn't seem to fit the "normal" uses of WeakReference.

From the MSDN Guidelines:

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.

雨落星ぅ辰 2024-08-12 05:05:22

使用弱引用来指向深度不可变的对象通常没有多大意义,尤其是不引用任何其他对象的对象。如果数据有用,你应该持有强有力的参考。如果没有用,你就不应该持有任何参考。仅当弱引用对象的有用性取决于强引用的存在时,才真正适合使用 Wea​​kReference;最常见的情况是使用弱引用将信息放入将通过强引用读取的对象中。如果没有人会读取放入对象中的信息,那么作者就没有理由再费心了。

There generally isn't much point to using a weak reference to point to a deeply-immutable object, especially one which doesn't reference any other objects. If the data is useful, you should hold a strong reference. If it's not useful, you shouldn't hold any reference. The use of WeakReference is only really appropriate in cases where the usefulness of the weakly-referenced object is contingent upon the existence of a strong reference; the most common such case is where a weak reference is used to put information into an object which will be read via strong reference. If nobody's going to read the information put into the object, there's no reason for the writer to bother with it anymore.

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