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分析和比较
StrongReference,SoftReference, WeakReference的使用实例,请参照博客:http://blog.csdn.net/To_be_Designer/article/details/72673421
该Demo主要原理: 加载图片时先查看缓存中时候存在该图片,如果存在则返回该图片,否则先加载载一个...并且处理的多个细节,包括使用WeakReference、SoftReference防止内存溢出、解决使用Handler时context泄露问题 登
3.1 强引 3.2 软引 3.3 弱引 3. 4 假象引
1.强引用1.强引用 2.软引用(SoftReference) 3.弱引用(WeakReference) 4.虚引用(PhantomReference) 等等;
周期无法控制可以采用SoftReference,WeakReference,PhantomReference这三种对象来执行(看了Ibatis的缓存机制才发现JDK居然还提供了PhantomReference这玩意儿,得恶补基础啊),这三种都是弱引用,区别在于强度...
ava有四种引用类型,strongreference,softreference,weakreference,phantomreference。本篇文档主要的就是进阶的介绍和了解这四种引用类型的异同点,助于你对java的更好的学习理解
前述:除了强引用外,其他引用不是我们所常见的 new出来的对象,而是需要借用3个类SoftReference软引用,WeakReference弱引用,PhantomReference虚引用 1.强引用 概述:即发生OOM(Out Of Memory)内存空间满了也...
├─Google Guava 第29讲-SoftReference,WeakReference,PhantomReference精讲.wmv ├─Google Guava 第30讲-SoftReference加LRU算法实现InMemoryCache.wmv ├─Google Guava 第31讲-Guava之CacheLoader,...
他们分别是强引用(StrongReference),软引用(SoftReference),弱引用(WeakReference)以及PhantomReference(虚引用),他们被 GC回收的可能性从小到大排列。 强引用(StrongReference) 只要强引用存在,垃圾回收器将...
很早Java API就添加了弱引用(WeakReference)和软引用(SoftReference),但并不是所有的程序员都熟悉这两个概念
软引用(SoftReference) 23 弱引用(WeakReference) 23 虚引用(PhantomReference) 24 【基础】final, finally, finalize的区别 24 【基础】Java 中定义常量的几种方法 25 【基础】什么时候使用字节流?什么时候...
基本数据类型的包装类 •八大数据类型的包装类分别为:Byte、Short、Integer、Long、Character、 Float、Double、Boolean。 把基本数据类型变量包装类实例是通过对应包装类的构造器来实现的,不仅如此,8个...