Xmx ignored when used in Runtime.exec

The Xmx and Xms is ignored in this code:

Runtime.getRuntime().exec(new String[]{"java", "-jar", "-Xms1024m", "-Xmx1200m", "\"data.jar\""});

or

Runtime.getRuntime().exec("java -jar -Xms1024m -Xmx1200m \"data.jar\"");

I get out of memory error.

But when I run the very same code from a batch file, it works fine:

java -jar -Xms1024m -Xmx1200m "data.jar"

Am I forgetting something? Why is the exec ignoring the Xmx and Xms arguments?

I’d shuffle the parameters to make -jar and the jar-path arguments consecutive. As to why it works from the commandline… you’re probably launching a totally different jvm version (pass -version to verify) .

Well that did not work :frowning:

java -jar \"data.jar\" -Xms1024m -Xmx1200m

I have no other version than java7 installed so it cant be that command line is launching a different version.

What is you’re doing that requires a 1.2GB heap? Seems suspiciously high.
There is another parameter that governs when you get OOMEs as well, which is the amount of heap reserved for native byte buffers. Are you running in to that?

Cas :slight_smile:

Note that any parameters after the jar name will be command line arguments to your program, not to the JVM. Move the -Xms/-Xmx to be before the “-jar jarName” parameters.


java -Xms1024m -Xmx1200m -jar "data.jar"

Well, that did not work either. This small issue is getting frustrating! Tbh, I dont think its the arguments that is the problem, I think its the exec method from Runtime that is buggy.
The reason I say that is because the very same code work its launched from a batch file.

I tried the exec method from apache commons, still the same error though.

Attempt to pass a bogus value eg. -Xmx1k. It should fail.

Cas :slight_smile:

Never put the blame on code that is not yours when you encounter a bug, that is very naive thinking. Always test for every single possibility.