0

我们有一个 JavaEE 应用程序,它使用 jython 来执行一些 python 脚本。渐渐地,使用的堆空间变得越来越大,直到没有更多的堆空间。在堆转储中,我可以确定有很多 Py* 类。

所以我写了一个小测试程序:TestApp

public class TestApp {
    private final ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    private HashMap<String, ScriptEngine> scriptEngines = new HashMap<String, ScriptEngine>();
    private final String scriptContainerPath = "";

    public static void main(String[] args) throws InterruptedException {
        int counter = 1;
        while(true) {
            System.out.println("iteration: " + counter);
            TestApp testApp = new TestApp();
            testApp.execute();
            counter++;
            Thread.sleep(100);
        }
    }

    void execute() {
        File scriptContainer = new File(scriptContainerPath);
        File[] scripts = scriptContainer.listFiles();
        if (scripts != null && scripts.length > 0) {
            Arrays.sort(scripts, new Comparator<File>() {
                @Override
                public int compare(File file1, File file2) {
                    return file1.getName().compareTo(file2.getName());
                }
            });

            for (File script : scripts) {

                String engineName = ScriptExecutor.getEngineNameByExtension(script.getName());
                if(!scriptEngines.containsKey(engineName)) {
                    scriptEngines.put(engineName, scriptEngineManager.getEngineByName(engineName));
                }
                ScriptEngine scriptEngine = scriptEngines.get(engineName);

                try {
                    ScriptExecutor scriptExecutor = new ScriptExecutor(scriptEngine, script, null);
                    Boolean disqualify = scriptExecutor.getBooleanScriptValue("disqualify");
                    String reason = scriptExecutor.getStringScriptValue("reason");
                    System.out.println("disqualify: " + disqualify);
                    System.out.println("reason: " + reason);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            // cleanup
            for(Map.Entry<String, ScriptEngine> entry : scriptEngines.entrySet()) {
                ScriptEngine engine = entry.getValue();             
                engine.getContext().setErrorWriter(null);
                engine.getContext().setReader(null);
                engine.getContext().setWriter(null);
            }
        }
    }
}

脚本执行器

public class ScriptExecutor {
    private final static String pythonExtension = "py";
    private final static String pythonEngine = "python";
    private final ScriptEngine scriptEngine;

    public ScriptExecutor(ScriptEngine se, File file, Map<String, Object> keyValues) throws FileNotFoundException, ScriptException {
        scriptEngine = se;
        if (keyValues != null) {
            for (Map.Entry<String, Object> entry : keyValues.entrySet()) {
                scriptEngine.put(entry.getKey(), entry.getValue());
            }
        }
        // execute script
        Reader reader = null;
        try {
            reader = new FileReader(file);
            scriptEngine.eval(reader);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    // nothing to do
                }
            }
        }
    }

    public Boolean getBooleanScriptValue(String key) {
        // convert Object to Boolean
    }

    public String getStringScriptValue(String key) {
        // convert Object to String
    }

    public static String getEngineNameByExtension(String fileName) {
        String extension = fileName.substring(fileName.lastIndexOf(".") + 1);

        if (pythonExtension.equalsIgnoreCase(extension)) {
            System.out.println("Found engine " + pythonEngine + " for extension " + extension + ".");
            return pythonEngine;
        }
        throw new RuntimeException("No suitable engine found for extension " + extension);
    }
}

在指定的目录中有 14 个 python 脚本,它们都看起来像这样:

disqualify = True
reason = "reason" 

我使用以下 VM 参数启动该程序:-Xrs -Xms16M -Xmx16M -XX:MaxPermSize=32M -XX:NewRatio=3 -Dsun.rmi.dgc.client.gcInterval=300000 -Dsun.rmi.dgc.server。 gcInterval=300000 -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -server

这些是我们的 AppServer 运行时使用的参数。在我的测试用例中,只有 Xms、Xmx 和 MaxPermSize 更小。

当我运行这个应用程序时,我可以看到 CMS Old Gen 池增加到其最大大小。之后,Par Eden Space 池会增加。此外,ParNewGC 在任何时候都不再运行。清理部分改善了情况,但没有解决问题。有人知道为什么我的堆没有完全清理吗?

4

1 回答 1

0

我想我已经找到了解决问题的方法:我删除了 JSR223 的东西,现在直接使用 PythonInterpreter。

于 2013-02-22T07:57:59.870 回答