Java 中软引用的用例是什么?

发布于 2024-08-24 20:35:38 字数 155 浏览 4 评论 0原文

Java 中软引用的用例是什么?当 JVM 内存不足时,对非关键项目进行垃圾收集是否有用,以便在关闭 JVM 之前释放足够的资源来转储关键信息?

它们被称为软引用吗,因为它们很软,并且在“承受压力”时会中断,即:JVM 内存不足。我了解弱引用和幻象引用,但在需要这些时却不太了解。

What is a use case for a soft reference in Java? Would it be useful to garbage collect non-critical items when a JVM has run out of memory in order to free up enough resources to perhaps dump critical information before shutting down the JVM?

Are they called soft-references in they they are soft and break when "put under stress" ie:the JVM has run out of memory. I understand weak references and phantom references but not really when these would be needed.

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

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

发布评论

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

评论(4

北凤男飞 2024-08-31 20:35:43

一种用途是缓存。想象一下,您想要维护大型对象的内存缓存,但不希望该缓存消耗可用于其他目的的内存(因为缓存始终可以重建)。通过维护对对象的软引用的缓存,JVM 可以释放引用的对象,并将它们占用的内存重新用于其他目的。缓存只需要在遇到损坏的软引用时清除它们即可。

另一种用途可能是在内存有限的设备(例如移动电话)上维护应用程序映像。当用户打开应用程序时,先前的应用程序映像可以保留为软引用,以便在需要内存用于其他用途时可以将其清除,但如果不需要内存,则它们仍然存在。如果内存没有压力,这将允许用户更快地返回到应用程序,并且如果需要执行其他操作,则允许回收前一个应用程序的内存。

One use is for caching. Imagine you want to maintain an in-memory cache of large objects but you don't want that cache to consume memory that could be used for other purposes (for the cache can always be rebuilt). By maintaining a cache of soft-references to the objects, the referenced objects can be freed by the JVM and the memory they occupied reused for other purposes. The cache would merely need to clear out broken soft-references when it encounters them.

Another use may be for maintaining application images on a memory-constrained device, such as a mobile phone. As the user opens applications, the previous application images could be maintained as soft-references so that they can be cleared out if the memory is needed for something else but will still be there if there is not demand for memory. This will allow the user to return to the application more quickly if there is no pressure on memory and allow the previous application's memory to be reclaimed if it is needed for something else.

叹梦 2024-08-31 20:35:43

这个文章让我对它们中的每一个都有了很好的理解(弱引用、软引用和幻像引用)。以下是总结的引用:

弱引用,简单来说,是指强度不足以强制对象保留在内存中的引用。弱引用允许您利用垃圾收集器的能力来确定您的可达性,因此您不必自己执行此操作。

软引用与弱引用完全相同,只是它不太急于丢弃它所引用的对象。只能弱可达的对象(对它的最强引用是WeakReferences)将在下一个垃圾收集周期中被丢弃,但软可达的对象通常会保留一段时间。

幻像引用与SoftReferenceWeakReference有很大不同。它对其对象的控制非常脆弱,您甚至无法检索该对象 - 它的 get() 方法始终返回 null。这种引用的唯一用途是跟踪它何时排入ReferenceQueue,因为此时您就知道它指向的对象已死亡。

This article gave me a good understanding of each of them (weak, soft and phantom references). Here's a summarized cite:

A weak reference, simply put, is a reference that isn't strong enough to force an object to remain in memory. Weak references allow you to leverage the garbage collector's ability to determine reachability for you, so you don't have to do it yourself.

A soft reference is exactly like a weak reference, except that it is less eager to throw away the object to which it refers. An object which is only weakly reachable (the strongest references to it are WeakReferences) will be discarded at the next garbage collection cycle, but an object which is softly reachable will generally stick around for a while.

A phantom reference is quite different than either SoftReference or WeakReference. Its grip on its object is so tenuous that you can't even retrieve the object -- its get() method always returns null. The only use for such a reference is keeping track of when it gets enqueued into a ReferenceQueue, as at that point you know the object to which it pointed is dead.

梦里梦着梦中梦 2024-08-31 20:35:43

我能想到的最好的例子就是缓存。如果内存出现问题,您可能不介意转储缓存中最旧的条目。缓存大型对象图也可能使这种情况成为可能。

The best example I can think of is a cache. You might not mind dumping the oldest entries in the cache if memory became a problem. Caching large object graphs might make this likely as well.

朮生 2024-08-31 20:35:43

有关如何将 SoftReference 用作缓存的示例,请参见 发布

An example of how a SoftReference can be used as a cache can be found in this post.

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