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和PhantomReference分析和比较 在 Java 中,引用类型分为强引用、软引用、弱引用和虚引用四种。强引用是我们最常用的引用类型,而软引用、弱引用和虚引用则是 Java 为我们提供的三种...
StrongReference,SoftReference, WeakReference的使用实例,请参照博客:http://blog.csdn.net/To_be_Designer/article/details/72673421
5. 使用WeakReference和SoftReference: WeakReference和SoftReference可以帮助开发者更好地管理应用程序中的对象生命周期。 OOM错误的常见解决方法 1. 使用System.gc():System.gc()可以强制垃圾回收器回收垃圾...
4. 使用WeakReference和SoftReference,WeakReference和SoftReference可以帮助避免内存泄漏。 Java的垃圾回收机制可以自动回收内存中的垃圾,避免内存泄漏,但是程序员需要注意避免内存泄漏的几点,以确保程序的...
Java从1.2版本开始引入了四种引用,分别是强引用(StrongReference)、软引用(SoftReference)、弱引用(WeakReference)和虚引用(PhantomReference)。这四种引用的级别由高到低依次为:强引用 > 软引用 > 弱引用...
4. WeakReference 与 SoftReference: WeakReference 和 SoftReference 都可以用于提高 GC 和内存的效率,但是 WeakReference 一旦失去最后一个强引用,就会被 GC 回收,而软引用虽然不能阻止被回收,但是可以延迟到...
- 使用WeakReference和SoftReference:当需要引用一些可能引发内存泄漏的对象时,可以使用WeakReference或SoftReference,使得对象能在不被使用的状态下被垃圾回收。 4. **源码分析**: - `JavaApk源码说明.txt`...
对于那些可能引起内存泄漏的引用,可以考虑使用WeakReference或SoftReference。WeakReference在对象无其他强引用时会立即被回收,而SoftReference在系统内存紧张时才会被回收。 8. 观察者模式与内存泄漏: 注册...
3. **WeakReference与SoftReference**:使用WeakReference或SoftReference持有Bitmap或其他大型对象,当内存不足时,这些引用的对象会优先被回收。 4. **Memory Analyzer Tool (MAT)**:Android提供的内存分析工具...
该Demo主要原理: 加载图片时先查看缓存中时候存在该图片,如果存在则返回该图片,否则先加载载一个...并且处理的多个细节,包括使用WeakReference、SoftReference防止内存溢出、解决使用Handler时context泄露问题 登
1. **使用WeakReference或SoftReference**:在Volley或其他库中,可以考虑使用WeakReference或SoftReference来持有Activity引用,这样当Activity销毁时,引用会被自动清除,避免内存泄漏。 2. **正确管理生命周期**...
- 使用WeakReference和SoftReference:避免强引用导致的对象无法回收,可以使用WeakReference或SoftReference弱化对象引用。 - 避免过度绘制:减少不必要的视图层次,优化布局,防止GPU过度绘制。 - 资源复用:例如...
WeakReference和SoftReference可以创建对对象的弱引用和软引用,它们不会阻止垃圾回收器清理对象。在需要临时存储对象但又不希望影响其生命周期的情况下,可以考虑使用这两种引用类型。 **避免无限制的递归** 无限...
使用WeakReference和SoftReference可以帮助开发者实现弱引用和软引用,让对象在内存不足时能被及时回收,防止内存泄漏。 4. **Bitmap优化** Bitmap是Android中消耗内存的主要因素之一。通过合理设置Bitmap的配置...
8. **WeakReference与SoftReference**:使用WeakReference或SoftReference可以弱化对象引用,让垃圾收集器更容易回收。 9. **内存分析工具**:Android Studio提供了内存分析工具,通过实时监控内存分配和使用,找出...
1. **WeakReference和SoftReference**:在Presenter中,可以使用WeakReference或SoftReference持有对View的引用,这样当View被回收时,Presenter不会阻止其被垃圾回收。 2. **Activity和Fragment的生命周期**:确保...