讲这么抽象干什么?我给你一定子!本文仅仅对引用类型的一些使用和特点进行了表述
进行测试时,需要使用-Xmx20M进行约束才能测试出效果来
强引用、软引用、弱引用、虚引用
1.强引用
平时我们使用的引用,例如:Object obj = new Object();
就叫强引用
在内存中一定有一小块变量记录这个变量:obj,它指向一大块内存,我们new出来的new Object();
特点:就算程序OOM,强引用也不会被回收
2.软引用
SoftReference
声明一个软引用:
public static void main(String[] args) throws IOException, InterruptedException {
//软引用
SoftReference<byte[]> sr = new SoftReference<>(new byte[1024 * 1024 * 10]);
System.out.println(sr.get());
//强引用
byte[] bytes = new byte[1024*1024*15];
System.out.println(sr.get());
}
第一次输出[B@7c30a502,第二次输出null
内存足够时,软引用会被保留,不足时,将会被回收
这时,它的内存布局就会变成这样
byte[]换成其他对象也是一样,与这个没关系
特点:如果内存不足,软引用将会被回收
3.弱引用
WeakReference
声明一个弱引用:
public static void main(String[] args) throws IOException {
WeakReference<Object> wr = new WeakReference<>(new byte[1024]);
System.out.println(wr.get());
System.gc();
System.out.println(wr.get());
}
两次打印输出值分别为:
[B@7c30a502
null
特点:在内存回收时,弱引用将会被回收
4.虚引用
PhantomReference
声明一个虚引用:
static final ReferenceQueue<Object> QUEUE = new ReferenceQueue<>();
static final List<Object> LIST = new ArrayList<>();
public static void main(String[] args) throws IOException, InterruptedException {
PhantomReference<Object> pr = new PhantomReference<>(new Object(), QUEUE);
new Thread(() -> {
while (true) {
LIST.add(new byte[1024 * 1024]);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(pr.get());
}
}).start();
new Thread(() -> {
while (true) {
Reference<? extends Object> poll = QUEUE.poll();
if (poll != null) {
System.out.println("被回收了:" + poll);
}
}
}).start();
Thread.sleep(500);
}
打印为null
在虚引用在被回收后,会给出一个信号,这个信号会存在QUEUE中
我们可以监听这个队列,获得一些信息
其实就是用来管理直接内存的,这个过程是JVM完成的
特点:无论对象是否被回收,都无法获取虚引用得值