岁月联盟 · 中国技术网 本站主页 | 安全认证 | 用户服务 | 技术论坛
新闻快报 | 新手学堂 | 黑客特区 | 程序语言 | 数 据 库 | 防 火 墙 | 路由交换 | 系统集成 | 服 务 器 | 存储备份 | 考试认证
Windows | Linux | Java | 协议分析 | 问题解答 | 进程大全 | 网页设计 | 多 媒 体 | 图库资料 | 软件下载 | 站内下载
  您现在的位置: 岁月联盟 >> Java >> 新手入门 >> oop面向对象 >> Java正文
Java对象的强、软、弱和虚引用
作者:未知 文章来源:本站整理 点击数: 更新时间:2007-7-22 16:30:32
来在第⑥行执行rq.poll()方法会返回null,因为此时引用队列中没有任何 引用。ReferenceQueue的poll()方法用于返回队列中的引用,如果没有则返回null。


图11-11 "hello"对象只具有弱引用

    在以下程序代码中,执行完第④行后,"hello"对象仅仅具有弱引用。接下来两次调用System.gc()方法,催促垃圾回收器工作,从而提高 "hello"对象被回收的可能性。假如"hello"对象被回收,那么WeakReference对象的引用被加入到ReferenceQueue中, 接下来wf.get()方法返回null,并且rq.poll()方法返回WeakReference对象的引用。图11-12显示了执行完第⑧行后内存 中引用与对象的关系。

String str = new String("hello"); //①
ReferenceQueue<String> rq = new ReferenceQueue<String>(); //②
WeakReference<String> wf = new WeakReference<String>(str, rq); //③
str=null; //④

//两次催促垃圾回收器工作,提高"hello"对象被回收的可能性
System.gc(); //⑤
System.gc(); //⑥
String str1=wf.get(); //⑦ 假如"hello"对象被回收,str1为null
Reference<? extends String> ref=rq.poll(); //⑧


图11-12 "hello"对象被垃圾回收,弱引用被加入到引用队列


    在以下例程11-15的References类中,依次创建了10个软引用、10个弱引用和10个虚引用,它们各自引用一个Grocery对象。从程序运 行时的打印结果可以看出,虚引用形同虚设,它所引用的对象随时可能被垃圾回收,具有弱引用的对象拥有稍微长的生命周期,当垃圾回收器执行回收操作时,有可 能被垃圾回收,具有软引用的对象拥有较长的生命周期,但在Java虚拟机认为内存不足的情况下,也会被垃圾回收。

例程11-15 References.java
import java.lang.ref.*;
import java.util.*;

class Grocery{
private static final int SIZE = 10000;
//属性d使得每个Grocery对象占用较多内存,有80K左右
private double[] d = new double[SIZE];
private String id;
public Grocery(String id) { this.id = id; }
public String toString() { return id; }
public void finalize() {
System.out.println("Finalizing " + id);
}
}

public class References {
private static ReferenceQueue<Grocery> rq = new ReferenceQueue<Grocery>();
public static void checkQueue() {
Reference<? extends Grocery> inq = rq.poll(); //从队列中取出一个引用
if(inq != null)
System.out.println("In queue: "+inq+" : "+inq.get());
}

public static void main(String[] args) {
final int size=10;

//创建10个Grocery对象以及10个软引用
Set<SoftReference<Grocery>> sa = new HashSet<SoftReference<Grocery>>();
for(int i = 0; i < size; i++) {
SoftReference<Grocery> ref=
new SoftReference<Grocery>(new Grocery("Soft " + i), rq);
System.out.println("Just created: " +ref.get());
sa.add(ref);
}
System.gc();
checkQueue();

//创建10个Grocery对象以及10个弱引用
Set<WeakReference<Grocery>> wa = new HashSet<WeakReference<Grocery>>();
for(int i = 0; i < size; i++) {
WeakReference<Grocery> ref=
new WeakReference<Grocery>(new Grocery("Weak " + i), rq);
System.out.println("Just created: " +ref.get());
wa.add(ref);
}
System.gc();
checkQueue();

//创建10个Grocery对象以及10个虚引用
Set<PhantomReference<Grocery>> pa = new HashSet<PhantomReference<Grocery>>();
for(int i = 0; i < size; i++) {
PhantomReference<Grocery>ref =
new PhantomReference<Grocery>(new Grocery("Phantom " + i), rq);
System.out.println("Just created: " +ref.get());
pa.add(ref);
}
System.gc();
checkQueue();
}
}


 

上一页  [1] [2] [3] 下一页  

   在Java集合中有一种特殊的Map类型:WeakHashMap, 在这种Map中存放了键对象的弱引用,当一个键对象被垃圾回收,那么相应的值对象的引用会从Map中删除。We

上一页  [1] [2] [3] 下一页


  • 上一个Java:
  • 下一个Java:
  •  
    热门文章
    推荐文章
    关于我们 | 发展历程 | 网站地图 | 广告服务 | 招贤纳士 | 战略合作 | 友情链接 | 著作声明 | 联系我们
    Copyright © 2002-2007 SYUE All rights reserved.
    E_mail:Admin@Syue.Com 皖ICP备05004589号
    未经授权禁止转载、摘编、复制或建立镜像.如有违反,追究法律责任.
    bet365 传世私服 天龙八部私服 热血江湖私服 英雄合击传奇私服 机战私服 惊天动地私服 天龙八部私服 热血江湖私服 bet365 天龙八部私服 bet365 传世私服 传奇世界私服