Groovy 脚本引起的 Old GC问题

近期上线了一个系统,鉴权部分使用了Groovy脚本,示例代码以下html

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("groovy");

String function = String.format("def getTargetParamValue(%s) {return \"%s\"}", "o", "$o");
engine.eval(function);
Invocable invocable = (Invocable) engine;

Object result = invocable.invokeFunction("getTargetParamValue", "test-string");
System.out.println(result);

这段代码定义了一个Groovy的方法,根据传进去的参数返回对应的值。java

因为生产环境流量很大,这段代码被频繁执行。测试时的代码以下node

public class ScriptEngineTest {

    public static void main(String[] args) {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("groovy");
        //测试时改成死循环
        for (int i = 0;; i++) {
            try {

                String function = String.format("def getTargetParamValue(%s) {return \"%s\"}", "o", "$o");
                engine.eval(function);
                Invocable invocable = (Invocable) engine;

                Object result = invocable.invokeFunction("getTargetParamValue", "test-string");
                System.out.println(result);

                TimeUnit.MICROSECONDS.sleep(100);

                System.out.println(new Date().toLocaleString());

            } catch (Exception e) {
                String errorMsg = String.format("异常!%s", e.getMessage());
                System.out.println(errorMsg);
            }

        }
    }
}

模拟生产环境的状况,每秒钟执行10次。经过VusualVM观察JVMweb

  • CPU使用状况,能够看到在每次堆内存扩容的时候,CPU使用量会有明显增长
    express

  • 堆内存使用状况
    缓存

  • metaspace使用量一直在增长
    测试

  • 类加载状况,total loaded classes一直在增长
    spa

  • 线程
    线程

  • dump内存
    3d

可见,每次循环中生成的 Groovy method在方法执行完成以后并无被释放掉,致使metaspace的使用量一直增长,最终撑爆JVM

针对以上问题,解决方法为每次将生成的方法缓存下了,下次要执行的时候从缓存中取。

private final ConcurrentHashMap<String, Invocable> concurrentHashMap = new ConcurrentHashMap<>();

private Object getInvokeResult(Object targetParam, String paramName, String expression) throws Exception {
    //targetParamClassName="com.umgsai.web.home.vo.NodeVO"
    String targetParamClassName = targetParam.getClass().getName();
    //expression="$nodeVO.bizOwner"
    //paramName="nodeVO"
    String functionKey = String.format("%s_%s_%s", targetParamClassName, paramName, expression);
    functionKey = StringUtil.replaceChars(functionKey, "$", "");
    functionKey = StringUtil.replaceChars(functionKey, ".", "_");
    //functionKey为方法的名称和concurrentHashMap的key,这里须要去掉特殊字符

    Invocable invocable = concurrentHashMap.get(functionKey);
    if (invocable != null) {
        //若是缓存中有,直接调用
        return invocable.invokeFunction(functionKey, targetParam);
    }

   //若是缓存中没有,生成方法,而且存到concurrentHashMap
    synchronized (lock) {
        invocable = concurrentHashMap.get(functionKey);
        if (invocable == null) {
            String function = String.format("def %s(%s) {return \"%s\"}", functionKey, paramName, expression);
            engine.eval(function);
            invocable = (Invocable) engine;
            concurrentHashMap.put(functionKey, invocable);
            if (log.isInfoEnabled()) {
                String msg = String.format("Create new Groovy function, functionKey=%s, paramName=%s, expression=%s",
                        functionKey,
                        paramName, expression);
                log.info(msg);
            }
        }
    }

    if (log.isInfoEnabled()) {
        log.info(String.format("Groovy function concurrentHashMap.size=%d", concurrentHashMap.size()));
    }

    return invocable.invokeFunction(functionKey, targetParam);
}

参考 http://www.zgxue.com/120/1204001.html
http://www.javashuo.com/article/p-hxnznolo-kh.html
https://www.jianshu.com/p/b1a46cc02377

转载请注明出处 http://www.javashuo.com/article/p-wdmarjgm-ko.html

相关文章
相关标签/搜索