您的位置:首页 > 编程语言 > Java开发

Java 四种引用类型

2015-02-12 17:56 459 查看
先看javadoc自己的介绍:

java.lang.ref.Reference<T>:

Provides an abstract class which describes behavior common to all reference objects. It is not possible to create immediate subclasses of Reference in addition to the ones provided by this package. It is also not desirable to do so, since references require
very close cooperation with the system's garbage collector. The existing, specialized reference classes should be used instead.

所有引用对象的基类,不建议直接引用,应该用具体的如下几个子类。

Three different type of references exist, each being weaker than the preceding one: java.lang.ref.SoftReference, java.lang.ref.WeakReference, and java.lang.ref.PhantomReference. "Weakness" here means
that less restrictions are being imposed on the garbage collector as to when it is allowed to actually garbage-collect the referenced object.

三种不同引用类型,SoftReference,WeakReference,PhantomReference.一个比一个弱。当GC时一个比一个更少的限制。

In order to use reference objects properly it is important to understand the different types of reachability that trigger their clearing and enqueueing. The following table lists these, from strongest to weakest. For each row,
an object is said to have the reachability on the left side if (and only if) it fulfills all of the requirements on the right side. In all rows, consider the root set to be a set of references that are "resistant" to garbage collection (that is, running
threads, method parameters, local variables, static fields and the like).

为了正确使用引用对象,去理解不同类型的引用当触发他们的清理和入队列时的可达性这很重要。下边的表的可达性从强到弱。

Strongly reachable

- There exists at least one path from the root set to the object that does not traverse any instance of a java.lang.ref.Reference subclass.

Softly reachable

- The object is not strongly reachable.

- There exists at least one path from the root set to the object that does traverse a java.lang.ref.SoftReference instance, but no java.lang.ref.

WeakReference or java.lang.ref.PhantomReference instances. Weakly reachable

- The object is neither strongly nor softly reachable.

- There exists at least one path from the root set to the object that does traverse a java.lang.ref.WeakReference instance, but no java.lang.ref.PhantomReference instances.

Phantom-reachable

- The object is neither strongly, softly, nor weakly reachable.

- The object is referenced by a java.lang.ref.PhantomReference instance.

- The object has already been finalized.
http://blog.csdn.net/coding_or_coded/article/details/6603549
⑴强引用(StrongReference)

强引用是使用最普遍的引用。如果一个对象具有强引用,那垃圾回收器绝不会回收它。当内存空间不足,Java虚拟机宁愿抛出OutOfMemoryError错误,使程序异常终止,也不会靠随意回收具有强引用的对象来解决内存不足的问题。 ps:强引用其实也就是我们平时A
a = new A()这个意思。

⑵软引用(SoftReference)

如果一个对象只具有软引用,则内存空间足够,垃圾回收器就不会回收它;如果内存空间不足了,就会回收这些对象的内存。只要垃圾回收器没有回收它,该对象就可以被程序使用。软引用可用来实现内存敏感的高速缓存(下文给出示例)。

软引用可以和一个引用队列(ReferenceQueue)联合使用,如果软引用所引用的对象被垃圾回收器回收,Java虚拟机就会把这个软引用加入到与之关联的引用队列中。

⑶弱引用(WeakReference)

弱引用与软引用的区别在于:只具有弱引用的对象拥有更短暂的生命周期。在垃圾回收器线程扫描它所管辖的内存区域的过程中,一旦发现了只具有弱引用的对象,不管当前内存空间足够与否,都会回收它的内存。不过,由于垃圾回收器是一个优先级很低的线程,因此不一定会很快发现那些只具有弱引用的对象。

弱引用可以和一个引用队列(ReferenceQueue)联合使用,如果弱引用所引用的对象被垃圾回收,Java虚拟机就会把这个弱引用加入到与之关联的引用队列中。

⑷虚引用(PhantomReference)

“虚引用”顾名思义,就是形同虚设,与其他几种引用都不同,虚引用并不会决定对象的生命周期。如果一个对象仅持有虚引用,那么它就和没有任何引用一样,在任何时候都可能被垃圾回收器回收。

虚引用主要用来跟踪对象被垃圾回收器回收的活动。虚引用与软引用和弱引用的一个区别在于:虚引用必须和引用队列 (ReferenceQueue)联合使用。当垃圾回收器准备回收一个对象时,如果发现它还有虚引用,就会在回收对象的内存之前,把这个虚引用加入到与之 关联的引用队列中。

ReferenceQueuequeue
=new ReferenceQueue ();

PhantomReferencepr =new
PhantomReference (object,queue);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: