3

I am launching an application from Java using the getRuntime().exec(...) method, everything works perfectly except for one thing: the program (a 3d robot simulator in my case) runs slower compared to when I run it using a terminal window, that is: when running a task the simulator can take 10 more seconds when launched from Java compared to running the same task when launched from a terminal.

What could be causing this behaviour?

I have tried increasing the memory available to the JVM to 4 GB using -Xmx and -Xms but with the same result, I am using mpj-express to open and manage various simulators at the same time and the code I am using to launch each simulator is:

try{
    Runtime rt = Runtime.getRuntime();
    rt.exec("/home/.../sim.sh");
    Thread.sleep(3000);
} catch(Exception e) {
    System.out.println(e.toString());
    e.printStackTrace();
}

I am running Linux 14.04 64 bits on a four core Intel Core i5 with 8 GB of RAM and my Java version is:

java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

Thanks

rodr
  • 31
  • 3
  • 2
    It runs in a different process, so Java memory settings have nothing to do with it. Look at your system CPU load, I/O load, memory usage, etc. – Thomas Oct 22 '14 at 20:30
  • 2
    Two thoughts: Do the additional 10 seconds include the start-up of the application? And have you tried to *reduce* the Java heap size? Maybe the application being started has to page out part of the Java process to be able to start. – Thomas Stets Oct 22 '14 at 20:31
  • @ThomasStets At the beginning I thought that it had to do with the start-up of the application, that is why I am waiting 3 seconds for the program to start, but the additional 10 seconds occur when running simulations after the simulator has started. I have also read that not handling the stdout of the program can affect it but I don't really have any usage for it and don't know how to discard it. – rodr Oct 23 '14 at 09:09
  • 1
    Does your application generate a lot of output? runtime.exec() returns a Process object, which will give you an InputStream. You can read the output of your program from that stream. Between the time your program generates the output, end the time you read it the VM keeps it in memory, possible resulting in low memory conditions and lots of garbage collection. – Thomas Stets Oct 23 '14 at 11:16
  • @ThomasStets The application generates about 10 lines of output, I have also noticed that when the application is launched from java, the java processes use a lot more CPU than when the application is launched externally. – rodr Oct 23 '14 at 14:10
  • 2
    See also [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process. – Andrew Thompson Oct 23 '14 at 23:42
  • @AndrewThompson Thanks for the link. The process I had was that I was using `process.waitFor()` and the output buffer was getting filled and dead-locking. – Max Dec 21 '15 at 18:41
  • @Max *"Thanks for the link."* An upvote on the comment would be more useful (for making the link obvious to later visitors). ;) – Andrew Thompson Dec 21 '15 at 18:44
  • You are talking about an external program so you need to tell us what platform you are on. Windows version? Linux distribution/version? MacOS? That said, likely the Java program is monopolizing some system resource e.g. IO, memory, CPU, or alternatively program is running with different command line or environment variables in each case. – Ben Sep 12 '16 at 09:40

0 Answers0