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(){ Listbyte[]>> 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 { Listbyte[]>> 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 { Listbyte[]>> 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