Topic: Class对象什么时候被垃圾收集 |
Print this page |
1.Class对象什么时候被垃圾收集 | Copy to clipboard |
Posted by: dissip Posted on: 2004-07-22 17:02 ClassLoader在load一个新的Class时,会得到一个Class对象,此对象什么时候会被垃圾收集? Object.getClass()得到的Class对象和上述对象是一个吗?什么时候会被垃圾收集? Class.forName("xxx")呢? |
2.Re:Class对象什么时候被垃圾收集 [Re: dissip] | Copy to clipboard |
Posted by: haibo Posted on: 2004-07-22 17:34 dissip wrote: 同其他对象一样,没有被引用时就会被回收。
Ojbect.getClass() 和ClassLoader.loadClass(),还有Class.forName()返回的Class 实例肯定不是同一个instance. |
3.Re:Class对象什么时候被垃圾收集 [Re: dissip] | Copy to clipboard |
Posted by: dreamchenwen Posted on: 2004-07-22 21:07 在java中有垃圾回收机制,当一个对象没有被引用,在系统内存不足的时候,会自己回收。 |
4.Re:Class对象什么时候被垃圾收集 [Re: dissip] | Copy to clipboard |
Posted by: 心处理器 Posted on: 2004-07-22 21:45 具体什么时候被回收,要看JVM的“兴趣”,程序员无法控制 |
5.Re:Class对象什么时候被垃圾收集 [Re: haibo] | Copy to clipboard |
Posted by: dissip Posted on: 2004-07-23 10:22 haibo wrote: 什么时候没有被引用?对应的ClassLoader是不是有对此Class instance的strong reference? 是不是说:当没有引用时(包括对应的ClassLoader),class对象(可能)被回收之后,如果当再次new此class的instance时,难道会再次从disk中读取类文件? |
6.Re:Class对象什么时候被垃圾收集 [Re: haibo] | Copy to clipboard |
Posted by: think Posted on: 2004-07-23 12:15 1.Class对象一直被虚拟机引用,所以不会被垃圾搜集,直至虚拟机退出。 所以Class对象不应用作WeakHashMap的Key。 2.据此推算,Class.class、Object.getClass()和Class.forName("xxx")应该引用同一个对象。 以上不考虑ClassLoader和虚拟机自己放弃对Class对象引用的情况。 |
7.Re:Class对象什么时候被垃圾收集 [Re: dissip] | Copy to clipboard |
Posted by: dissip Posted on: 2004-07-23 12:32 are you sure? 我在读org.springframework.beans.CachedIntrospectionResults中,Rod Johnson就是用Class对象作WeakHashMap的Key。(spring framework 1.0.2) 而且他说:'Many thanks to Guillaume Poirier for pointing out the garbage collection issues and for suggesting this solution.' 说明他也考虑过这个问题后,做出的决策。 我正是由于对这个类的理解有困难,才题的这个问题。 |
8.Re:Class对象什么时候被垃圾收集 [Re: dissip] | Copy to clipboard |
Posted by: haibo Posted on: 2004-07-23 12:56 Class 对象可以得到load自己的ClassLoader,JVM记载有ClassLoader所load过的Class,但这并不能说明,ClassLoader或者JVM拥有对Class对象的引用,JVM清除Class对象与否,从api spec 中没能查到确凿的说明,同样在vm spec中也没查出。但我们可做一个试验,就是在请new过一个Object后,进入没有Object ref的域,然后将硬盘class文件删除,再次new 一个Object,如果成功,则说明jvm持有Object 的Class 信息,不会再从.class 文件中读取object的Class信息,否则就说明内存中的object Class信息在ClassLoader用过后被清理过 |
9.Re:Class对象什么时候被垃圾收集 [Re: haibo] | Copy to clipboard |
Posted by: think Posted on: 2004-07-23 16:30 to haibo : 有一点可以肯定,class加载后一般是驻留在内存的,参见 http://java.sun.com/docs/books/performance/1st_edition/html/JPRAMFootprint.fm.html#24368 http://java.sun.com/docs/books/performance/1st_edition/html/JPRAMFootprint.fm.html#11232 to dissip:spring为什么这样用很奇怪。但用WeakHashMap测试一下,大家会发现class是被jvm引用的。 public class JxlTester { public static void main(String[] args) { JxlTester t = new JxlTester(); t.test(); Map.Entry entry; for(Iterator it=t.weak.entrySet().iterator(); it.hasNext(); ) { entry = (Map.Entry)it.next(); System.out.println("value is: " + entry.getValue()); } } WeakHashMap weak = new WeakHashMap(); public void test() { Class c = JxlTester.class; weak.put(c, "test"); //注意; weak.put(new Integer(1), "test1"); weak.put("2", "test2"); //注意; System.gc(); } } 查了一下spring1.0,好像不是这样的,用的是HashMap。难道1.0.2改的? |
10.Re:Class对象什么时候被垃圾收集 [Re: dissip] | Copy to clipboard |
Posted by: dissip Posted on: 2004-07-23 19:05 同意: think:‘class加载后一般是驻留在内存的‘ haibo:‘但这并不能说明,ClassLoader或者JVM拥有对Class对象的引用’ think的测试说明:确实存在对class的引用。 但从haibo:’从api spec 中没能查到确凿的说明,同样在vm spec中也没查出‘,并不能断定这个测试对于所有的jvm实现都适用。 对于spring, 这样的解释似乎说的过去:如果确实存在从classloader到class的引用的话(或者其等价物),当classloader被释放,对应的放在WeakHashMap中的class和其数据也会被释放。 |
11.Re:Class对象什么时候被垃圾收集 [Re: dissip] | Copy to clipboard |
Posted by: floater Posted on: 2004-07-23 22:03 http://www.onjava.com/pub/a/onjava/2001/07/09/optimization.html |
12.Re:Class对象什么时候被垃圾收集 [Re: floater] | Copy to clipboard |
Posted by: think Posted on: 2004-07-24 09:17 看O'Reilly - Java Perfomance Tuning第四章的WeakHashMap效果一样。 |
13.Re:Class对象什么时候被垃圾收集 [Re: dissip] | Copy to clipboard |
Posted by: dissip Posted on: 2004-07-24 15:59 我找到了Guillaume Poirier 对spring framework 1.0.2org.springframework.beans.CachedIntrospectionResults中的WeakHashMap,WeakReference用法的解释:“ Claudio, I'm not completely sure to understand correctly your question, but I will try to answer as best as I can. WeakHashMap is implemented with WeakReference for keys, and strong reference for values. That means if the value has a strong reference on the key, then the key cannot be garbage collected until the WeakHashMap is ready for collection. However, if the value has no strong reference on its key, then being in the WeakHashMap won't prevent the key and value from being garbage collected if it is otherwise ready. The WeakHashMap knows when to remove the key (and the value with it) by using the notification provided by the java.lang.ref package. For more information on this, see: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ref/package-summary.html So the problem here with the CachedIntrospectionResults is that it uses BeanInfo and PropertyDescriptor that both have strong reference to the class (indirectly by a reference on Methods of the class). That will be solved with JDK 1.5 that uses a combinaison of Weak and Soft Reference to the Class and Method objects, but for 1.4.2, there's not really any better solution than to flush the Introspector's cache and/or use WeakReference on CachedIntrospectionResults. Using WeakReference on the CachedIntrospectionResults is safer, but decrease performance, and in such case a manual Instrospector.flushFromCaches(Class) must be used, so that the Instrospector does not keep a strong reference on the BeanInfo. When a webapp is hot-redeployed, a new ClassLoader is created to load the webapp, and the old one is thrown away, expected to be garbage collected. For the collection to happen, the server must clear any strong reference to the ClassLoader or its classes, and also the webapp must make sure that any code in parent ClassLoaders (or siblings) clear any reference it might have to any of the webapp's class. Hopefully this answers your question, but if it does not (or if it's not clear enough), please give me more details of what information you need. Regards, Guillaume “ 根据他的说法: 会对Class有strong reference的有:ClassLoader,java.beans.BeanInfo,java.beans.PropertyDescriptor,java.lang.reflect.Method.只有当对上述4中对象的strong reference全部消除后,对应的Class object才可能被垃圾收集 |
14.Re:Class对象什么时候被垃圾收集 [Re: dissip] | Copy to clipboard |
Posted by: think Posted on: 2004-07-24 17:41 是这样的。 我前面说的ClassLoader的情况就是指Web/EJB应用的hot-deploy问题。 onjava上有几篇讲classLoader的文章。 classLoader也是Singleton-DP在Web/EJB下有问题的原因之一,须通过classpath来解决。 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |