对象的强软弱和虚引用计划

回转窑设备2021年09月06日

在图中,带实线的箭头表示强引用,带虚线的箭头表示弱引用。从图中可以看出,此时\"hello\"对象被str强引用,并且被一个WeakReference对象弱引用,因此\"hello\"对象不会被垃圾回收。

在以下程序代码中,把引用\"hello\"对象的str变量置为null,然后再通过WeakReference弱引用的get()方法获得\"hello\"对象的引用:

String str = new String(\"hello\"); //①

ReferenceQueueString rq = new ReferenceQueueString(); //②

WeakReferenceString wf = new WeakReferenceString(str, rq); //③

str=null; //④取消\"hello\"对象的强引用

String str1=t(); //⑤假如\"hello\"对象没有被回收,str1引用\"hello\"对象

//假如\"hello\"对象没有被回收,ll()返回null

Reference? extends String ref=ll(); //⑥

执行完以上第④行后,内存中引用与对象的关系如图所示,此 时\"hello\"对象仅仅具有弱引用,因此它有可能被垃圾回收。假如它还没有被垃圾回收,那么接下来在第⑤行执行t()方法会返回 \"hello\"对象的引用,并且使得这个对象被str1强引用。再接下来在第⑥行执行ll()方法会返回null,因为此时引用队列中没有任何 引用。ReferenceQueue的poll()方法用于返回队列中的引用,如果没有则返回null。

□本报邢东伟 图 \"hello\"对象只具有弱引用

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

String str = new String(\"hello\"); //①

ReferenceQueueString rq = new ReferenceQueueString(); //②

WeakReferenceString wf = new WeakReferenceString(str, rq); //③

str=null; //④

//两次催促垃圾回收器工作,提高\"hello\"对象被回收的可能性

(); //⑤

(); //⑥

String str1=t(); //⑦ 假如\"hello\"对象被回收,str1为null

Reference? extends String ref=ll(); //⑧

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

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

例程 va

import f.*;

import il.*;

class Grocery{

private static final int SIZE = 10000;

//属性d使得每个Grocery对象占用较多内存,有80K左右

private double[] d = new double[SIZE];

private String id;

public Grocery(String id) { = id; }

public String toString() { return id; }

public void finalize() {

intln(\"Finalizing \" + id);

}

}

public class References {

private static ReferenceQueueGrocery rq = new ReferenceQueueGrocery();

public static void checkQueue() {

Reference? extends Grocery inq = ll(); //从队列中取出一个引用

if(inq != null)

intln(\"In queue: \"+inq+\" : \"+t());

}

public static void main(String[] args) {

final int size=10;

//创建10个Grocery对象以及10个软引用

SetSoftReferenceGrocery sa = new HashSetSoftReferenceGrocery();

for(int i = 0; i size; i++) {

SoftReferenceGrocery ref=

new SoftReferenceGrocery(new Grocery(\"Soft \" + i), rq);

intln(\"Just created: \" +t());

d(ref);

}

();

checkQueue();

//创建10个Grocery对象以及10个弱引用

SetWeakReferenceGrocery wa = new HashSetWeakReferenceGrocery();

for(int i = 0; i size; i++) {

WeakReferenceGrocery ref=

new WeakReferenceGrocery(new Grocery(\"Weak \" + i), rq);

intln(\"Just created: \" +t());

d(ref);

}

();

checkQueue();

//创建10个Grocery对象以及10个虚引用

SetPhantomReferenceGrocery pa = new HashSetPhantomReferenceGrocery();

for(int i = 0; i size; i++) {

PhantomReferenceGroceryref =

new PhantomReferenceGrocery(new Grocery(\"Phantom \" + i), rq);

intln(\"Just created: \" +t());

d(ref);

}

();

checkQueue();

}

}

查看本文来源

乌兰察布哪里治白癜风最好
广州医院男科哪好
蒙脱石散有什么作用
相关阅读
健康领域人均薪酬次于TOP30行业出炉,你所在公司上榜了吗?

有时只能来点聘任母公司一夜暴富的运气文/ 陈天宇编/蔡 图/pixabay据闻管理者...

2023-09-25
【东方焦点】北京中医药大学东方医院与海外中医中心共同承办国际代表大会“中医儿科”线上分论坛取得圆满成

北京里药学药大学圣城疗养院与海外里药学区域内联合负责负责管理国际则会...

2023-09-18
vivo Y72t上架,天玑700+6000毫安大蓄电池

知晓vivo的朋友都究竟看起来X第四部、S第四部这些主力产品线有同型发布,都...

2023-09-15
九龙建业拟8.17亿港元购并能杰发展

  乐居财经昨 赵微5月末20日,九龙建业(00034)公告,收购能杰工业发展有限...

2023-09-12
期货黄金价格反弹向1780阻击 其价格要上涨?

7月底6日,据股市势头图揭示,今天,证券黄金和的价钱“平开很高走”,小...

2023-09-10
女人们过了50岁,夏天穿裙子记住“三不要”原则,显老不说还没气质

对于女人们来说,50岁是一个如此一来,无论是身体静止状态还是心态各个方...

2023-09-06
友情链接