宁波做网站,手机优化如何弄到100,百度站长平台网站提交,怎么做网站开发的方案功能概述
从JDK1.2版本开始#xff0c;程序可以通过4种类型的对象的引用来管控对象的生命周期。这4种引用分别为#xff0c;强引用、软引用、弱引用和虚引用。本文中针对各种引用做了相关测试#xff0c;并做对应分析。
功能实践
场景1#xff1a;弱引用、虚引用、软引用…功能概述
从JDK1.2版本开始程序可以通过4种类型的对象的引用来管控对象的生命周期。这4种引用分别为强引用、软引用、弱引用和虚引用。本文中针对各种引用做了相关测试并做对应分析。
功能实践
场景1弱引用、虚引用、软引用基本使用
用例代码
Test
public void test_reference_v1() {ReferenceQueueRef queue new ReferenceQueue();// 创建一个弱引用指定引用的对象以及引用对象要注册的队列WeakReferenceRef weak new WeakReference(new Ref(Weak), queue);// 创建一个虚引用PhantomReferenceRef phantom new PhantomReference(new Ref(Phantom), queue);// 创建一个软引用SoftReferenceRef soft new SoftReference(new Ref(Soft), queue);System.out.println(引用内容);System.out.println(weak.get());System.out.println(phantom.get()); //看源码phantom.get()始终返回nullSystem.out.println(soft.get());System.out.println(被回收的引用);for (Reference r null; (r queue.poll()) ! null;) {System.out.println(r);}
}class Ref {Object v;Ref(Object v) {this.v v;}public String toString() {return this.v.toString();}
}运行结果
引用内容
Weak
null
Soft
被回收的引用结果分析
弱引用对象和软引用对象都是可达的但是虚引用对象不可点phantom.get()调用时总是为null创建弱引用、软引用、虚引用时需要执行引用的的对象、引用对象注册的队列如new WeakReference(new Ref(“Weak”), queue)
场景2GC垃圾回收时对象引用的行为
用例代码
Test
public void test_reference_v2() {
ReferenceQueueRef queue new ReferenceQueue();WeakReferenceRef weak new WeakReference(new Ref(WeakV2), queue); //注册此处的Ref对象在外部没有任何引用所以在某个时间点GC应当回收这个对象
PhantomReferenceRef phantom new PhantomReference(new Ref(PhantomV2), queue);
SoftReferenceRef soft new SoftReference(new Ref(SoftV2), queue);System.out.println(引用内容V2);
System.out.println(weak.get());
System.out.println(phantom.get()); //看源码phantom.get()始终返回null
System.out.println(soft.get());System.gc();
try {Thread.sleep(100); //给GC留点时间保证GC执行完成} catch (InterruptedException e) {throw new RuntimeException(e);
}System.out.println(被回收的引用V2);
for (Reference r null; (r queue.poll()) ! null; ) {System.out.println(r);
}运行结果
引用内容V2
WeakV2
null
SoftV2
被回收的引用V2
java.lang.ref.WeakReference1b701da1
java.lang.ref.PhantomReference726f3b58结果分析
弱引用和虚引用都会回收了软引用要在接近OOM异常时回收
场景3GC垃圾回收时关联强引用
用例代码
Test
public void test_reference_v3() {ReferenceQueueRef queue new ReferenceQueue();Ref wr new Ref(Hard); //强引用WeakReferenceRef weak new WeakReference(wr, queue); //引用的对象wr是强引用PhantomReferenceRef phantom new PhantomReference(wr, queue);SoftReferenceRef soft new SoftReference(new Ref(Soft), queue);System.out.println(引用内容V3);System.out.println(weak.get());System.out.println(phantom.get());System.out.println(soft.get());System.gc();try {Thread.sleep(100); //给GC留点时间保证GC执行完成} catch (InterruptedException e) {throw new RuntimeException(e);}System.out.println(被回收的引用V3);for (Reference r null; (r queue.poll()) ! null; ) {System.out.println(r);}
}运行结果
引用内容V3
Hard
null
Soft
被回收的引用V3结果分析
弱引用、虚引用在创建时若关联了强引用在强引用可达时不会被回收在强引用置为null如wrnull表明强引用可被回收此时关联的弱引用、虚引用都可被回收
功能总结
多个引用说明 aHardReference强引用注没有这个类只是形象说明类似String str new String()建立起来的引用都是强引用。在str指向另一个对象或null之前都不会被GC回收指向另一个对象或strnull才会被GC回收bWeakReference弱引用当GC要求回收对象时不会阻止对象被回收即使有弱引用存在cSoftReference软引用当GC要求回收对象时也不会阻止对象被回收但回收过程会有延迟必须要等到JVM heap内存不够用接近产生OutOfMemory错误时才会被回收dPhantomReference虚引用这种类型的引用比较特别在大多数时间里无法通过它拿到其引用的对象即phantom.get()总是为null但是在这个对象消失的时候该引用还是会进入ReferenceQueue队列中的