连续运行shell脚本时JVM进入休眠状态

时间:2015-02-02 07:14:18

标签: java linux bash shell jvm

我有几个shell脚本需要一个接一个地运行,Java需要获取错误和输出流。我想确保每个shell只在前一个shell完成时执行。我试过这个,它在很多条件下都能正常工作。但是一些shell脚本超过10到50MB。当我执行它时,JVM挂起。我尝试了ProcessBuilder和Runtime。两者提供相同的结果。我认为JVM进入退出状态。当我运行它时,shell脚本在普通终端中运行。只有从Java运行时它才会出现问题。我尝试了两种类型的代码,如下所示。

Runtime.getRuntime().freeMemory();
Runtime.getRuntime().runFinalization();
Runtime.getRuntime().gc();
String[] envp = {"WORK_HOME="+Cisma.constant.getWork_home_directory(),                      "BUILD_DIR="+Cisma.constant.getWork_home_directory()+"verif/compile/build"};
Process p = Runtime.getRuntime().exec(commands, envp); //hangs in this line
p.waitFor();
result.add(Output, getOutput(p));
result.add(Error, getError(p));
System.out.println(commands[0]+"End "+p.exitValue()); //printing 0, for our hang state it does not go to this line
Runtime.getRuntime().freeMemory();
Runtime.getRuntime().runFinalization();
Runtime.getRuntime().gc();
p.destroy();

我使用的另一种方法是,

ProcessBuilder pb = new ProcessBuilder(commands);
pb.directory(new File(working_directory));
pb.environment().put("WORK_HOME", Cisma.constant.getWork_home_directory());
pb.environment().put("BUILD_DIR", Cisma.constant.getWork_home_directory()+"verif/compile/build");
//pb.inheritIO(); // tried but did not work
//File f = new File ("temp");
//pb.redirectError(f);
//File o = new File ("temp1");
//pb.redirectOutput(o);
Process p = pb.start(); // hangs in this line
p.getOutputStream().flush();
p.waitFor();
p.destroy();

我的项目名称是CISMA_Regression main位于com.cisma.Cisma

src/apb_test17% /bin/ps -edf | grep java
jeevan    5263  5262  7 11:47 ?        00:03:13 /usr/bin/java -Dosgi.requiredJavaVersion=1.6 -XX:MaxPermSize=256m -Xms40m -Xmx512m -jar /home/jeevan/Application/eclips
jeevan    7818  5263  0 12:21 ?        00:00:03 /usr/java/jdk1.8.0_31/bin/java -Dfile.encoding=ANSI_X3.4-1968 -classpath /home/jeevan/workspace/CISMA_Regression/target
jeevan    9751  6681  0 12:29 pts/10   00:00:00 grep --color=auto java
src/apb_test17% top -b -n1 | grep 5263
 5263 jeevan    20   0 6016600 675364  54836 S 0.000 8.296   3:14.08 java
src/apb_test17% ps -edf | grep java
jeevan    5263  5262  5 11:47 ?        00:03:35 /usr/bin/java -Dosgi.requiredJavaV
jeevan    7818  5263  0 12:21 ?        00:00:04 /usr/java/jdk1.8.0_31/bin/java -Df
jeevan   10324  6681  0 12:50 pts/10   00:00:00 grep --color=auto java

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

通过jconsole检查你的记忆,这将在jdk中提供。 使用以下命令增加jvm。

-Xms<size>        set initial Java heap size
-Xmx<size>        set maximum Java heap size
-Xss<size>        set java thread stack size

链接how to find memory in java