Java: ProcessBuilder makes hog memory

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();

  // display any output in stderr or stdout  
  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) {
            // TODO: handle exception
        }
    }
}
+5
2

: sort -o big-text-file.sorted big-text-file

?

+2

, , StreamConsumer , , ? :

//...  
final StreamConsumer stderr = new StreamConsumer(p.getErrorStream(), "stderr");
final StreamConsumer stdout = new StreamConsumer(p.getInputStream(), "stdout");
final Thread stderrThread = new Thread(stderr);
final Thread stdoutThread = new Thread(stdout);
stderrThread.setDaemon(true);
stdoutThread.setDaemon(true);
stderrThread.start();
stdoutThread.start();
//...

?

0

All Articles