- 浏览: 124910 次
- 性别:
- 来自: 北京
最新评论
-
C_J:
有必要这么鸡冻咩?
编写自己的ClassLoader知识点 -
jason61719:
你这不是说了等于没说吗……就解析个loadClass(),谁不 ...
编写自己的ClassLoader知识点 -
jiming:
tedeyang 写道很好的改进,不过话说回来,mybatis ...
开源,从关注产品社区做起(ibatis3.x的最近一个issue展示) -
C_J:
独爱Java 写道好像实际用处并不是很大,只是多了解了有这个东 ...
Java内存模型笔记 -
独爱Java:
好像实际用处并不是很大,只是多了解了有这个东西而已。。。
Java内存模型笔记
题记:
前几天讨论到WeakHashMap(这个是个弱引用的Map,用于缓存,弱引用的特点是随时被GC回收),WHM在每次put()会间接地调用expungeStaleEntries()来从去除实体。今天看到Hudon的WeakLogHandler的应用就怀着学习的目的来看看WeakRefence。(软引用在内存不足的时候被GC回收,弱引用可能在每次都被GC回收,她们的机制或原理是什么呢?我总是很好奇)
分析:
粗略看了下设计,有很多疑点不懂:
1、pending对象的用处?
2、ReferenceHandler线程什么时候notify呢?
3、到底哪些对象会被GC回收呢?
查了点资料后:http://www.ibm.com/developerworks/cn/java/i-garbage2/index.html
才有了点领悟。
“在 Java 1.2.2 中,整个引用处理发生很大的改变。其目标是平等地对待所有的引用,并以相同的方式处理它们。因此,我们实际上在堆上创建两个单独的对象 —— 对象本身和一个单独的 “引用” 对象。可选地,引用对象可以与一个队列相关联,当 referent 变得不可获得时,可以将引用对象添加到该队列中。”
所有引用都有个“引用队列”,也就是代码里的queue,ReferenceHandler 线程负责把reference入队列,如果未“标记”或者该引用没有指向任何Queue,则等待GC回收。
这里所谓的“标记”是指GC的标记阶段,也就是GC标记所有活动的对象,从效率上考虑,GC不会去标记unreachable的对象,而未“标记”的对象就是“垃圾”了。
所以作为WeakReference引用就不会被“标记”(register),从而会被GC回收掉,这点与Soft引用不同,Soft引用被标记但在内存不足的情况下,被特殊处理。
有分析不正确的地方,还请指正,也是处于学习阶段:)
弱引用Logger例子:
public final class WeakLogHandler extends Handler { private final WeakReference<Handler> target; private final Logger logger; // Logger的管理对象 public WeakLogHandler(Handler target, Logger logger) {// 典型的把管理对象传入 this.logger = logger; logger.addHandler(this);// 把干活对象加入管理对象中。 this.target = new WeakReference<Handler>(target); // 新建弱引用,且未指定queue } public void publish(LogRecord record) { Handler t = resolve(); if(t!=null) t.publish(record); } public void flush() { Handler t = resolve(); if(t!=null) t.flush(); } public void close() throws SecurityException { Handler t = resolve(); if(t!=null) t.close(); } private Handler resolve() { Handler r = target.get(); // get引用,判断是否已经被回收 if(r==null) logger.removeHandler(this); return r; } @Override public void setFormatter(Formatter newFormatter) throws SecurityException { super.setFormatter(newFormatter); Handler t = resolve(); if(t!=null) t.setFormatter(newFormatter); } }
看看小兵WeakRefence的定义:
public class WeakReference<T> extends Reference<T> { public WeakReference(T referent) { super(referent); } public WeakReference(T referent, ReferenceQueue<? super T> q) { super(referent, q); } }
看到基本共享父类Reference。(莫非JAVA引用默认就是弱引用?)
看看Reference类定义:
public abstract class Reference<T> { private T referent; /* Treated specially by GC */ ReferenceQueue<? super T> queue; // 从后面分析来看,这个就是引用队列了,被标记的引用入队列 Reference next; // 指向下一个引用 transient private Reference<T> discovered; /* used by VM */ /* Object used to synchronize with the garbage collector. The collector * must acquire this lock at the beginning of each collection cycle. It is * therefore critical that any code holding this lock complete as quickly * as possible, allocate no new objects, and avoid calling user code. */ static private class Lock { }; private static Lock lock = new Lock(); // GC线程在回收的时候的锁。 /* List of References waiting to be enqueued. The collector adds * References to this list, while the Reference-handler thread removes * them. This list is protected by the above lock object. */ private static Reference pending = null; /* -- Constructors -- */ Reference(T referent) { this(referent, null); } Reference(T referent, ReferenceQueue<? super T> queue) { this.referent = referent; this.queue = (queue == null) ? ReferenceQueue.NULL : queue;// 弱引用就是一个NULL的引用队列 } /* High-priority thread to enqueue pending References */ private static class ReferenceHandler extends Thread {// 注意这里是static线程,且优先级最高。 ReferenceHandler(ThreadGroup g, String name) { super(g, name); } public void run() { for (;;) { Reference r; synchronized (lock) { if (pending != null) { r = pending; Reference rn = r.next; pending = (rn == r) ? null : rn; r.next = r; } else { try { lock.wait(); } catch (InterruptedException x) { } continue; } } // Fast path for cleaners if (r instanceof Cleaner) { ((Cleaner)r).clean(); continue; } ReferenceQueue q = r.queue; if (q != ReferenceQueue.NULL) q.enqueue(r); // 构造函数里面未指定queue,则指向NULL queue } } } static { ThreadGroup tg = Thread.currentThread().getThreadGroup(); for (ThreadGroup tgn = tg; tgn != null; tg = tgn, tgn = tg.getParent()); Thread handler = new ReferenceHandler(tg, "Reference Handler"); /* If there were a special system-only priority greater than * MAX_PRIORITY, it would be used here */ handler.setPriority(Thread.MAX_PRIORITY); handler.setDaemon(true); handler.start(); } /* -- Referent accessor and setters -- */ /** * Returns this reference object's referent. If this reference object has * been cleared, either by the program or by the garbage collector, then * this method returns <code>null</code>. 如果引用被程序或GC回收,则返回null */ public T get() { return this.referent; } /** * Clears this reference object. Invoking this method will not cause this * object to be enqueued. */ public void clear() { this.referent = null; } /* -- Queue operations -- */ /** * Tells whether or not this reference object has been enqueued, either by * the program or by the garbage collector. If this reference object was * not registered with a queue when it was created, then this method will * always return <code>false</code>. * 当前引用是否被程序或者GC入队列,如果创建的时候未注册,则返回false */ public boolean isEnqueued() { /* In terms of the internal states, this predicate actually tests whether the instance is either Pending or Enqueued */ synchronized (this) { return (this.queue != ReferenceQueue.NULL) && (this.next != null); } } /** * Adds this reference object to the queue with which it is registered, * if any. */ public boolean enqueue() { return this.queue.enqueue(this); } /* -- Constructors -- */ Reference(T referent) { this(referent, null); } Reference(T referent, ReferenceQueue<? super T> queue) { this.referent = referent; this.queue = (queue == null) ? ReferenceQueue.NULL : queue; } }
评论
GC采用很多策略进行GC回收。其中为了消除内存碎片,采用“复制”策略:
JVM把内存拆分了3大区域,就是GC中常说的:eden、survivor、old,把eden区域“存活”的对象copy到survivor区域,然后清空eden所有内存。
另外GC根据“根搜索”策略来判断对象是否“存活”,我不确定是否跟上面提及到的ReferenceQueue有关。
希望有大大讲解下呗~
1.private static Reference pending = null;,pending对象是一个static对象,他是等待入引用队列的引用。
2.private static class ReferenceHandler extends Thread,看到ReferenceHandler其实是个静态全局线程,优先级最高,他的工作就是一发现pending引用不为NULL,就进行入引用队列操作,可以猜测,当pending不为NULL时,ReferenceHandler被吹起来。
3.其实Reference就是一个弱引用的实现,判断一个对象引用是否在引用队列里,或者说GC判断对象是否回收,靠Reference里的isEnqueued方法进行判断:
public boolean isEnqueued() { /* In terms of the internal states, this predicate actually tests whether the instance is either Pending or Enqueued */ synchronized (this) { return (this.queue != ReferenceQueue.NULL) && (this.next != null); } }
所以可以猜测每个Reference的ReferenceQueue应该在构造函数时被赋值,发现果然:
Reference(T referent, ReferenceQueue<? super T> queue) { this.referent = referent; this.queue = (queue == null) ? ReferenceQueue.NULL : queue; }
对于入引用队列的线程,有个很巧妙的设计:
r = pending; Reference rn = r.next; pending = (rn == r) ? null : rn; r.next = r;
把next重新指向pending,这样不断循环入队列。
发表评论
-
iOS入门(ongoing)
2012-09-13 11:32 1244Record it: The overview of ... -
Stuff about Android
2011-07-09 16:15 1029Foreword: long time ... -
JQuery初体验(Demo)
2011-05-22 13:43 1408Demo:Show <meta content ... -
Java内存模型笔记
2011-04-13 15:48 1504题记: 看到C/C++ ... -
Radiant_The Popular Ruby's CMS Demo篇
2011-04-02 14:49 1192题记: 上篇 记录我第一次安装Rodiant经过和 ... -
Radiant_The Popular Ruby’s CMS安装篇
2011-03-28 00:48 1231题记: 今天第一次参加JE的线下活动,robbin等 ... -
关于Azul 并发垃圾回收器
2011-03-26 14:40 1274题记: 总感觉JE讨论的帖子的东西都比较滞后,所以会 ... -
phpCMS & jQuery是我该做的(阉割了)
2011-02-27 23:02 81WD讲究以plugin挂载为结构,我需要构造一个p ... -
我的玩意:J2ME的Criteria初探
2011-01-20 21:59 980题记: 前几天跟初中同学聊天,他问我能不能做一个GP ... -
编写自己的ClassLoader知识点
2011-01-13 14:41 1839题记: 看到InfoQ关于ClassLoader的文 ... -
周末好玩,用短信控制你的计算机
2011-01-10 16:34 2905Snapshot: 详情 ... -
About Dock Plugin on Mac
2010-11-21 22:47 1429题记: 第一次接触MAC的开发..... ... -
可变hashcode的隐患和序列化安全
2010-10-25 00:55 1291可变hashcode的隐患 为识别对象,JDK ... -
体验OSGi(helloworld.jar)—富app的热拔插
2010-10-18 23:22 2376记得以前工作的时候,有天direct manager问 ... -
MongoDB on DAO with Java Language
2010-08-26 19:17 1396A Quick Tour Using the Java d ... -
Getting Start on Mongodb
2010-08-26 01:29 1452题记: 最近老和同学聊到non-relational ... -
Java Media Framework本地玩转摄像头
2010-08-04 00:57 16901、简介The JavaTM Media Framework ... -
《重构》读书笔记
2010-05-09 00:05 1004Martin Fowler于2003年出版 ... -
RPC之WebServices&RMI&JMS,phprpc框架?(待续)
2010-05-06 22:31 55前段时间写过基本的WebServices,也没再做深入 ... -
Java应用中的SQL注入攻击和防范
2010-04-24 01:06 6654说说自己对注入的 ...
相关推荐
这是一个java队列实现的全部工程文件,包含了所有代码,具体的设计文档在上传的另外文件中。这个工程能够实现所有队列的操作,运行没有问题。设计的是在应用程序上的基于界面的队列操作的实现。
java队列实现(顺序队列、链式队列、循环队列)
主要介绍了Java引用队列和虚引用,结合实例形式分析了java引用队列和虚引用相关概念、原理与使用方法,需要的朋友可以参考下
JAVA 的五种引用类型及引用队列 在介绍 JAVA 的五种引用之前,先介绍一下什么是引用,以及引用和对象之间的关系 什么是引用 众所周知,JAVA 是一种面向对象的语言,在 JAVA 程序运行时,对象是存储在堆内存...
Java消息队列常见面试题,2022年最新,104页,祝您斩获高薪offer! Java消息队列常见面试题,2022年最新,104页,祝您斩获高薪offer! Java消息队列常见面试题,2022年最新,104页,祝您斩获高薪offer! Java消息...
java 自定义Queue队列 java 自定义Queue队列
redis 案例。包含, 队列操作, socket通信, 以及 socket 和 redis 配合 redis 案例。包含, 队列操作, socket通信, 以及 socket 和 redis 配合
优先队列-java可以选择属性和升序降序
java定时器、多线程(池)、java队列的demodemo,下载看看看吧
用Java实现一个队列
主要给大家介绍了java利用delayedQueue实现本地的延迟队列的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
1、什么是死信队列 2、死信队列应用场景 3、延迟消息概念 4、死信队列使用流程图 5、死信队列应用(管理控制台) 6、死信队列应用(Java代码)
NULL 博文链接:https://128kj.iteye.com/blog/1665616
JAVA建立队列与堆栈,希望对大家有用啊
java多线程模拟队列实现排队叫号,多线程模拟排队叫号取号 java多线程模拟队列实现排队叫号,多线程模拟排队叫号取号
主要介绍了JAVA 实现延迟队列的方法,文中讲解非常详细,供大家参考和学习,感兴趣的朋友可以了解下
队列 死信队列(Dead Letter Queue,DLQ)是消息队列中一种特殊的队列,用于存储处理失败的消息或...在实际应用中,死信队列通常与消息队列服务(如RabbitMQ、Kafka等)结合使用,以提高消息处理的可靠性和容错能力。
关于站和队列的java语言的代码 上传来赚几个积分的
java 多线程 队列工厂
Java Design Demo -简单的队列(java android) 博客:http://write.blog.csdn.net/postedit/8577141