2019独角兽企业重金招聘Python工程师标准>>>

From Understanding Weak References, by Ethan Nicholas:

Weak references

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);

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.

...

Soft references

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 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.

And Peter Kessler added in a comment:

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.

转载于:https://my.oschina.net/u/138995/blog/223633

difference softreference and weakreference相关推荐

  1. Java中的SoftReference和WeakReference有什么区别?

    本文翻译自:What's the difference between SoftReference and WeakReference in Java? java.lang.ref.WeakRefer ...

  2. java中的softreference_Java中的SoftReference和WeakReference有什么区别?

    问题 Java中的SoftReference和WeakReference有什么区别?它们有什么不同? #1 热门回答(806 赞) 弱引用弱引用,简单地说,是一个不足以强制对象保留在内存中的引用.弱引 ...

  3. SoftReference和WeakReference

    Java2增强了内存管理功能, 增加了一个java.lang.ref包,其中定义了三种引用类.这三种引用类分别为SoftReference.WeakReference和PhantomReference ...

  4. java softreference_Java引用总结--StrongReference、SoftReference、WeakReference、PhantomReference...

    Java引用总结--StrongReference.SoftReference.WeakReference.PhantomReference 1 Java引用介绍 Java从1.2版本开始引入了4种引 ...

  5. 深入理解StrongReference,SoftReference, WeakReference和PhantomReference

    Java 中一共有 4 种类型的引用 : StrongReference. SoftReference. WeakReference 以及 PhantomReference (传说中的幽灵引用 呵呵) ...

  6. Java 引用分类:StrongReference、SoftReference、WeakReference、PhantomReference

    一,定义 在Java中,引用的定义是:如果reference类型的数据中存储的数值代表的是另一块内存的起始地址,就称这块内存代表着一个引用.后面在JDK1.2开始,引用的概念被扩充,引用被分为强引用( ...

  7. java中的softreference_Java中内存优化的SoftReference 和 WeakReference浅析

    一.引用对象类型定义 首先,引用对象在Java定义中有三种类型,从弱到强依次为:软引用.弱引用与虚引用,三种级别也各有所不同(软引用>弱引用).本文浅析下软引用与弱引用.大概的解释,软引用适合应 ...

  8. java中SoftReference与WeakReference应用于高速缓存示例

    前言: 本文首先介绍强引用StrongReference.软引用SoftReference.弱引用WeakReference与虚引用PhantomReference之间的区别与联系: 并通过一个高速缓 ...

  9. weakreference_Java中WeakReference,SoftReference,PhantomReference和Strong Reference之间的区别...

    weakreference 很长一段时间以来,WeakReference和SoftReference都已添加到Java API中,但是并不是每个Java程序员都熟悉它. 这意味着在Java中使用Wea ...

  10. Java中WeakReference,SoftReference,PhantomReference和Strong Reference之间的区别

    很长一段时间以来,WeakReference和SoftReference都已添加到Java API中,但是并不是每个Java程序员都熟悉它. 这意味着在Java中使用WeakReference和Sof ...

最新文章

  1. Git Merge VS Rebase
  2. DL之CNN:利用自定义DeepConvNet【7+1】算法对mnist数据集训练实现手写数字识别并预测(超过99%)
  3. java instanceof翻译_Java 中的instanceof简单讲解
  4. 【转贴】想应聘的瞧仔细了:HW分析大全
  5. 前端学习(2784):首页轮播图的渲染
  6. overleaf创建表格
  7. 怎么看曲线有没有斜渐近线_?成考结束后,怎么看你有没有被录取?
  8. js学习笔记 chapter5 引用类型
  9. 删除数据所有的表和存储过程
  10. Flash中的计时器
  11. linux下curl安装报错symbol lookup error
  12. java做抽奖小程序_随机抽奖小程序
  13. PhotonServer简介(Yanlz+Unity+Photon+PUN+BOLT+UNet+KBEngine+立钻哥哥+CCU+MMO+WWW+==)
  14. 【ceph】ceph分布式存储MDS(各种状态、源码)
  15. 小米air2se耳机只有一边有声音怎么办_小米真无线蓝牙耳机Air2 SE体验:花小钱也能办大事...
  16. 《人性的弱点》(一)
  17. python释放变量内存_Python变量内存管理
  18. python 删除文件夹下面的所有文件python删除文件夹下面的所有文件夹及子文件
  19. 曝各城市娶妻成本:深圳208万 程序员成为“妻奴”?
  20. RK3399平台开发系列讲解(PCI/PCI-E)5.55、PCIE RC枚举EP过程

热门文章

  1. jq ajax购物车,jquery制作的移动端购物车效果完整示例
  2. @pathvariable参数_SpringMVC 五种注解参数绑定
  3. 转载:相同版本的JVM和Java应用,在 x86 和AArch64 平台性能相差30%
  4. UOS系统JAVA应用在任务栏显示类名的问题跟踪调用
  5. 面对技术难题,都是硬着头皮向前冲
  6. 高手的作用在于解决难题
  7. 下划线(Underline)与低线(Lowline)的差异
  8. 安卓的自动启动设置界面的启动代码
  9. SecureCRT的Backspace显示为^H的解决办法
  10. ios不能保存png_ios转一加8t使用体验随用随更新