使用groovy动态编译类、script时,经常会有gc溢出问题。其原因之一就是groovy把每次编译的文件或String做了缓存,而字符串的处理最易引发问题。位于于GroovyClassLoader.java类中,其代码如下:
每次groovy编译脚本后,都会缓存该脚本的Class对象,下次编译该脚本时,会优先从缓存中读取,这样节省掉编译的时间。这个缓存的Map由GroovyClassLoader持有,key是脚本的类名,而脚本的类名在不同的编译场景下(从文件读取脚本/从流读取脚本/从字符串读取脚本)其规则不同,当传入text时,class对象的命名规则为:
"script" + System.currentTimeMillis() + Math.abs(text.hashCode()) + ".groovy"
因此,每次编译的对象名都不同,都会在中添加一个class对象,导致class对象不可释放,随着次数的增加,编译的class对象将PERM区撑满。
注意:以下代码是必然把内存撑爆的:
GroovyShell shell = new GroovyShell();
String scriptText = "def mul(x, y) { x * y }\n println mul(5, 7)";
while (true) {
Script script = shell.parse(scriptText);
Object result = script.run();
}
而这段代码才是正解:
while (true) {
GroovyShell shell = new GroovyShell();
String scriptText = "def mul(x, y) { x * y }\n println mul(5, 7)";
Script script = shell.parse(scriptText);
Object result = script.run();
}
这是因为再多的GroovyShell,在gc时都可以被收掉,而其中容纳的Class也会随之消亡。
把GroovyShell换成GroovyClassLoader也是一样的用法。