本文介绍对象的强、软、弱和虚引用的概念、应用及其在UML中的表示。
1.对象的强、软、弱和虚引用
在JDK1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象。也就是说,只有对象处于可触及(reachable)状态,程序才能使用它。从JDK1.2版本开始,把对象的引用分为4种级别,从而使程序能更加灵活地控制对象的生命周期。这4种级别由高到低依次为:强引用、软引用、弱引用和虚引用。图1为对象应用类层次。
图1
⑴强引用(StrongReference)<wbr style="line-height:25px"><br style="line-height:25px"> 强引用是使用最普遍的引用。如果一个对象具有强引用,那垃圾回收器绝不会回收它。当内存空间不足,Java虚拟机宁愿抛出OutOfMemoryError错误,使程序异常终止,也不会靠随意回收具有强引用的对象来解决内存不足的问题。<br style="line-height:25px"><span style="line-height:25px">⑵软引用(SoftReference)</span><br style="line-height:25px"> 如果一个对象只具有软引用,则内存空间足够,垃圾回收器就不会回收它;如果内存空间不足了,就会回收这些对象的内存。只要垃圾回收器没有回收它,该对象就可以被程序使用。软引用可用来实现内存敏感的高速缓存(下文给出示例)。<br style="line-height:25px"> 软引用可以和一个引用队列(<span style="color:#993300; line-height:25px">ReferenceQueue</span>)联合使用,如果软引用所引用的对象被垃圾回收器回收,Java虚拟机就会把这个软引用加入到与之关联的引用队列中。<br style="line-height:25px"><span style="line-height:25px">⑶弱引用(WeakReference)</span><br style="line-height:25px"> 弱引用与软引用的区别在于:只具有弱引用的对象拥有更短暂的生命周期。在垃圾回收器线程扫描它所管辖的内存区域的过程中,一旦发现了只具有弱引用的对象,不管当前内存空间足够与否,都会回收它的内存。不过,由于垃圾回收器是一个优先级很低的线程,因此不一定会很快发现那些只具有弱引用的对象。<br style="line-height:25px"><span style="color:#000080; line-height:25px">弱引用可以和一个引用队列(ReferenceQueue)联合使用,如果弱引用所引用的对象被垃圾回收,Java虚拟机就会把这个弱引用加入到与之关联的引用队列中。</span><br style="line-height:25px"><span style="line-height:25px">⑷虚引用(PhantomReference)</span><br style="line-height:25px"> “虚引用”顾名思义,就是形同虚设,与其他几种引用都不同,虚引用并不会决定对象的生命周期。如果一个对象仅持有虚引用,那么它就和没有任何引用一样,在任何时候都可能被垃圾回收器回收。<br style="line-height:25px"> 虚引用主要用来跟踪对象被垃圾回收器回收的活动。<span style="color:#000080; line-height:25px">虚引用与软引用和弱引用的一个区别在于:虚引用必须和引用队列(ReferenceQueue)联合使用</span>。当垃圾回收器准备回收一个对象时,如果发现它还有虚引用,就会在回收对象的内存之前,把这个虚引用加入到与之关联的引用队列中。<br style="line-height:25px"> ReferenceQueuequeue=newReferenceQueue();<br style="line-height:25px"> PhantomReferencepr=newPhantomReference(object,queue);<br style="line-height:25px"> 程序可以通过判断引用队列中是否已经加入了虚引用,来了解被引用的对象是否将要被垃圾回收。如果程序发现某个虚引用已经被加入到引用队列,那么就可以在所引用的对象的内存被回收之前采取必要的行动。<br style="line-height:25px"><span style="line-height:25px">2.对象可及性的判断</span><br style="line-height:25px"> 在很多时候,一个对象并不是从根集直接引用的,而是一个对象被其他对象引用,甚至同时被几个对象所引用,从而构成一个以根集为顶的树形结构。如图2所示<br style="line-height:25px"> 在这个树形的引用链中,箭头的方向代表了引用的方向,所指向的对象是被引用对象。由图可以看出,从根集到一个对象可以由很多条路径。比如到达对象5的路径就有①-⑤,③-⑦两条路径。由此带来了一个问题,那就是某个对象的可及性如何判断:<br style="line-height:25px"> ◆单条引用路径可及性判断:在这条路径中,最弱的一个引用决定对象的可及性。<br style="line-height:25px"> ◆多条引用路径可及性判断:几条路径中,最强的一条的引用决定对象的可及性。<br style="line-height:25px"> 比如,我们假设图2中引用①和③为强引用,⑤为软引用,⑦为弱引用,对于对象5按照这两个判断原则,路径①-⑤取最弱的引用⑤,因此该路径对对象5的引用为软引用。同样,③-⑦为弱引用。在这两条路径之间取最强的引用,于是对象5是一个软可及对象。<br style="line-height:25px"><span style="line-height:25px">3.使用软引用构建敏感数据的缓存</span><br style="line-height:25px"><span style="line-height:25px">3.1为什么需要使用软引用</span><br style="line-height:25px"> 首先,我们看一个雇员信息查询系统的实例。我们将使用一个Java语言实现的雇员信息查询系统查询存储在磁盘文件或者数据库中的雇员人事档案信息。作为一个用户,我们完全有可能需要回头去查看几分钟甚至几秒钟前查看过的雇员档案信息(同样,我们在浏览WEB页面的时候也经常会使用“后退”按钮)。这时我们通常会有两种程序实现方式:一种是把过去查看过的雇员信息保存在内存中,每一个存储了雇员档案信息的Java对象的生命周期贯穿整个应用程序始终;另一种是当用户开始查看其他雇员的档案信息的时候,把存储了当前所查看的雇员档案信息的Java对象结束引用,使得垃圾收集线程可以回收其所占用的内存空间,当用户再次需要浏览该雇员的档案信息的时候,重新构建该雇员的信息。很显然,第一种实现方法将造成大量的内存浪费,而第二种实现的缺陷在于即使垃圾收集线程还没有进行垃圾收集,包含雇员档案信息的对象仍然完好地保存在内存中,应用程序也要重新构建一个对象。我们知道,访问磁盘文件、访问网络资源、查询数据库等操作都是影响应用程序执行性能的重要因素,如果能重新获取那些尚未被回收的Java对象的引用,必将减少不必要的访问,大大提高程序的运行速度。<br style="line-height:25px"><span style="line-height:25px">3.2如何使用软引用</span><br style="line-height:25px"> SoftReference的特点是它的一个实例保存对一个Java对象的软引用,该软引用的存在不妨碍垃圾收集线程对该Java对象的回收。也就是说,一旦SoftReference保存了对一个Java对象的软引用后,在垃圾线程对这个Java对象回收前,<span style="color:#993300; line-height:25px">SoftReference</span>类所提供的<span style="color:#993300; line-height:25px">get()</span>方法返回Java对象的强引用。另外,一旦垃圾线程回收该Java对象之后,get()方法将返回null。<br style="line-height:25px"> 看下面代码:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">MyObjectaRef=new MyObject();<br style="line-height:25px"> SoftReferenceaSoftRef=newSoftReference(aRef);</span><br style="line-height:25px"> 此时,对于这个MyObject对象,有两个引用路径,一个是来自SoftReference对象的软引用,一个来自变量aReference的强引用,所以这个MyObject对象是强可及对象。<br style="line-height:25px"> 随即,我们可以结束aReference对这个MyObject实例的强引用:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">aRef=null;</span><br style="line-height:25px"> 此后,这个MyObject对象成为了软可及对象。如果垃圾收集线程进行内存垃圾收集,并不会因为有一个SoftReference对该对象的引用而始终保留该对象。<span style="color:#000080; line-height:25px">Java虚拟机的垃圾收集线程对软可及对象和其他一般Java对象进行了区别对待:软可及对象的清理是由垃圾收集线程根据其特定算法按照内存需求决定的。也就是说,垃圾收集线程会在虚拟机抛出OutOfMemoryError之前回收软可及对象,而且虚拟机会尽可能优先回收长时间闲置不用的软可及对象,对那些刚刚构建的或刚刚使用过的“新”软可反对象会被虚拟机尽可能保留</span>。在回收这些对象之前,我们可以通过:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">MyObjectanotherRef=(MyObject)aSoftRef.get();</span><br style="line-height:25px"> 重新获得对该实例的强引用。而回收之后,调用get()方法就只能得到null了。<br style="line-height:25px"><span style="line-height:25px">3.3使用ReferenceQueue清除失去了软引用对象的SoftReference</span><br style="line-height:25px"> 作为一个Java对象,SoftReference对象除了具有保存软引用的特殊性之外,也具有Java对象的一般性。所以,当软可及对象被回收之后,虽然这个SoftReference对象的get()方法返回null,但这个SoftReference对象已经不再具有存在的价值,需要一个适当的清除机制,避免大量SoftReference对象带来的内存泄漏。在java.lang.ref包里还提供了<span style="color:#993300; line-height:25px">ReferenceQueue</span>。如果在创建SoftReference对象的时候,使用了一个ReferenceQueue对象作为参数提供给SoftReference的构造方法,如:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ReferenceQueuequeue=new ReferenceQueue();<br style="line-height:25px"> SoftReference ref=new SoftReference(aMyObject,queue);</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">那么当这个SoftReference所软引用的aMyOhject被垃圾收集器回收的同时,ref所强引用的SoftReference对象被列入</span><span style="color:#993300; line-height:25px">ReferenceQueue</span>。也就是说,ReferenceQueue中保存的对象是Reference对象,而且是已经失去了它所软引用的对象的Reference对象。另外从ReferenceQueue这个名字也可以看出,它是一个队列,当我们调用它的poll()方法的时候,如果这个队列中不是空队列,那么将返回队列前面的那个Reference对象。<br style="line-height:25px"><span style="color:#000080; line-height:25px">在任何时候,我们都可以调用ReferenceQueue的poll()方法来检查是否有它所关心的非强可及对象被回收</span>。如果队列为空,将返回一个null,否则该方法返回队列中前面的一个Reference对象。利用这个方法,我们可以检查哪个SoftReference所软引用的对象已经被回收。于是我们可以把这些失去所软引用的对象的SoftReference对象清除掉。常用的方式为:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">SoftReferenceref=null;<br style="line-height:25px"> while((ref=(EmployeeRef)q.poll())!=null){<br style="line-height:25px"> //清除ref<br style="line-height:25px"> }</span><br style="line-height:25px"> 理解了ReferenceQueue的工作机制之后,我们就可以开始构造一个Java对象的高速缓存器了。<br style="line-height:25px"><span style="line-height:25px">3.4、通过软可及对象重获方法实现Java对象的高速缓存</span><br style="line-height:25px"> 利用Java2平台垃圾收集机制的特性以及前述的垃圾对象重获方法,我们通过一个雇员信息查询系统的小例子来说明如何构建一种高速缓存器来避免重复构建同一个对象带来的性能损失。我们将一个雇员的档案信息定义为一个Employee类:<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px">Employee{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateStringid;//雇员的标识号码</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateStringname;//雇员姓名</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateStringdepartment;//该雇员所在部门</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateStringPhone;//该雇员联系电话</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateintsalary;//该雇员薪资</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateStringorigin;//该雇员信息的来源</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">//构造方法</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">Employee(Stringid){<br style="line-height:25px"> this.id=id;<br style="line-height:25px"> getDataFromlnfoCenter();<br style="line-height:25px"> }</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//到数据库中取得雇员信息</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privatevoidgetDataFromlnfoCenter(){</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//和数据库建立连接井查询该雇员的信息,将查询结果赋值<br style="line-height:25px"> //给name,department,plone,salary等变量<br style="line-height:25px"> //同时将origin赋值为"FromDataBase"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> ……<br style="line-height:25px"> 这个Employee类的构造方法中我们可以预见,如果每次需要查询一个雇员的信息。哪怕是几秒中之前刚刚查询过的,都要重新构建一个实例,这是需要消耗很多时间的。下面是一个对Employee对象进行缓存的缓存器的定义:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.lang.ref.ReferenceQueue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.lang.ref.SoftReference;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.Hashtable;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">EmployeeCache</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"> staticprivateEmployeeCachecache;</span><span style="color:#808080; line-height:25px">//一个Cache实例</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateHashtable<String,EmployeeRef>employeeRefs;</span><span style="color:#808080; line-height:25px">//用于Chche内容的存储</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateReferenceQueue<Employee>q;//垃圾Reference的队列</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//继承SoftReference,使得每一个实例都具有可识别的标识。</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privateclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">EmployeeRef</span><span style="color:#3366ff; line-height:25px">extendsSoftReference<Employee>{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateString_key="";</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicEmployeeRef(Employeeem,ReferenceQueue<Employee>q){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super(em,q);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">_key=em.getID();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//构建一个缓存器实例</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">private</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">EmployeeCache</span><span style="color:#3366ff; line-height:25px">(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">employeeRefs=newHashtable<String,EmployeeRef>();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">q=newReferenceQueue<Employee>();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//取得缓存器实例</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicstatic</span><span style="color:#3366ff; line-height:25px">EmployeeCachegetInstance(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(cache==null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cache=newEmployeeCache();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">cache;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//以软引用的方式对一个Employee对象的实例进行引用并保存该引用</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatevoid</span><span style="color:#3366ff; line-height:25px">cacheEmployee(Employeeem){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cleanCache();//清除垃圾引用</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">EmployeeRefref=newEmployeeRef(em,q);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">employeeRefs.put(em.getID(),ref);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//依据所指定的ID号,重新获取相应Employee对象的实例</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">EmployeegetEmployee(StringID){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Employeeem=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//缓存中是否有该Employee实例的软引用,如果有,从软引用中取得。</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(employeeRefs.containsKey(ID)){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">EmployeeRefref=(EmployeeRef)employeeRefs.get(ID);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">em=(Employee)ref.get();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//如果没有软引用,或者从软引用中得到的实例是null,重新构建一个实例,<br style="line-height:25px"> //并保存对这个新建实例的软引用</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(em==null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">em=newEmployee(ID);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("RetrieveFromEmployeeInfoCenter.ID="+ID);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.cacheEmployee(em);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnem;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">privatevoid</span><span style="color:#3366ff; line-height:25px">cleanCache(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">EmployeeRefref=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">((ref=(EmployeeRef)q.poll())!=null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">employeeRefs.remove(ref._key);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//清除Cache内的全部内容</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">clearCache(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cleanCache();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">employeeRefs.clear();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.gc();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.runFinalization();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><br style="line-height:25px"> 注:<span style="color:#000080; line-height:25px">原来ReferenceQueue只是起到一个监听器的效果</span>,当发现SoftReference.get()方法返回的是null值时,就会将SoftReference注册到自己里面队列里,当我们调用ReferenceQueue的poll()方法时,返回并删除该SoftReference。</wbr>
1.对象的强、软、弱和虚引用
在JDK1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象。也就是说,只有对象处于可触及(reachable)状态,程序才能使用它。从JDK1.2版本开始,把对象的引用分为4种级别,从而使程序能更加灵活地控制对象的生命周期。这4种级别由高到低依次为:强引用、软引用、弱引用和虚引用。图1为对象应用类层次。
图1
⑴强引用(StrongReference)<wbr style="line-height:25px"><br style="line-height:25px"> 强引用是使用最普遍的引用。如果一个对象具有强引用,那垃圾回收器绝不会回收它。当内存空间不足,Java虚拟机宁愿抛出OutOfMemoryError错误,使程序异常终止,也不会靠随意回收具有强引用的对象来解决内存不足的问题。<br style="line-height:25px"><span style="line-height:25px">⑵软引用(SoftReference)</span><br style="line-height:25px"> 如果一个对象只具有软引用,则内存空间足够,垃圾回收器就不会回收它;如果内存空间不足了,就会回收这些对象的内存。只要垃圾回收器没有回收它,该对象就可以被程序使用。软引用可用来实现内存敏感的高速缓存(下文给出示例)。<br style="line-height:25px"> 软引用可以和一个引用队列(<span style="color:#993300; line-height:25px">ReferenceQueue</span>)联合使用,如果软引用所引用的对象被垃圾回收器回收,Java虚拟机就会把这个软引用加入到与之关联的引用队列中。<br style="line-height:25px"><span style="line-height:25px">⑶弱引用(WeakReference)</span><br style="line-height:25px"> 弱引用与软引用的区别在于:只具有弱引用的对象拥有更短暂的生命周期。在垃圾回收器线程扫描它所管辖的内存区域的过程中,一旦发现了只具有弱引用的对象,不管当前内存空间足够与否,都会回收它的内存。不过,由于垃圾回收器是一个优先级很低的线程,因此不一定会很快发现那些只具有弱引用的对象。<br style="line-height:25px"><span style="color:#000080; line-height:25px">弱引用可以和一个引用队列(ReferenceQueue)联合使用,如果弱引用所引用的对象被垃圾回收,Java虚拟机就会把这个弱引用加入到与之关联的引用队列中。</span><br style="line-height:25px"><span style="line-height:25px">⑷虚引用(PhantomReference)</span><br style="line-height:25px"> “虚引用”顾名思义,就是形同虚设,与其他几种引用都不同,虚引用并不会决定对象的生命周期。如果一个对象仅持有虚引用,那么它就和没有任何引用一样,在任何时候都可能被垃圾回收器回收。<br style="line-height:25px"> 虚引用主要用来跟踪对象被垃圾回收器回收的活动。<span style="color:#000080; line-height:25px">虚引用与软引用和弱引用的一个区别在于:虚引用必须和引用队列(ReferenceQueue)联合使用</span>。当垃圾回收器准备回收一个对象时,如果发现它还有虚引用,就会在回收对象的内存之前,把这个虚引用加入到与之关联的引用队列中。<br style="line-height:25px"> ReferenceQueuequeue=newReferenceQueue();<br style="line-height:25px"> PhantomReferencepr=newPhantomReference(object,queue);<br style="line-height:25px"> 程序可以通过判断引用队列中是否已经加入了虚引用,来了解被引用的对象是否将要被垃圾回收。如果程序发现某个虚引用已经被加入到引用队列,那么就可以在所引用的对象的内存被回收之前采取必要的行动。<br style="line-height:25px"><span style="line-height:25px">2.对象可及性的判断</span><br style="line-height:25px"> 在很多时候,一个对象并不是从根集直接引用的,而是一个对象被其他对象引用,甚至同时被几个对象所引用,从而构成一个以根集为顶的树形结构。如图2所示<br style="line-height:25px"> 在这个树形的引用链中,箭头的方向代表了引用的方向,所指向的对象是被引用对象。由图可以看出,从根集到一个对象可以由很多条路径。比如到达对象5的路径就有①-⑤,③-⑦两条路径。由此带来了一个问题,那就是某个对象的可及性如何判断:<br style="line-height:25px"> ◆单条引用路径可及性判断:在这条路径中,最弱的一个引用决定对象的可及性。<br style="line-height:25px"> ◆多条引用路径可及性判断:几条路径中,最强的一条的引用决定对象的可及性。<br style="line-height:25px"> 比如,我们假设图2中引用①和③为强引用,⑤为软引用,⑦为弱引用,对于对象5按照这两个判断原则,路径①-⑤取最弱的引用⑤,因此该路径对对象5的引用为软引用。同样,③-⑦为弱引用。在这两条路径之间取最强的引用,于是对象5是一个软可及对象。<br style="line-height:25px"><span style="line-height:25px">3.使用软引用构建敏感数据的缓存</span><br style="line-height:25px"><span style="line-height:25px">3.1为什么需要使用软引用</span><br style="line-height:25px"> 首先,我们看一个雇员信息查询系统的实例。我们将使用一个Java语言实现的雇员信息查询系统查询存储在磁盘文件或者数据库中的雇员人事档案信息。作为一个用户,我们完全有可能需要回头去查看几分钟甚至几秒钟前查看过的雇员档案信息(同样,我们在浏览WEB页面的时候也经常会使用“后退”按钮)。这时我们通常会有两种程序实现方式:一种是把过去查看过的雇员信息保存在内存中,每一个存储了雇员档案信息的Java对象的生命周期贯穿整个应用程序始终;另一种是当用户开始查看其他雇员的档案信息的时候,把存储了当前所查看的雇员档案信息的Java对象结束引用,使得垃圾收集线程可以回收其所占用的内存空间,当用户再次需要浏览该雇员的档案信息的时候,重新构建该雇员的信息。很显然,第一种实现方法将造成大量的内存浪费,而第二种实现的缺陷在于即使垃圾收集线程还没有进行垃圾收集,包含雇员档案信息的对象仍然完好地保存在内存中,应用程序也要重新构建一个对象。我们知道,访问磁盘文件、访问网络资源、查询数据库等操作都是影响应用程序执行性能的重要因素,如果能重新获取那些尚未被回收的Java对象的引用,必将减少不必要的访问,大大提高程序的运行速度。<br style="line-height:25px"><span style="line-height:25px">3.2如何使用软引用</span><br style="line-height:25px"> SoftReference的特点是它的一个实例保存对一个Java对象的软引用,该软引用的存在不妨碍垃圾收集线程对该Java对象的回收。也就是说,一旦SoftReference保存了对一个Java对象的软引用后,在垃圾线程对这个Java对象回收前,<span style="color:#993300; line-height:25px">SoftReference</span>类所提供的<span style="color:#993300; line-height:25px">get()</span>方法返回Java对象的强引用。另外,一旦垃圾线程回收该Java对象之后,get()方法将返回null。<br style="line-height:25px"> 看下面代码:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">MyObjectaRef=new MyObject();<br style="line-height:25px"> SoftReferenceaSoftRef=newSoftReference(aRef);</span><br style="line-height:25px"> 此时,对于这个MyObject对象,有两个引用路径,一个是来自SoftReference对象的软引用,一个来自变量aReference的强引用,所以这个MyObject对象是强可及对象。<br style="line-height:25px"> 随即,我们可以结束aReference对这个MyObject实例的强引用:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">aRef=null;</span><br style="line-height:25px"> 此后,这个MyObject对象成为了软可及对象。如果垃圾收集线程进行内存垃圾收集,并不会因为有一个SoftReference对该对象的引用而始终保留该对象。<span style="color:#000080; line-height:25px">Java虚拟机的垃圾收集线程对软可及对象和其他一般Java对象进行了区别对待:软可及对象的清理是由垃圾收集线程根据其特定算法按照内存需求决定的。也就是说,垃圾收集线程会在虚拟机抛出OutOfMemoryError之前回收软可及对象,而且虚拟机会尽可能优先回收长时间闲置不用的软可及对象,对那些刚刚构建的或刚刚使用过的“新”软可反对象会被虚拟机尽可能保留</span>。在回收这些对象之前,我们可以通过:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">MyObjectanotherRef=(MyObject)aSoftRef.get();</span><br style="line-height:25px"> 重新获得对该实例的强引用。而回收之后,调用get()方法就只能得到null了。<br style="line-height:25px"><span style="line-height:25px">3.3使用ReferenceQueue清除失去了软引用对象的SoftReference</span><br style="line-height:25px"> 作为一个Java对象,SoftReference对象除了具有保存软引用的特殊性之外,也具有Java对象的一般性。所以,当软可及对象被回收之后,虽然这个SoftReference对象的get()方法返回null,但这个SoftReference对象已经不再具有存在的价值,需要一个适当的清除机制,避免大量SoftReference对象带来的内存泄漏。在java.lang.ref包里还提供了<span style="color:#993300; line-height:25px">ReferenceQueue</span>。如果在创建SoftReference对象的时候,使用了一个ReferenceQueue对象作为参数提供给SoftReference的构造方法,如:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">ReferenceQueuequeue=new ReferenceQueue();<br style="line-height:25px"> SoftReference ref=new SoftReference(aMyObject,queue);</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">那么当这个SoftReference所软引用的aMyOhject被垃圾收集器回收的同时,ref所强引用的SoftReference对象被列入</span><span style="color:#993300; line-height:25px">ReferenceQueue</span>。也就是说,ReferenceQueue中保存的对象是Reference对象,而且是已经失去了它所软引用的对象的Reference对象。另外从ReferenceQueue这个名字也可以看出,它是一个队列,当我们调用它的poll()方法的时候,如果这个队列中不是空队列,那么将返回队列前面的那个Reference对象。<br style="line-height:25px"><span style="color:#000080; line-height:25px">在任何时候,我们都可以调用ReferenceQueue的poll()方法来检查是否有它所关心的非强可及对象被回收</span>。如果队列为空,将返回一个null,否则该方法返回队列中前面的一个Reference对象。利用这个方法,我们可以检查哪个SoftReference所软引用的对象已经被回收。于是我们可以把这些失去所软引用的对象的SoftReference对象清除掉。常用的方式为:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">SoftReferenceref=null;<br style="line-height:25px"> while((ref=(EmployeeRef)q.poll())!=null){<br style="line-height:25px"> //清除ref<br style="line-height:25px"> }</span><br style="line-height:25px"> 理解了ReferenceQueue的工作机制之后,我们就可以开始构造一个Java对象的高速缓存器了。<br style="line-height:25px"><span style="line-height:25px">3.4、通过软可及对象重获方法实现Java对象的高速缓存</span><br style="line-height:25px"> 利用Java2平台垃圾收集机制的特性以及前述的垃圾对象重获方法,我们通过一个雇员信息查询系统的小例子来说明如何构建一种高速缓存器来避免重复构建同一个对象带来的性能损失。我们将一个雇员的档案信息定义为一个Employee类:<br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px">Employee{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateStringid;//雇员的标识号码</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateStringname;//雇员姓名</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateStringdepartment;//该雇员所在部门</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateStringPhone;//该雇员联系电话</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateintsalary;//该雇员薪资</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateStringorigin;//该雇员信息的来源</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">//构造方法</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">Employee(Stringid){<br style="line-height:25px"> this.id=id;<br style="line-height:25px"> getDataFromlnfoCenter();<br style="line-height:25px"> }</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//到数据库中取得雇员信息</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privatevoidgetDataFromlnfoCenter(){</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//和数据库建立连接井查询该雇员的信息,将查询结果赋值<br style="line-height:25px"> //给name,department,plone,salary等变量<br style="line-height:25px"> //同时将origin赋值为"FromDataBase"</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> ……<br style="line-height:25px"> 这个Employee类的构造方法中我们可以预见,如果每次需要查询一个雇员的信息。哪怕是几秒中之前刚刚查询过的,都要重新构建一个实例,这是需要消耗很多时间的。下面是一个对Employee对象进行缓存的缓存器的定义:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.lang.ref.ReferenceQueue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.lang.ref.SoftReference;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">importjava.util.Hashtable;</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">EmployeeCache</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"> staticprivateEmployeeCachecache;</span><span style="color:#808080; line-height:25px">//一个Cache实例</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateHashtable<String,EmployeeRef>employeeRefs;</span><span style="color:#808080; line-height:25px">//用于Chche内容的存储</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateReferenceQueue<Employee>q;//垃圾Reference的队列</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//继承SoftReference,使得每一个实例都具有可识别的标识。</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privateclass</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">EmployeeRef</span><span style="color:#3366ff; line-height:25px">extendsSoftReference<Employee>{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">privateString_key="";</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">publicEmployeeRef(Employeeem,ReferenceQueue<Employee>q){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">super(em,q);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">_key=em.getID();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//构建一个缓存器实例</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">private</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">EmployeeCache</span><span style="color:#3366ff; line-height:25px">(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">employeeRefs=newHashtable<String,EmployeeRef>();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">q=newReferenceQueue<Employee>();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//取得缓存器实例</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicstatic</span><span style="color:#3366ff; line-height:25px">EmployeeCachegetInstance(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">if(cache==null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cache=newEmployeeCache();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">cache;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//以软引用的方式对一个Employee对象的实例进行引用并保存该引用</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatevoid</span><span style="color:#3366ff; line-height:25px">cacheEmployee(Employeeem){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cleanCache();//清除垃圾引用</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">EmployeeRefref=newEmployeeRef(em,q);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">employeeRefs.put(em.getID(),ref);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//依据所指定的ID号,重新获取相应Employee对象的实例</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">EmployeegetEmployee(StringID){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Employeeem=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#808080; line-height:25px">//缓存中是否有该Employee实例的软引用,如果有,从软引用中取得。</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(employeeRefs.containsKey(ID)){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">EmployeeRefref=(EmployeeRef)employeeRefs.get(ID);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">em=(Employee)ref.get();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//如果没有软引用,或者从软引用中得到的实例是null,重新构建一个实例,<br style="line-height:25px"> //并保存对这个新建实例的软引用</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">if</span><span style="color:#3366ff; line-height:25px">(em==null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">em=newEmployee(ID);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("RetrieveFromEmployeeInfoCenter.ID="+ID);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">this.cacheEmployee(em);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">returnem;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">privatevoid</span><span style="color:#3366ff; line-height:25px">cleanCache(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">EmployeeRefref=null;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">while</span><span style="color:#3366ff; line-height:25px">((ref=(EmployeeRef)q.poll())!=null){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#0000ff; line-height:25px">employeeRefs.remove(ref._key);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//清除Cache内的全部内容</span><br style="line-height:25px"><span style="color:#993300; line-height:25px">publicvoid</span><span style="color:#3366ff; line-height:25px">clearCache(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">cleanCache();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">employeeRefs.clear();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.gc();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.runFinalization();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><br style="line-height:25px"> 注:<span style="color:#000080; line-height:25px">原来ReferenceQueue只是起到一个监听器的效果</span>,当发现SoftReference.get()方法返回的是null值时,就会将SoftReference注册到自己里面队列里,当我们调用ReferenceQueue的poll()方法时,返回并删除该SoftReference。</wbr>