title: Java Runtime.getRuntime().exec() alternatives
body: <p>I have a collection of webapps that are running under tomcat.  Tomcat is configured to have as much as 2 GB of memory using the -Xmx argument.</p>
  
  <p>Many of the webapps need to perform a task that ends up making use of the following code:</p>
  
  <pre><code>Runtime runtime = Runtime.getRuntime();
  Process process = runtime.exec(command);
  process.waitFor();
  ...
  </code></pre>
  
  <p>The issue we are having is related to the way that this "child-process" is getting created on Linux (Redhat 4.4 and Centos 5.4).</p>
  
  <p>It's my understanding that an amount of memory equal to the amount tomcat is using needs to be free in the pool of physical (non-swap) system memory initially for this child process to be created.  When we don't have enough free physical memory, we are getting this:</p>
  
  <pre><code>    java.io.IOException: error=12, Cannot allocate memory
       at java.lang.UNIXProcess.&lt;init&gt;(UNIXProcess.java:148)
       at java.lang.ProcessImpl.start(ProcessImpl.java:65)
       at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
       ... 28 more
  </code></pre>
  
  <p>My questions are:</p>
  
  <p>1)  <strong>Is it possible to remove the requirement for an amount of memory equal to the parent process being free in the physical memory?</strong>  I'm looking for an answer that allows me to specify how much memory the child process gets or to allow java on linux to access swap memory.</p>
  
  <p>2)  <strong>What are the alternatives to Runtime.getRuntime().exec() if no solution to #1 exists?</strong>  I could only think of two, neither of which is very desirable.  JNI (very un-desirable) or rewriting the program we are calling in java and making it it's own process that the webapp communicates with somehow.  There has to be others.</p>
  
  <p>3)  <strong>Is there another side to this problem that I'm not seeing that could potentially fix it?</strong>  Lowering the amount of memory used by tomcat is not an option.  Increasing the memory on the server is always an option, but seems like more a band-aid.</p>
  
  <p>Servers are running java 6.</p>
  
  <p>EDIT:  I should specify that I'm not looking for a tomcat specific fix.  This problem can be seen with any of the java applications we have running on the webserver (there are multiple).  I simply used tomcat as an example because it will most likely have the most memory allocated to it and it's where we actually saw the error the first time.  It is a reproducible error.</p>
  
  <p>EDIT:  In the end, we solved this problem by re-writing what the system call was doing in java.  I feel that we were pretty lucky being able to do this without making additional system calls.  Not all processes will be able to do this, so I would still love to see an actual solution to this.</p>