2


Suppose I'm invoking a command line program through java using following code segment

 p = Runtime.getRuntime().exec(command);
 p.waitFor();

In this case, how much memory will be allocated to command the program invoked by exec? Does it have anything to do with memory allocated for jvm? If so, How can I overcome this limitation?

UPDATE
According to an answer given for this question, How to solve "java.io.IOException: error=12, Cannot allocate memory" calling Runtime#exec()?

Runtime.getRuntime().exec allocates the process with the same amount of memory as the main. If you had you heap set to 1GB and try to exec then it will allocate another 1GB for that process to run.

So is there any way by which, I can have more memory for the process that I'm invoking using java?

What if I run command through command prompt like this?

rt.exec("cmd.exe /c command");

In this case, who will decide memory limit? Command Prompt or Java?

Community
  • 1
  • 1
niyasc
  • 4,440
  • 1
  • 23
  • 50
  • Should be unrelated to JVM memory settings, and it is not limited by Java, either. Only OS-level per-process memory limits (for your user) apply. – Thilo Oct 26 '15 at 09:28
  • @FranMontero I'm asking about memory restriction. If memory allocated to program is limited by memory allotted to jvm, how can I overcome that. – niyasc Oct 26 '15 at 09:28

1 Answers1

1

The javadoc for Runtime.exec states that the command will be started in a separate process. Hence, the command itself will be run in its own process with its own memory and will not use any of the JVM memory.

However of course the method also returns a Process object, which itself will use a bit of the JVM memory, but that will be just a few variables to keep track of the process that was launched, so unless you start tens of thousands of processes in parallel this should not be an issue.

secolive
  • 499
  • 2
  • 7