在Java虚拟机(JVM)中,有几种不同类型的引用,它们分别是:强引用(Strong Reference)、软引用(Soft Reference)、弱引用(Weak Reference)、虚引用(Phantom Reference)和引用队列(Reference Queue)。这些引用类型提供了对对象的不同级别的引用,用于垃圾回收和内存管理。
1. 强引用(Strong Reference)
强引用是最常见的引用类型,它在程序中通过普通的对象引用方式进行声明。当一个对象具有强引用时,垃圾回收器不会回收该对象,即使内存不足也不会被回收。
示例代码:
java
Object obj = new Object(); // 创建一个强引用对象
我们平常一般使用的引用就是强引用。
2. 软引用(Soft Reference)
软引用是一种比强引用弱一些的引用。当内存不足时,垃圾回收器可能会回收具有软引用的对象,以释放内存。这使得软引用非常适合用于实现内存敏感的高速缓存。
代码示例:
java
public class a {
public static void main(String[] args) throws InterruptedException {
List<SoftReference<byte[]>> list=new ArrayList<>();
for(int i=0;i<5;i++){
SoftReference<byte[]> softReference=new SoftReference<>(new byte[4*1024*1024]);
list.add(softReference);
}
for (SoftReference<byte[]> softReference : list) {
System.out.println(softReference.get());
}
}
}
然后在vm options设置堆内存最大内存和打印垃圾回收信息参数如下:
-Xmx20m -XX:+PrintGCDetails -verbose:gc
打印日志如下:
GC (Allocation Failure) \[PSYoungGen: 2287K-\>488K(6144K)\] 14575K-\>13060K(19968K), 0.0132177 secs\] \[Times: user=0.00 sys=0.00, real=0.01 secs
GC (Allocation Failure) --\[PSYoungGen: 4584K-\>4584K(6144K)\] 17156K-\>17212K(19968K), 0.0010759 secs\] \[Times: user=0.02 sys=0.00, real=0.00 secs
Full GC (Ergonomics) \[PSYoungGen: 4584K-\>4512K(6144K)\] \[ParOldGen: 12628K-\>12559K(13824K)\] 17212K-\>17071K(19968K), \[Metaspace: 3053K-\>3053K(1056768K)\], 0.0043431 secs\] \[Times: user=0.00 sys=0.00, real=0.01 secs
GC (Allocation Failure) --\[PSYoungGen: 4512K-\>4512K(6144K)\] 17071K-\>17145K(19968K), 0.0008326 secs\] \[Times: user=0.00 sys=0.00, real=0.00 secs
Full GC (Allocation Failure) \[PSYoungGen: 4512K-\>0K(6144K)\] \[ParOldGen: 12633K-\>672K(8704K)\] 17145K-\>672K(14848K), \[Metaspace: 3053K-\>3053K(1056768K)\], 0.0036381 secs\] \[Times: user=0.00 sys=0.00, real=0.00 secs
null
null
null
null
B@ed17bee
Heap
PSYoungGen total 6144K, used 4378K \[0x00000000ff980000, 0x0000000100000000, 0x0000000100000000)
eden space 5632K, 77% used \[0x00000000ff980000,0x00000000ffdc6828,0x00000000fff00000)
from space 512K, 0% used \[0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
to space 512K, 0% used \[0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
ParOldGen total 8704K, used 672K \[0x00000000fec00000, 0x00000000ff480000, 0x00000000ff980000)
object space 8704K, 7% used \[0x00000000fec00000,0x00000000feca8218,0x00000000ff480000)
Metaspace used 3062K, capacity 4486K, committed 4864K, reserved 1056768K
class space used 324K, capacity 386K, committed 512K, reserved 1048576K
Disconnected from the target VM, address: '127.0.0.1:63645', transport: 'socket'
当堆内存不足时,进行垃圾回收的时候,软引用对象就可以被垃圾回收。
### 3. 引用队列(Reference Queue)
引用队列是一个专门用于管理被垃圾回收器回收的引用对象的队列。当一个对象被回收时,与之关联的引用会被放入引用队列中,以便程序可以检测到对象的回收并执行相应的操作。
```java
public class a {
public static void main(String[] args) throws InterruptedException {
List
B@5caf905d \[B@27716f4 \[B@8efb846 \[B@2a84aee7 \[GC (Allocation Failure) \[PSYoungGen: 4704K-\>480K(6144K)\] 17428K-\>13227K(19968K), 0.0006313 secs\] \[Times: user=0.00 sys=0.00, real=0.00 secs
B@5caf905d \[B@27716f4 \[B@8efb846 null \[B@a09ee92 \[B@5caf905d \[B@27716f4 \[B@8efb846 null \[B@a09ee92 Heap PSYoungGen total 6144K, used 4745K \[0x00000000ff980000, 0x0000000100000000, 0x0000000100000000) eden space 5632K, 75% used \[0x00000000ff980000,0x00000000ffdaa478,0x00000000fff00000) from space 512K, 93% used \[0x00000000fff80000,0x00000000ffff8020,0x0000000100000000) to space 512K, 0% used \[0x00000000fff00000,0x00000000fff00000,0x00000000fff80000) ParOldGen total 13824K, used 12747K \[0x00000000fec00000, 0x00000000ff980000, 0x00000000ff980000) object space 13824K, 92% used \[0x00000000fec00000,0x00000000ff872fa8,0x00000000ff980000) Metaspace used 3352K, capacity 4500K, committed 4864K, reserved 1056768K class space used 366K, capacity 388K, committed 512K, reserved 1048576K Process finished with exit code 0 在运行过程中每次打印信息不一样,如果中间发生一次Full GC,那么前面的list集合中的软引用对象全为null。 ### 5. 虚引用(Phantom Reference) 虚引用是最弱的引用类型,它不会阻止对象被垃圾回收。虚引用的主要作用是允许您在对象被回收时收到通知。通常与引用队列(Reference Queue)一起使用,可以在对象被回收时执行一些清理或其他操作。