什么是“实际后果”?使用软引用? [英] What are the "practical consequences" of using soft references?

查看:99
本文介绍了什么是“实际后果”?使用软引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Guava的 MapMaker.softValues()


警告:在大多数情况下,最好设置每个缓存的最大大小而不是使用软引用。如果您熟悉软引用的实际后果,则应该只使用此方法。


我对软引用有中等的理解 - 他们的行为,用途和与垃圾收集的合同。不过,我想知道这些文件暗示的实际后果是什么。为什么使用最大尺寸而不是软引用更好?在实现缓存方面,不要使用软引用的算法和行为使得它们的使用效率更高,硬编码最大限度吗?解析方案

p>我认为他们所指的也是,如果你使用软参考地图,你应该为最大内存使用量和潜在的更多gc活动做好准备,因为只有在需要释放内存时引用gc'd。

如果您知道只需要缓存中的最后n个值,那么使用LRU缓存是一种更简单的方法,对于正在运行的应用程序来说,资源使用的可预测性更高。



此外,根据,看起来在-server和-client JVM之间的行为有细微差别。


Sun JRE确实将SoftReferences
与WeakReferences不同。如果
没有对可用的
内存施加压力,那么
试图持有对象由SoftReference引用的
。一个细节:
-client和-serverJRE的策略是
不同的:-client JRE尝试
让你的脚印小
倾向于清除SoftReferences
而不是扩展堆,而
服务器JRE通过优先选择
扩展堆(如果可能)而不是扩展堆
清除SoftReferences。一种尺码
不适合所有人。



Per the documentation for Guava's MapMaker.softValues():

Warning: in most circumstances it is better to set a per-cache maximum size instead of using soft references. You should only use this method if you are well familiar with the practical consequences of soft references.

I have an intermediate understanding of soft references - their behavior, uses, and their contract with garbage collection. However I'm wondering what these practical consequences are which the doc alludes to. Why exactly is it better to use maximum size rather than soft references? Don't the algorithms and behavior of soft references make their use more efficient that a hardcoded ceiling, in terms of implementing a cache?

解决方案

I think that all they are alluding too is that you should be prepared for maximum memory usage, and potentially more gc activity, if you use a Soft reference map, since references are only gc'd as memory needs to be freed up.

If you know you only need the last n values in the cache then using a LRU Cache is a leaner approach, with more predictable resource usage for a running application.

Furthermore, according to this, it seems there are subtle differences in behaviour between -server and -client JVM's.

The Sun JRE does treat SoftReferences differently from WeakReferences. We attempt to hold on to object referenced by a SoftReference if there isn't pressure on the available memory. One detail: the policy for the "-client" and "-server" JRE's are different: the -client JRE tries to keep your footprint small by preferring to clear SoftReferences rather than expand the heap, whereas the -server JRE tries to keep your performance high by preferring to expand the heap (if possible) rather than clear SoftReferences. One size does not fit all.

这篇关于什么是“实际后果”?使用软引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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