I have some problems related to ProcessBuilder. A program is basically a simple shell that invokes a command line script.
When you run the script yourself through the terminal, memory consumption remains below 2G. When a script is run through a java wrapper, memory consumption explodes, and even 8G quickly fills up, which leads to memory errors.
The code to start the process is simple:
public static int execute(String command) throws IOException
{
System.out.println("Executing: " + command);
ProcessBuilder pb = new ProcessBuilder(command.split(" +"));
Process p = pb.start();
StreamConsumer stderr = new StreamConsumer(p.getErrorStream(), "stderr");
StreamConsumer stdout = new StreamConsumer(p.getInputStream(), "stdout");
new Thread(stderr).start();
new Thread(stdout).start();
try {
return p.waitFor();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
The StreamConsumer class is simply a class that consumes stdout / stderr streams and displays them on the console.
... the question is, why is there a burst of memory on earth?
Regards,
Arnaud
Edit:
- I use ProcessBuilder or Runtime.getRuntime.exec (...), the result is the same.
- , , unix-,
shell script :
sort big-text-file > big-text-file.sorted
2 :
, StreamConsumer, , :
class StreamConsumer implements Runnable
{
InputStream stream;
String descr;
StreamConsumer(InputStream stream, String descr) {
this.stream = stream;
this.descr = descr;
}
@Override
public void run()
{
String line;
BufferedReader brCleanUp =
new BufferedReader (new InputStreamReader (stream));
try {
while ((line = brCleanUp.readLine ()) != null)
System.out.println ("[" + descr + "] " + line);
brCleanUp.close();
} catch (IOException e) {
}
}
}