执行Runtime.exec异常: error=12,Cannot allocate memory
转:http://blog.csdn.net/chifengxin/article/details/6573134
Exception Trace:
In the Linux circumstance, when the program executes till this place:
Process p = Runtime.getRuntime().exec(cmdArr);
it throws an exception like this:
java.io.IOException: Cannot run program "/opt/X.sh": java.io.IOException: error=12, Cannot allocate memory
???????????????????????????????????Caused by: java.io.IOException: java.io.IOException: error=12, Cannot allocate memory
????????????????????????????These are the status and environment for it:
Top:
?16405 root?????Free:
????????????Mem:??????-/+ buffers/cache:???Swap:?????As shown before, the program has hold about 87% memory. When the memory usage is low, there is no exception throwed.?
?The Official Explains:
Bug ID:5049299
Votes
45
Synopsis
(process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion
Category
java:classes_lang
Reported Against
5.0 , b06 , 1.4.2_04
Release Fixed
?State
6-Fix Understood,Priority:
2-High
Related Bugs
6381152Submit Date
18-MAY-2004
Description
If you run a "small" program (e.g., a Perl script) from a "big" Java process on
a machine with "moderate" free swap space (but not as much as the big Java
process), then Runtime.exec() fails.
?
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5049299
?
?Cause Detail:
This issue dues to the operating mechanism of “Runtime.exec” in Java.
In Java program, “ProcessBuilder.start” and “Runtime.exec” use fork() on *NIX system, which allocates the child process the same amount of memory as the parent process. This will double the used memory for a short time. So when the Java main process has used over 50% memory, it will absolutely never launch a child process using “Runtime.exec” successful, even the process needs almost no memory.
Solutions:
There are three workable solutions:
1. The middleware of Tanuki may solve this problem. The question is, it is complicated and also expensive.
???2. Separate the process using “Runtime.exec” from the main process into a new java process. So when the “Runtime.exec” is called ,it will only double the memory of the new process, using almost no memory.
which means:
??????This will surely increase the complexity and maintenance of the system.
?
3. Update JDK1.6 to JDK1.7
This bug is fixed in JDK1.7 ,using new invoking mechanism of external program.
(Pipes be tested in JDK snapshot release: build 1.7.0-b147,passed with no exceptions; it also reduced the memory usage for about 20%)
This will need no modification for current source code.
?
?
Quote:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5049299
http://hi.baidu.com/lewutian/blog/item/3a60a2fa553f6115a9d31172.html
http://www.cnblogs.com/happyy/archive/2010/11/28/1890111.html
http://wrapper.tanukisoftware.com/doc/english/child-exec.html