【Java 的四大引用详解】
创始人
2025-01-10 04:03:09
0

首先分别介绍一下这几种引用

image.png

强引用: 只要能通过GC ROOT根对象引用链找到就不会被垃圾回收器回收,当所有的GC Root都不通过强引用引用该对象时,才能被垃圾回收器回收。

软引用(SoftReference): 当只有软引用引用该对象时,在垃圾回收之后内存仍然不足会再次发起垃圾回收,这时会回收掉软引用对象,我们可以配合引用队列来释放软引用自身。

弱引用: 当发生垃圾回收时,无论内存是否够用,只有软引用的对象都会被垃圾回收器回收

虚引用: 必须配合引用队列使用,主要配合 ByteBuffer 使用,被引用对象回收时,会将虚引用入引用队列, 由 Reference Handler 线程调用虚引用相关方法释放直接内存。

终结器引用: 无需手动编码,但其内部配合引用队列使用,在垃圾回收时,终结器引用入队(被引用对象 暂时没有被回收),再由 Finalizer 线程通过终结器引用找到被引用对象并调用它的 finalize 方法,第二次 GC 时才能回收被引用对象

实例

强引用 首先我们设置内存大小为20MB

image.png

    public class Demo2_3 {         private static final int _4MB = 4 * 1024 * 1024;         public static void main(String[] args) throws IOException {              ArrayList list = new ArrayList<>();              for (int i = 0; i < 5; i++) {                 list.add(new byte[_4MB]);             }             System.in.read();         }          } 

启动main,因为强引用无法被垃圾回收会发生内存溢出,报错内存不足无法启动 image.png

弱引用 应用场景

public class Demo2_4 {      private static final int _4MB = 4 * 1024 * 1024;      //软引用,当堆内存空间不足时,会回收来释放内存空间     public static void main(String[] args) throws IOException {          //list ---> SoftReference ---> byte[] list先引用了软引用对象SoftReference,软引用对象SoftReference再间接引用byte         List> list = new ArrayList<>();          for (int i = 0; i < 5; i++) {             //引用对象关联引用队列,当软引用所关联的byte[]被回收时,软引用自己会加入到引用队列queue中去             SoftReference ref = new SoftReference<>(new byte[_4MB]);             System.out.println(ref.get());             list.add(ref);             System.out.println(list.size());          }         System.out.println("循环结束:" + list.stream());         for (SoftReference softReference : list) {             System.out.println(softReference.get());         }     } }  

运行:

image.png

查看打印结果,程序在第四次循环的时候内存不足触发了垃圾回收,此时将前面的软引用的对象给回收了,所以我们最后打印结果只有第五个对象不为null

同时我们还可以配合引用队列来释放软引用自身

public class Demo2_3 {      private static final int _4MB = 4 * 1024 * 1024;      public static void main(String[] args) throws IOException { //list ---> SoftReference ---> byte[] list先引用了软引用对象SoftReference,软引用对象SoftReference再间接引用byte         List> list = new ArrayList<>();         //引用队列         ReferenceQueue queue = new ReferenceQueue<>();         for (int i = 0; i < 5; i++) {             //引用对象关联引用队列,当软引用所关联的byte[]被回收时,软引用自己会加入到引用队列queue中去             SoftReference ref = new SoftReference<>(new byte[_4MB],queue);             System.out.println(ref.get());             list.add(ref);             System.out.println(list.size());          }         //poll方法就是从队列中获取最先放入队列的元素移除队列         //从队列中获取无用的软引用对象并移除         Reference poll = queue.poll();         while (poll != null){             list.remove(poll);             poll = queue.poll();          }         System.out.println("循环结束:" + list.stream());         for (SoftReference softReference : list) {             System.out.println(softReference.get());         }     } } 

运行程序:

image.png

前四次循环的软引用自身已经被释放

弱引用: 应用场景举例

public class Demo2_5 {      private static final int _4MB = 4 * 1024 * 1024;      public static void main(String[] args) {         ArrayList> list = new ArrayList<>();         for (int i = 0; i < 8; i++) {             WeakReference ref = new WeakReference<>(new byte[_4MB]);             list.add(ref);             for (WeakReference w : list) {                 System.out.print(w.get()+" ");             }             System.out.println();         }         System.out.println("循环结束:"+list.size());     } } 

运行程序:

image.png

相关内容

热门资讯

透视辅助!wejoker辅助器... 透视辅助!wejoker辅助器怎么卖,佛手在线大菠萝辅助,关于开挂辅助工具(透视竟然有挂) >>您好...
热点推荐“xpoker怎么辅助... 开挂教程视频分享装挂详细步骤在当今的网络游戏中,作为一种经典的娱乐方式,吸引了无数玩家的参与。尤其是...
透视辅助!wepoker透视脚... 透视辅助!wepoker透视脚本是什么,aapoker插件,科技开挂辅助工具(透视有挂解密)【无需打...
推荐一款“hhpkoer辅助挂... 您好:这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别好,总是好牌...
透视辅助!hh poker辅助... hh poker辅助器先试用是一款专注玩家量身打造的游戏记牌类型软件,在hh poker辅助器先试用...
我来向大家传授“wepoker... 我来向大家传授“wepoker永久免费脚本”开挂(透视)辅助平台(科技教程有挂技术)您好:这款游戏可...
透视辅助!wepoker怎么设... 透视辅助!wepoker怎么设置盖牌,拱趴大菠萝十三水辅助,分享开挂辅助平台(透视有挂教程);无需打...
重磅来袭“wepoker可以免... 重磅来袭“wepoker可以免费玩吗”开挂(透视)辅助安装(新2026教程有挂猫腻)1、下载安装好,...
透视辅助!aa poker透视... 透视辅助!aa poker透视软件,wepoker插件功能辅助器,教会开挂辅助神器(透视有挂总结);...
实测教程“wepoker免费脚... 实测教程“wepoker免费脚本”开挂(透视)辅助安装(切实教程有挂秘笈) 【无需打开直接搜索加薇1...