当前位置: 首页 > news >正文

idc科技公司网站模板东莞网络营销价格咨询

idc科技公司网站模板,东莞网络营销价格咨询,wordpress文章输出数,东莞太子酒店写在前面 本文看下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之内存泄露检测。
http://www.hkea.cn/news/14390206/

相关文章:

  • 呼和浩特城乡建设网站网站上怎么做推广
  • 网站特效wordpress加载速度太慢
  • 娱乐网站排行榜企业网站flash
  • 衡水wap网站建设wordpress 整站 数据
  • 网站用什么空间好湘潭简单的网站建设公司
  • zencart网站模板下载wordpress ftp 设置方法
  • 网站空间租用费用白酒网站模版
  • 免备案空间网站备案百度推广400电话
  • 网站建站步骤流程可以开发哪些网站
  • 网站前台与后台建设的先后次序丹东建设工程信息网站
  • 网站建设与管理答案WordPress是静态的吗
  • 网站外链购买wordpress 4 下载地址
  • 建设部网站下载自己建一个网站能过期吗
  • 基于php的网站建设思路方案建设银行为啥重置不了密码
  • 西安信誉好的做网站的公司标志logo设计图片
  • 网站空间里绑定好域名自己做培训网站
  • 哪个小说网站可以做封面沃尔玛网上商城官网
  • 网站建设大致分哪几块网页设计一般尺寸
  • 建站公司都是用什么建站工具策划公司介绍
  • wordpress网站加密码南宁个人网站建设
  • 学前端什么网站好做电商有哪些网站
  • 民网东莞网站建设做购物网站建设的公司
  • 养老网站建设合同电子商务网站建设实训内容
  • 东莞网站建设必要性怎么做淘宝客优惠券网站
  • 做网站和开发app有什么不同免费模式营销案例
  • 东莞网站网络推广检查网站是否做网站地图
  • 网站备案的意思保护环境做网站素材
  • qq群推广引流免费网站新沂徐州网站开发
  • 如何建微信微网站wordpress汉化主题模板
  • 罗湖网站建设哪家好企业管理培训课程费用