当前位置: 首页 > news >正文

Java之对象引用实践

功能概述

  • 从JDK1.2版本开始,程序可以通过4种类型的对象的引用来管控对象的生命周期。这4种引用分别为,强引用、软引用、弱引用和虚引用。本文中针对各种引用做了相关测试,并做对应分析。

功能实践

场景1:弱引用、虚引用、软引用基本使用

用例代码

@Test
public void test_reference_v1() {ReferenceQueue<Ref> queue = new ReferenceQueue<>();// 创建一个弱引用(指定引用的对象,以及引用对象要注册的队列)WeakReference<Ref> weak = new WeakReference<>(new Ref("Weak"), queue);// 创建一个虚引用PhantomReference<Ref> phantom = new PhantomReference<>(new Ref("Phantom"), queue);// 创建一个软引用SoftReference<Ref> 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)

场景2:GC垃圾回收时,对象引用的行为

用例代码

@Test
public void test_reference_v2() {
ReferenceQueue<Ref> queue = new ReferenceQueue<>();WeakReference<Ref> weak = new WeakReference<>(new Ref("WeakV2"), queue); //注册:此处的Ref对象在外部没有任何引用,所以在某个时间点,GC应当回收这个对象
PhantomReference<Ref> phantom = new PhantomReference<>(new Ref("PhantomV2"), queue);
SoftReference<Ref> 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.WeakReference@1b701da1
java.lang.ref.PhantomReference@726f3b58

结果分析

  • 弱引用和虚引用都会回收了,软引用要在接近OOM异常时回收

场景3:GC垃圾回收时,关联强引用

用例代码

@Test
public void test_reference_v3() {ReferenceQueue<Ref> queue = new ReferenceQueue<>();Ref wr = new Ref("Hard"); //强引用WeakReference<Ref> weak = new WeakReference<>(wr, queue); //引用的对象wr是强引用PhantomReference<Ref> phantom = new PhantomReference<>(wr, queue);SoftReference<Ref> 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,如wr=null,表明强引用可被回收,此时关联的弱引用、虚引用都可被回收

功能总结

  • 多个引用说明:
    • a)HardReference:强引用(注:没有这个类,只是形象说明),类似String str = new String()建立起来的引用,都是强引用。在str指向另一个对象或null之前都不会被GC回收(指向另一个对象,或str=null才会被GC回收)
    • b)WeakReference:弱引用,当GC要求回收对象时,不会阻止对象被回收,即使有弱引用存在
    • c)SoftReference:软引用,当GC要求回收对象时,也不会阻止对象被回收,但回收过程会有延迟,必须要等到JVM heap内存不够用,接近产生OutOfMemory错误时,才会被回收
    • d)PhantomReference:虚引用,这种类型的引用比较特别,在大多数时间里,无法通过它拿到其引用的对象(即phantom.get()总是为null),但是,在这个对象消失的时候,该引用还是会进入ReferenceQueue队列中的
http://www.lryc.cn/news/144770.html

相关文章:

  • IntelliJ IDEA快捷键大全 + 动图演示!
  • React 生命周期
  • 5G智能网关如何解决城市停车痛点难点
  • docker 学习-- 04 实践搭建 1(宝塔)
  • MySQL的mysql-bin.00xx binlog日志文件的清理
  • Java实现SM2前后端加解密
  • 自动化PLC工程师能否转到c#上位机开发?
  • LiveData相关基本使用及去除黏性数据的方法
  • 【MegaCli】安装MegaCli后执行报参数错误
  • 时间范围选择时选中日期所使用的当日内具体时刻 如00:00:00= 23:59:59
  • 算法面试-深度学习面试题整理(2024.8.29开始,每天下午持续更新....)
  • Maven之高版本的 lombok 和 tomcat 7 插件冲突问题
  • 微信小程序申请
  • ffmpeg 配合Fiddler抓包操作
  • 美团面试拷打:ConcurrentHashMap 为何不能插入 null?HashMap 为何可以?
  • 【力扣每日一题】2023.8.29 带因子的二叉树
  • origin导出pdf曲线超出边框
  • 由Android10适配到Android12时遇到相关编译失败问题
  • 高职教育应对ChatGPT应用的策略
  • Linux 内核编译参数
  • vscode使用anaconda自带的python环境在终端运行时报错
  • 葡萄叶病害识别(图像连续识别和视频识别,Python代码,pyTorch框架)
  • Oracle drop删除表如何恢复
  • 5、监测数据采集物联网应用开发步骤(5.1)
  • ZZULIOJ 1148: 组合三位数之一,Java
  • ROS功能包目录下CMakeLists.txt
  • Python爬虫追踪新闻事件发展进程及舆论反映
  • block层:7. 请求下发
  • Matlab图像处理-平移运算
  • 美创科技一体化智能化公共数据平台数据安全建设实践