`
tmrp
  • 浏览: 44762 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

weakReference vs softReference

阅读更多
weak reference
    A weak reference is one that does not prevent the referenced object from being garbage collected. You might use them to manage a HashMap to look up a cache of objects. A weak reference is a reference that does not keep the object it refers to alive. A weak reference is not counted as a reference in garbage collection. If the object is not referred to elsewhere as well, it will be garbage collected.

    The GC will send some sort of "finalize" message to the object and then set any weakly-referencing variables to null whenever it disposes of the referenced object. This allows "finalization" logic to be run before the object is disposed of (e.g., close a file if still open, commit any open transaction(s), etc.). Java 1.1 does not support weak references other than via an undocumented Ref class that is not supported under Netscape. Weak references arrived officially with JDK 1.2. Java has three kinds of weak references, called soft references, weak references, and phantom references, in order of increasing weakness.

    Java has four orders of strength in holding onto Objects. In descending order from strongest to weakest they are:

       1. The JVM holds onto regular Objects until they are no longer reachable by either clients or any container. In other words Objects are garbage collected when there are no more live references to them. Dead references don’t count.
          garbage collection
       2. Soft references can be deleted from a container if the clients are no longer referencing them and memory is tight.
       3. Weak references are automatically deleted from a container as soon clients stop referencing them.
       4. Phantom references point to objects that are already dead and have been finalised.

                                            
Soft vs Weak vs Phantom References
Type Purpose Use When GCed Implementing Class
Strong Reference An ordinary reference. Keeps objects alive as long as they are referenced. normal reference. Any object not pointed to can be reclaimed. default
Soft Reference Keeps objects alive provided there’s enough memory. to keep objects alive even after clients have removed their references (memory-sensitive caches), in case clients start asking for them again by key. After a first gc pass, the JVM decides it still needs to reclaim more space. java.lang.ref.SoftReference
Weak Reference Keeps objects alive only while they’re in use (reachable) by clients. Containers that automatically delete objects no longer in use. After gc determines the object is only weakly reachable java.lang.ref.WeakReferencejava.util.WeakHashMap
Phantom Reference Lets you clean up after finalization but before the space is reclaimed (replaces or augments the use of finalize()) Special clean up processing After finalization. java.lang.ref.PhantomReference

http://mindprod.com/jgloss/weak.html
分享到:
评论

相关推荐

    SoftReference、WeakReference和PhantomRefrence分析和比较

    SoftReference、WeakReference和PhantomReference分析和比较 在 Java 中,引用类型分为强引用、软引用、弱引用和虚引用四种。强引用是我们最常用的引用类型,而软引用、弱引用和虚引用则是 Java 为我们提供的三种...

    StrongReference,SoftReference, WeakReference的使用实例

    StrongReference,SoftReference, WeakReference的使用实例,请参照博客:http://blog.csdn.net/To_be_Designer/article/details/72673421

    00M原因以及分析处理

    5. 使用WeakReference和SoftReference: WeakReference和SoftReference可以帮助开发者更好地管理应用程序中的对象生命周期。 OOM错误的常见解决方法 1. 使用System.gc():System.gc()可以强制垃圾回收器回收垃圾...

    Java中内存泄露及垃圾回收机制参照.pdf

    4. 使用WeakReference和SoftReference,WeakReference和SoftReference可以帮助避免内存泄漏。 Java的垃圾回收机制可以自动回收内存中的垃圾,避免内存泄漏,但是程序员需要注意避免内存泄漏的几点,以确保程序的...

    Java引用总结--StrongReference、SoftReference、WeakReference、PhantomRef

    Java从1.2版本开始引入了四种引用,分别是强引用(StrongReference)、软引用(SoftReference)、弱引用(WeakReference)和虚引用(PhantomReference)。这四种引用的级别由高到低依次为:强引用 > 软引用 > 弱引用...

    JVM 77 道面试题及答案.docx

    4. WeakReference 与 SoftReference: WeakReference 和 SoftReference 都可以用于提高 GC 和内存的效率,但是 WeakReference 一旦失去最后一个强引用,就会被 GC 回收,而软引用虽然不能阻止被回收,但是可以延迟到...

    Android 图片下载以及内存处理防止OOM内存溢出 源码

    - 使用WeakReference和SoftReference:当需要引用一些可能引发内存泄漏的对象时,可以使用WeakReference或SoftReference,使得对象能在不被使用的状态下被垃圾回收。 4. **源码分析**: - `JavaApk源码说明.txt`...

    博客 工作中遇到的Android内存优化问题demo

    对于那些可能引起内存泄漏的引用,可以考虑使用WeakReference或SoftReference。WeakReference在对象无其他强引用时会立即被回收,而SoftReference在系统内存紧张时才会被回收。 8. 观察者模式与内存泄漏: 注册...

    安卓Android源码——图片下载以及内存处理防OOM.zip

    3. **WeakReference与SoftReference**:使用WeakReference或SoftReference持有Bitmap或其他大型对象,当内存不足时,这些引用的对象会优先被回收。 4. **Memory Analyzer Tool (MAT)**:Android提供的内存分析工具...

    Android异步加载图片Demo

    该Demo主要原理: 加载图片时先查看缓存中时候存在该图片,如果存在则返回该图片,否则先加载载一个...并且处理的多个细节,包括使用WeakReference、SoftReference防止内存溢出、解决使用Handler时context泄露问题 登

    Android Volley,AsyncTask,FinalHttp 请求对比内存泄漏

    1. **使用WeakReference或SoftReference**:在Volley或其他库中,可以考虑使用WeakReference或SoftReference来持有Activity引用,这样当Activity销毁时,引用会被自动清除,避免内存泄漏。 2. **正确管理生命周期**...

    Android学习资料之内存优化.zip

    - 使用WeakReference和SoftReference:避免强引用导致的对象无法回收,可以使用WeakReference或SoftReference弱化对象引用。 - 避免过度绘制:减少不必要的视图层次,优化布局,防止GPU过度绘制。 - 资源复用:例如...

    基于Android防止内存溢出浅析

    WeakReference和SoftReference可以创建对对象的弱引用和软引用,它们不会阻止垃圾回收器清理对象。在需要临时存储对象但又不希望影响其生命周期的情况下,可以考虑使用这两种引用类型。 **避免无限制的递归** 无限...

    Android-memroy.zip_android_android 优化

    使用WeakReference和SoftReference可以帮助开发者实现弱引用和软引用,让对象在内存不足时能被及时回收,防止内存泄漏。 4. **Bitmap优化** Bitmap是Android中消耗内存的主要因素之一。通过合理设置Bitmap的配置...

    Android应用源码之防止内存溢出浅析.zip

    8. **WeakReference与SoftReference**:使用WeakReference或SoftReference可以弱化对象引用,让垃圾收集器更容易回收。 9. **内存分析工具**:Android Studio提供了内存分析工具,通过实时监控内存分配和使用,找出...

    Android MVP 架构设计 (一)优化、抽取

    1. **WeakReference和SoftReference**:在Presenter中,可以使用WeakReference或SoftReference持有对View的引用,这样当View被回收时,Presenter不会阻止其被垃圾回收。 2. **Activity和Fragment的生命周期**:确保...

Global site tag (gtag.js) - Google Analytics