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

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

问题描述

java.lang.ref.WeakReference java.lang.ref.SoftReference ?

推荐答案

来自

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确实将SoftReference与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天全站免登陆