织梦 大型综合旅游网站 源码,html怎么做,网站导航栏固定,厦门网站建设厦门写在前面
本文看下Java的强#xff0c;软#xff0c;弱#xff0c;虚引用相关内容。
1#xff1a;各种引用介绍
顶层类是java.lang.ref.Reference,注意是一个抽象类#xff0c;而不是接口#xff0c;其中比较重要的引用队列ReferenceQueue就在该类中定义#xff0c;子…写在前面
本文看下Java的强软弱虚引用相关内容。
1各种引用介绍
顶层类是java.lang.ref.Reference,注意是一个抽象类而不是接口其中比较重要的引用队列ReferenceQueue就在该类中定义子类们共同使用
// java.lang.ref.Reference
public abstract class ReferenceT {// ...volatile ReferenceQueue? super T queue;// ...
}另以下测试案例使用的gc配置
-Xms10m -Xmx10m -XX:PrintGC1.1强引用
平时我们用的默认就是强引用。所以也就没有一个类似于StrongReference的类来代表强引用了。
1.2软引用
使用类SoftReference代表一个软引用比强引用稍微弱化些在内存空间充足时发生GC不会被回收但是在内存不足发生GC时将会被回收所以适合用在类似于缓存这种并不会对程序起到决定性作用的场景中。如下例子
public void softRefTest() {/**** author mikechen*/Object obj new Object();SoftReference softRef new SoftReferenceObject(obj);obj null;//删除强引用byte[] b new byte[1024 * 1024];System.gc();//调用gcSystem.out.println(gc之后的值 softRef.get()); // 对象依然存在
}运行
[GC (System.gc()) 2663K-1713K(9728K), 0.0011382 secs]
[Full GC (System.gc()) 1713K-1647K(9728K), 0.0055792 secs]
gc之后的值java.lang.Object330bedb4Process finished with exit code 0这里内存不足的场景我没有试出来要么就OOM了。
软引用也可以选择和ReferenceQueue来一起使用当软应用关联的对象被GC之后就会将软引用本身添加到队列中如下
public void softRefWithQueue() throws Exception {ReferenceQueueObject queue new ReferenceQueue();Object obj new Object();SoftReference softRef new SoftReferenceObject(obj, queue);//删除强引用obj null;//调用gcSystem.gc();System.out.println(gc之后的值: softRef.get()); // 对象依然存在,虽然GC但内存足够不会回收//申请较大内存使内存空间使用率达到阈值强迫gcbyte[] bytes new byte[1024 * 1024 * 6];//如果obj被回收则软引用会进入引用队列System.out.println(111);//调用gcSystem.gc();Reference? reference queue.remove(); // 因为没有触发内存不足的场景所以不会添加到队列中所以这里会卡着System.out.println(222);if (reference ! null) {System.out.println(对象已被回收: reference.get());// 对象为null}
}运行 同样试不出来内存不足被回收的场景。
1.3弱引用
弱引用和软引用的区别是在发生GC时不管内存是否足够都会被回收看个例子 private void weakRef() {Object o1 new Object();WeakReferenceObject w1 new WeakReferenceObject(o1);// System.out.println(o1);System.out.println(w1.get()); // 因为此时还有强引用肯定不会被回收o1 null; // 手动去除强引用System.gc();// System.out.println(o1);System.out.println(因为发生了GC所以就被回收掉了:);System.out.println(w1.get()); // 因为发生了GC所以就被回收掉了
}运行
java.lang.Object330bedb4
[GC (System.gc()) 1570K-745K(9728K), 0.0010741 secs]
[Full GC (System.gc()) 745K-622K(9728K), 0.0059361 secs]
因为发生了GC所以就被回收掉了:
nullProcess finished with exit code 0当然也可以和ReferenceQueue一起使用来监听对象被回收的动作:
private void weakRefWithQueueV1() throws Exception {CC o1 new CC();o1.setName(张三);ReferenceQueue referenceQueue new ReferenceQueue();MapWeakReference, String map new HashMap();WeakReferenceCC w1 new WeakReferenceCC(o1, referenceQueue);map.put(w1, w1.get().getName());
// System.out.println(w1);
//
// System.out.println(o1);System.out.println(w1.get()); // 因为此时还有强引用肯定不会被回收o1 null; // 手动去除强引用System.gc();// System.out.println(o1);System.out.println(因为发生了GC所以就被回收掉了:);System.out.println(w1.get()); // 因为发生了GC所以就被回收掉了final Reference ref referenceQueue.remove();System.out.println(map.get(ref) 被回收了); // 因为对象被回收所以弱引用对象本身会被放到队列中
}运行
org.example.Main$CC2503dbd3
[GC (System.gc()) 1647K-709K(9728K), 0.0009438 secs]
[Full GC (System.gc()) 709K-628K(9728K), 0.0058506 secs]
因为发生了GC所以就被回收掉了:
null
张三 被回收了Process finished with exit code 01.4虚引用
虚引用是最弱的的一种引用不决定对象的生命周期有跟没有一样即形同虚设必须和ReferenceQueue共同使用一般用来监控jvm的gc活动如下例子
private void weakPhantomWithQueueV1() throws Exception {CC o1 new CC();o1.setName(张三1);ReferenceQueue referenceQueue new ReferenceQueue();MapPhantomReference, String map new HashMap();PhantomReferenceCC w1 new PhantomReferenceCC(o1, referenceQueue);map.put(w1, o1.getName());
// System.out.println(w1);// System.out.println(o1);System.out.println(w1.get()); // 因为此时还有强引用肯定不会被回收o1 null; // 手动去除强引用System.gc();// System.out.println(o1);System.out.println(因为发生了GC所以就被回收掉了:);System.out.println(w1.get()); // 因为发生了GC所以就被回收掉了final Reference ref referenceQueue.remove();System.out.println(map.get(ref) 被回收了); // 因为对象被回收所以弱引用对象本身会被放到队列中
}运行
null
[GC (System.gc()) 1643K-741K(9728K), 0.0012171 secs]
[Full GC (System.gc()) 741K-627K(9728K), 0.0062680 secs]
因为发生了GC所以就被回收掉了:
null
张三1 被回收了Process finished with exit code 02在框架中的应用
2.1在netty中的应用
just go。
2.2在mybatis中的应用
TODO
写在后面
参考文章列表
Java四大引用详解强引用、软引用、弱引用、虚引用 。
netty之内存泄露检测。