调用外部Python进程时,超出了GC开销限制

时间:2019-03-11 06:29:29

标签: java python process

我有一个Java程序,该程序可按如下方式调用python脚本script.py

Process p1 = Runtime.getRuntime().exec("python3 /home/user/script.py " + id + " " + text);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p1.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p1.getErrorStream()));
String s;
StringBuilder t = new StringBuilder();
boolean isT = false;
while ((s = stdInput.readLine()) != null) {
    isT = true;
    t.append(s);
}
if (isT) {
    stdInput.close();
    stdError.close();
    p1.destroy();
    return t.toString();
}
logger.info(text);
StringBuilder error_text = new StringBuilder();
while ((s = stdError.readLine()) != null) {
    error_text.append(s);
}
logger.info(error_text.toString());
stdInput.close();
stdError.close();
p1.destroy();
return text;

只要满足某些条件,就会在循环中调用此代码段。我正在从python获取输出,并使用p1.getInputStream()收集它。

python脚本如下

import sys
text = ''
id_ = sys.argv[1]

for w in sys.argv[2:]:
    text += w + ' '

text = text.replace('\n', ' ')

list_ = [text]
ret_val = my_function(list_)
with open('/home/user/output.txt', 'a') as out:
    for r in ret_val:
        print(r)
        out.write(id_ + '\t\t' + r + '\n')

该脚本在开始时运行良好,但是随着时间的流逝,我收到以下错误消息

Exception in thread "..." java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOfRange(Arrays.java:3664)
    at java.lang.StringBuffer.toString(StringBuffer.java:669)
    at java.io.BufferedReader.readLine(BufferedReader.java:359)
    at java.io.BufferedReader.readLine(BufferedReader.java:389)
Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
    at java.util.LinkedHashMap.newNode(LinkedHashMap.java:256)
    at java.util.HashMap.putVal(HashMap.java:631)
    at java.util.HashMap.put(HashMap.java:612)
    ...

即使在此之后,python脚本也会被反复调用,并且一切正常,但是过了一段时间,我仍然收到如下错误消息

Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
Exception in thread "..." java.lang.OutOfMemoryError: GC overhead limit exceeded
...

并且我的python脚本停止响应。

我试图通过使用stdInput.close()stdError.close()来停止inputstreamreader,并且通过使用p1.destroy()来终止进程,但是我认为这并没有破坏子进程或还有什么我想念的吗,还有什么其他方法可以实现呢?

谢谢。

0 个答案:

没有答案