[英]Compiled Groovy Classes - GC
This is a follow on to a previous question: Using GroovyClassLoader from Java - Classes not GC'd 这是对前一个问题的回答: 使用Java中的GroovyClassLoader - 类不是GC
I was previously loading Groovy classes in a java environment using a custom groovy classloader, and to get them GC'd correctly I had to explicitly clear the meta class registry as so: 我以前使用自定义groovy类加载器在java环境中加载Groovy类,并且为了正确地使用GC,我必须明确地清除元类注册表:
for(Class<?> c : groovyClassLoader.getLoadedClasses()) {
GroovySystem.getMetaClassRegistry().removeMetaClass(c);
}
I have now switched to pre-compiling the groovy classes in to a JAR that I am loading via a normal java classloader, and am now seeing the perm gen memory leaks again when I try to re-load the updated classes. 我现在已经切换到将groovy类预编译为JAR,我正在通过普通的java类加载器加载,现在我在尝试重新加载更新的类时再次看到perm gen内存泄漏。
Anyone know if i need to do anything special to get my classes GC'd? 任何人都知道我是否需要做一些特别的事情来获得我的课程GC'd? I am dynamically adding constructor/methods to some of the groovy at runtime, so assume I still need to clear the metaClassRegistry?
我在运行时动态地向一些groovy添加构造函数/方法,所以假设我仍然需要清除metaClassRegistry?
The above code does not work (obviously) as I am no longer using a groovyClassLoader, but I have also tried just iterating the metaClassRegistry and that just returns null for the registry: 上面的代码(显然)不起作用,因为我不再使用groovyClassLoader,但我也试过迭代metaClassRegistry并且只为注册表返回null:
def metaClasses = GroovySystem.getMetaClassRegistry().iterator()
while( metaClasses.hasNext()){
def thisGuy = metaClasses.next()
GroovySystem.getMetaClassRegistry().removeMetaClass(thisGuy)
}
It was the same problem, but just needed a different route to get to the MetaClassRegistry. 这是同样的问题,但只需要一个不同的路线来进入MetaClassRegistry。 The following code did the job:
以下代码完成了这项工作:
def registry = metaClass.getRegistry()
def iterator = registry.iterator()
while ( iterator.hasNext() ){
def mc = iterator.next()
registry.removeMetaClass( mc.getJavaClass() )
}
(note, this is called from one of the compiled groovy classes so is using the same classloader/metaClassRegistry) (注意,这是从一个已编译的groovy类中调用的,所以使用相同的classloader / metaClassRegistry)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.