GroovyClassLoader和permgen

时间:2013-12-13 12:33:34

标签: java groovy

我们很遗憾地得到了PermgenError。 jmap -permstat返回如下内容:

0x00000006c17f8520  1   1968    0x00000006ad000000  dead    sun/reflect/DelegatingClassLoader@0x00000007e00686e0
0x00000006babab308  3   19920   0x00000006b6874f88  dead    groovy/lang/GroovyClassLoader$InnerLoader@0x00000007e975bb80
0x00000006bca26800  3   15776   0x00000006b6874f88  dead    groovy/lang/GroovyClassLoader$InnerLoader@0x00000007e975bb80
0x00000006bb84db80  3   16176   0x00000006b6874f88  dead    groovy/lang/GroovyClassLoader$InnerLoader@0x00000007e975bb80
0x00000006b71dffd0  3   19272   0x00000006b6874f88  dead    groovy/lang/GroovyClassLoader$InnerLoader@0x00000007e975bb80

2小时后jmap -permstat返回:

0x00000006c17f8520  1   1968    0x00000006ad000000  dead    sun/reflect/DelegatingClassLoader@0x00000007e00686e0
0x00000006babab308  3   19920   0x00000006b6874f88  dead    groovy/lang/GroovyClassLoader$InnerLoader@0x00000007e975bb80
0x00000006bca26800  43  437232 0x00000006b6874f88   dead    groovy/lang/GroovyClassLoader$InnerLoader@0x00000007e975bb80
0x00000006bb84db80  3   16176   0x00000006b6874f88  dead    groovy/lang/GroovyClassLoader$InnerLoader@0x00000007e975bb80
0x00000006b71dffd0  7   16176 0x00000006b6874f88    dead    groovy/lang/GroovyClassLoader$InnerLoader@0x00000007e975bb80

正如您所看到的,classLoader加载的类数量增加,例如从3增加到43。 这只是permstat的短片段 - (总数= 4676 53521 436264528 alive = 1,dead = 4675)。我们使用参数运行java:

-XX:+DisableExplicitGC -server -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:MaxGCPauseMillis=800 -XX:+CMSParallelRemarkEnabled -XX:CMSFullGCsBeforeCompaction=1 -XX:+CMSClassUnloadingEnabled

但是在permstat中,所有的clasloader都死了。 我们的代码解析了groovy类:

class GroovyInvoker {

    private static GroovyClassLoader gcl = new GroovyClassLoader(Thread.currentThread()
            .getContextClassLoader());

    protected static GroovyInvoker getInstance(String fileName, String className)
            throws BaseScriptingException {

        GroovyInvoker instance = new GroovyInvoker();
        instance.setFileName(fileName);
        instance.setClassName(className);
        Class clazz;
        clazz = GroovyInvoker.gcl.parseClass(instance.getFile(fileName));
        Script aScript = (Script) clazz.newInstance();
        instance.setScript(aScript);

        return instance;
    }           
}

我读了这个主题:Locating code that is filling PermGen with dead Groovy code并回答了metaClassRegistry问题,但我们现在无法在prod服务器上测试它。我的问题是: 为什么当Groovy使用缓存时,GroovyClassLoader加载的类数量会增加?这个数字不应该是常数吗? 为什么gc没有收集死亡的类加载器?

Java版本1.6.0_26。 Groovy版本1.5.8。

1 个答案:

答案 0 :(得分:1)

我认为它与此有关: http://jira.codehaus.org/browse/GROOVY-6494

如果您不需要上下文类加载器,那么GroovyClassLoader将不会泄漏。