弱引用的其他用途?

发布于 2024-08-05 03:04:26 字数 286 浏览 3 评论 0原文

我知道弱引用是记忆潜在的大量数据的良好候选者,并且维基百科关于弱引用的文章仅列出了“跟踪应用程序中引用的当前变量”和声明“弱引用的另一个用途是编写缓存”。

在其他哪些情况下(比“缓存结果”更具体),使用弱引用是一个好主意TM

I know that weak references are a good candidate for memoizing potentially large sets of data, and Wikipedia's article on weak references only lists "keeping track of the current variables being referenced in the application" and the statement "Another use of weak references is in writing a cache".

What are some other situations (more specific than just "caching results") where the use of weak references is A Good IdeaTM?

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

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

发布评论

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

评论(4

素食主义者 2024-08-12 03:04:26

弱引用的主要正确用途是识别其重要性源自对它们的强引用的存在。两种最常见的情况是:

  • 对象持有对某事物的引用,不是因为它“关心”相关对象,而是因为关心该对象的其他实体可能希望它用它做某事。如果一段时间后没有人再关心该对象,那么其他实体就没有理由继续代表“所有关心它的实体”来操作它。

  • 保存对同一不可变对象的许多引用的内存成本可能比保存对许多相同对象的引用的内存成本低得多,并且比较对同一对象的引用可能比比较相同对象要快得多。创建不可变对象、放弃它、收集它以及创建相同对象的内存成本本质上与创建对象并随后返回对其的第二个引用的成本相同。返回对现有对象的引用无论如何都必须保留是一个巨大的胜利;返回对符合收集条件但尚未被收集的对象的引用可能是也可能不是胜利(通常是轻微的胜利,但在分代 GC 中有时会稍微损害性能);在许多情况下,后者的好处不足以证明使对象保持活动时间比原本必要的时间更长。

The primary correct use for weak references is to identify things whose importance derives from the existence of strong references to them. The two most common scenarios are either:

  • An object holds a reference to something not because it "cares" about the object in question, but rather because other entities which do care about the object might want it to do something with it. If after awhile nobody cares about the object anymore, there's no reason why other entities should continue to manipulate it on behalf of "all the entities that care about it".

  • The memory cost of holding many references to the same immutable object may be much lower than the memory cost of holding references to many identical objects, and comparing references to the same object may be be much faster than comparing identical objects. The memory cost of creating an immutable object, abandoning it, having it get collected, and creating an identical object is essentially the same as the cost of creating an object and later returning a second reference to it. Returning a reference to an existing object which would have to be retained anyway is a big win; returning a reference to an object which was eligible for collection but hadn't been collected yet may or may not be a win (it's usually a slight win, but in a generational GC it can sometimes hurt performance slightly); in many cases, the latter benefits would not be sufficient to justify keeping an object alive longer than would otherwise be necessary.

浮云落日 2024-08-12 03:04:26

在 Flex 中,使用弱引用来避免内存泄漏

如果事件处理程序是短期实例对象的成员,则将处理程序作为对寿命较长的对象的强引用传递可能会不必要地使短期实例保持活动状态。

In Flex, use weak references to avoid memory leaks.

If an event handler is a member of a short-lived instance object, passing the handler as a strong reference to an object which will live longer may keep the short-lived instance alive unnecessarily.

骄傲 2024-08-12 03:04:26

我对一些事情使用弱引用......

我喜欢在.Net 中创建“弱事件”以避免可观察量使观察者存活时间过长。

我还使用了弱事件 检测内存泄漏

I use weak references for a few things...

I like to create "Weak Events" in .Net to avoid observables from keeping observers alive too long.

I have also used weak events to detect memory leaks.

暗地喜欢 2024-08-12 03:04:26

在Python中,垃圾收集器使用引用计数来决定何时“销毁”或以其他方式释放对象。普通的循环引用可能会导致对象永远不会被垃圾回收,因为它们的引用计数分别保持在 1 或更高;但是使用弱引用< /a> 将允许两个/所有对象在超出范围时被正确清理。

In Python, the garbage collector uses reference counting to decide when to "destroy" or otherwise deallocate an object. A normal circular reference can result in objects never being garbage collected because their reference counts respectively stay at 1 or higher; but using weak references will allow both/all objects to be properly cleaned up when they go out of scope.

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