Java 中的 SoftReference 和 WeakReference 有什么区别? [英] What's the difference between SoftReference and WeakReference in Java?

查看:26
本文介绍了Java 中的 SoftReference 和 WeakReference 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.lang.ref.WeakReferencejava.lang.ref.SoftReference ?

推荐答案

来自 了解弱引用,作者 Ethan Nicholas:

From Understanding Weak References, by Ethan Nicholas:

弱引用

弱引用,简单地说,是一个引用不够强强制对象保留在内存中.弱引用允许您利用垃圾收集器的能力确定您的可达性,因此您不必自己做.你创建一个像这样的弱引用:

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. You create a weak reference like this:

WeakReference weakWidget = new WeakReference(widget);

然后您可以在代码中的其他地方使用weakWidget.get() 获取实际Widget 对象.当然是弱者参考不够强防止垃圾收集,所以你可以找到(如果没有强对小部件的引用)weakWidget.get() 突然启动返回 null.

and then elsewhere in the code you can use weakWidget.get() to get the actual Widget object. Of course the weak reference isn't strong enough to prevent garbage collection, so you may find (if there are no strong references to the widget) that weakWidget.get() suddenly starts returning null.

...

软引用

软引用就像一个弱引用,除了它更少渴望扔掉物体它指的是.一个对象是只有微弱可达(最强的对它的引用是 WeakReferences)将被丢弃在下一个垃圾收集周期,但一个对象通常可以轻柔地到达坚持一段时间.

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.

SoftReferences 不是必需的表现得与WeakReferences,但在实践中软可达对象通常是只要有记忆就一直保留货源充足.这使他们成为缓存的绝佳基础,例如作为上述图像缓存,因为你可以让垃圾收藏家担心两者如何可到达的对象是(强烈可达对象将永远被删除从缓存中)以及它的需求有多严重他们消耗的内存.

SoftReferences aren't required to behave any differently than WeakReferences, but in practice softly reachable objects are generally retained as long as memory is in plentiful supply. This makes them an excellent foundation for a cache, such as the image cache described above, since you can let the garbage collector worry about both how reachable the objects are (a strongly reachable object will never be removed from the cache) and how badly it needs the memory they are consuming.

Peter Kessler 在评论中补充说:

And Peter Kessler added in a comment:

Sun JRE 确实将 SoftReferences 与 WeakReferences 区别对待.如果可用内存没有压力,我们会尝试保留由 SoftReference 引用的对象.一个细节:-client"和-server"JRE 的策略是不同的:-client JRE 试图通过更喜欢清除 SoftReferences 而不是扩展堆来保持你的足迹较小,而 -server JRE 试图保持你的通过更喜欢扩展堆(如果可能)而不是清除 SoftReferences 来提高性能.一种尺寸并不适合所有人.

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.

这篇关于Java 中的 SoftReference 和 WeakReference 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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