jvm:java中的引用(强引用、软引用、虚引用、弱引用)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了jvm:java中的引用(强引用、软引用、虚引用、弱引用),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含9210字,纯文字阅读大概需要14分钟。
内容图文
![jvm:java中的引用(强引用、软引用、虚引用、弱引用)](/upload/InfoBanner/zyjiaocheng/632/c3bc67ec2e2b44ccac0c18e2c977c924.jpg)
1、分类
强引用、软引用、弱引用、虚引用、终结器引用
强引用:只要能够通过GC Root的引用链找到就不会被垃圾回收,也就是说只有所有的GC Roots对象都不通过强引用引用该对象的时候,该对象才能被垃圾回收
软引用:没有被其他强引用所引用的时候,当内存空间不够时,软引用的对象可能会被垃圾回收,可以配合引用队列来释放软引用自身
弱引用:没有被其他强引用所引用的时候,弱引用的对象就会被垃圾回收,不管内存是否充足,可以配合引用队列来释放弱引用自身
虚引用:创建的时候会关联一个引用队列,例如:创建ByteBuffer的时候会创建一个名为Cleaner的虚引用对象,当ByteBuffer没有被强引用所引用就会被jvm垃圾回收,虚引用Cleaner就会进入引用队列,会有专门的线程扫描引用队列,被发现后会调用直接内存地址的方法将直接内存释放掉,保证直接内存不会导致内存泄漏
终结器引用:创建的时候会关联一个引用队列,当A4对象没有被强引用所引用时,A4被垃圾回收的时候,会将终结器引用放入到一个引用队列(被引用对象暂时还没有被垃圾回收),有专门的线程(优先级较低,可能会造成对象迟迟不被回收)扫描引用队列并调用finallize()方法,第二次GC的时候才能回收掉被引用对象
2、强引用
(1)强引用的使用:
设置jvm的内存为20M
public class Test2 { public static final int _4MB=4*1024*1024; public static void main(String[] args) throws IOException { List<byte[]> list =new ArrayList<>(); for(int i=0;i<5;i++){ list.add(new byte[_4MB]); } System.in.read(); } }
运行上面的程序会出现内存溢出问题,也就是说有的内存即使不太重要依旧会被占用:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at pers.zhb.test.Test2.main(Test2.java:12)
3、软引用
(1)软引用的场景:
输入jvm指令:
public class Test2 { private static final int _4MB=4*1024*1024; public static void main(String[] args) throws IOException { soft(); } public static void soft(){ List<SoftReference<byte[]>> list=new ArrayList<>(); for(int i=0;i<5;i++){ SoftReference<byte[]> reference=new SoftReference<>(new byte[_4MB]); System.out.println(reference.get()); list.add(reference); System.out.println(list.size()); } System.out.println("循环结束:"+list.size()); for(SoftReference<byte[]> reference:list){ System.out.println(reference.get()); } } }
运行结果:
[B@1b6d3586 1 [B@4554617c 2 [B@74a14482 3 [GC (Allocation Failure) [PSYoungGen: 1744K->488K(6144K)] 14032K->12932K(19968K), 0.0427814 secs] [Times: user=0.00 sys=0.00, real=0.05 secs] [B@1540e19d 4 [GC (Allocation Failure) --[PSYoungGen: 4696K->4696K(6144K)] 17140K->17140K(19968K), 0.0030222 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (Ergonomics) [PSYoungGen: 4696K->4574K(6144K)] [ParOldGen: 12444K->12414K(13824K)] 17140K->16988K(19968K), [Metaspace: 3118K->3118K(1056768K)], 0.0095409 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] [GC (Allocation Failure) --[PSYoungGen: 4574K->4574K(6144K)] 16988K->16996K(19968K), 0.0010492 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (Allocation Failure) [PSYoungGen: 4574K->0K(6144K)] [ParOldGen: 12422K->587K(8704K)] 16996K->587K(14848K), [Metaspace: 3118K->3118K(1056768K)], 0.0076471 secs] [Times: user=0.03 sys=0.00, real=0.01 secs] [B@677327b6 5 循环结束:5 null null null null [B@677327b6 Heap PSYoungGen total 6144K, used 4546K [0x00000000ff980000, 0x0000000100000000, 0x0000000100000000) eden space 5632K, 80% used [0x00000000ff980000,0x00000000ffdf0940,0x00000000fff00000) from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000) to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000) ParOldGen total 8704K, used 587K [0x00000000fec00000, 0x00000000ff480000, 0x00000000ff980000) object space 8704K, 6% used [0x00000000fec00000,0x00000000fec92d20,0x00000000ff480000) Metaspace used 3224K, capacity 4500K, committed 4864K, reserved 1056768K class space used 349K, capacity 388K, committed 512K, reserved 1048576K
可以看出,在第四次和第五次的时候就触发了垃圾回收,在对集合进行遍历后只有第五个软引用对象存在,也就是说在内存不足的时候将前面的虚引用的对象进行了垃圾回收
(2)软引用与引用队列:
public class Test2 { private static final int _4MB=4*1024*1024; public static void main(String[] args) throws IOException { List<SoftReference<byte[]>> list=new ArrayList<>(); //引用队列 ReferenceQueue<byte[]> queue=new ReferenceQueue<>(); for(int i=0;i<5;i++){ //关联了引用队列,当软引用所关联的byte数组被回收时,软引用自己会加入到引用队列中 SoftReference<byte[]> reference=new SoftReference<>(new byte[_4MB],queue); System.out.println(reference.get()); list.add(reference); System.out.println(list.size()); } //从队列获取无用的软引用对象并移除 Reference<? extends byte[]> poll=queue.poll(); while(poll!=null){ list.remove(poll); poll=queue.poll(); } System.out.println("循环结束"); for(SoftReference<byte[]> reference:list){ System.out.println(reference.get()); } } }
测试:
[B@1b6d3586 1 [B@4554617c 2 [B@74a14482 3 [GC (Allocation Failure) [PSYoungGen: 1866K->488K(6144K)] 14154K->12964K(19968K), 0.0010895 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [B@1540e19d 4 [GC (Allocation Failure) --[PSYoungGen: 4696K->4696K(6144K)] 17172K->17180K(19968K), 0.0011250 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (Ergonomics) [PSYoungGen: 4696K->4564K(6144K)] [ParOldGen: 12484K->12458K(13824K)] 17180K->17022K(19968K), [Metaspace: 3228K->3228K(1056768K)], 0.0055610 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] [GC (Allocation Failure) --[PSYoungGen: 4564K->4564K(6144K)] 17022K->17030K(19968K), 0.0009186 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (Allocation Failure) [PSYoungGen: 4564K->0K(6144K)] [ParOldGen: 12466K->620K(8704K)] 17030K->620K(14848K), [Metaspace: 3228K->3228K(1056768K)], 0.0065061 secs] [Times: user=0.00 sys=0.01, real=0.01 secs] [B@677327b6 5 循环结束 [B@677327b6 Heap PSYoungGen total 6144K, used 4546K [0x00000000ff980000, 0x0000000100000000, 0x0000000100000000) eden space 5632K, 80% used [0x00000000ff980000,0x00000000ffdf0918,0x00000000fff00000) from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000) to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000) ParOldGen total 8704K, used 620K [0x00000000fec00000, 0x00000000ff480000, 0x00000000ff980000) object space 8704K, 7% used [0x00000000fec00000,0x00000000fec9b160,0x00000000ff480000) Metaspace used 3237K, capacity 4500K, committed 4864K, reserved 1056768K class space used 351K, capacity 388K, committed 512K, reserved 1048576K Process finished with exit code 0
4、弱引用
(1)弱引用举例
public class Test2 { private static final int _4MB = 4 * 1024 * 1024; public static void main(String[] args) throws IOException { List<WeakReference<byte[]>> list = new ArrayList<>(); for (int i = 0; i < 10; i++) { WeakReference<byte[]> reference = new WeakReference<>(new byte[_4MB]); list.add(reference); for (WeakReference<byte[]> w : list) { System.out.println(w.get()); } System.out.println(); } System.out.println("循环结束:" + list.size()); } }
(2)测试:
[B@1b6d3586 [B@1b6d3586 [B@4554617c [B@1b6d3586 [B@4554617c [B@74a14482 [GC (Allocation Failure) [PSYoungGen: 1865K->488K(6144K)] 14153K->12992K(19968K), 0.0014680 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [B@1b6d3586 [B@4554617c [B@74a14482 [B@1540e19d [GC (Allocation Failure) [PSYoungGen: 4809K->496K(6144K)] 17313K->13044K(19968K), 0.0013554 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [B@1b6d3586 [B@4554617c [B@74a14482 null [B@677327b6 [GC (Allocation Failure) [PSYoungGen: 4702K->472K(6144K)] 17250K->13020K(19968K), 0.0017348 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [B@1b6d3586 [B@4554617c [B@74a14482 null null [B@14ae5a5 [GC (Allocation Failure) [PSYoungGen: 4678K->488K(6144K)] 17226K->13036K(19968K), 0.0016909 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [B@1b6d3586 [B@4554617c [B@74a14482 null null null [B@7f31245a [GC (Allocation Failure) [PSYoungGen: 4694K->488K(6144K)] 17242K->13044K(19968K), 0.0008993 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [B@1b6d3586 [B@4554617c [B@74a14482 null null null null [B@6d6f6e28 [GC (Allocation Failure) [PSYoungGen: 4694K->472K(5120K)] 17250K->13028K(18944K), 0.0011246 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [B@1b6d3586 [B@4554617c [B@74a14482 null null null null null [B@135fbaa4 [GC (Allocation Failure) [PSYoungGen: 4657K->32K(5632K)] 17214K->13012K(19456K), 0.0012595 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] [Full GC (Ergonomics) [PSYoungGen: 32K->0K(5632K)] [ParOldGen: 12980K->638K(7680K)] 13012K->638K(13312K), [Metaspace: 3232K->3232K(1056768K)], 0.0084781 secs] [Times: user=0.00 sys=0.00, real=0.01 secs] null null null null null null null null null [B@45ee12a7 循环结束:10 Heap PSYoungGen total 5632K, used 4371K [0x00000000ff980000, 0x0000000100000000, 0x0000000100000000) eden space 4608K, 94% used [0x00000000ff980000,0x00000000ffdc4e70,0x00000000ffe00000) from space 1024K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x00000000fff00000) to space 1024K, 0% used [0x00000000fff00000,0x00000000fff00000,0x0000000100000000) ParOldGen total 7680K, used 638K [0x00000000fec00000, 0x00000000ff380000, 0x00000000ff980000) object space 7680K, 8% used [0x00000000fec00000,0x00000000fec9fa38,0x00000000ff380000) Metaspace used 3239K, capacity 4500K, committed 4864K, reserved 1056768K class space used 351K, capacity 388K, committed 512K, reserved 1048576K Process finished with exit code 0
内容总结
以上是互联网集市为您收集整理的jvm:java中的引用(强引用、软引用、虚引用、弱引用)全部内容,希望文章能够帮你解决jvm:java中的引用(强引用、软引用、虚引用、弱引用)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。