java - 调用外部 Python 进程时超出 GC 开销限制
问题描述
我有一个调用python脚本的java程序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()
and来停止输入流读取器,stdError.close()
并且我正在通过 using 杀死进程p1.destroy()
,但我认为这并没有破坏子进程,或者我还缺少其他什么东西,我还有什么其他方法可以实现这一点。
谢谢你。
解决方案
推荐阅读
- java - Array/ArrayList 如何直接指向对象的索引?或者 ArrayList 如何知道对象存储在这个地方?
- java - 来自 subscribeToStreamingNotifications 的带有 Item.bind() 的 AccessDenied
- c# - 如何使用 newtonsoft JSON 反序列化器反序列化 Geopoint?
- java - WebSocket 客户端关闭连接
- r - 使用 R 预测多个时间序列
- docker - 我应该/如何避免多个相互依赖的项目的 Docker compose 文件的重复部分
- python - Pandas Dataframe - 用 current_time 替换 NULL 值
- typescript - 打字稿:从泛型类型中获取文字值
- reactjs - Reactjs:以编程方式关注以编程方式生成的输入
- browser - 如何手动停止 getDisplayMedia 流以结束屏幕捕获?