执行Runtime.exec异常: error=12,Cannot allocate memory

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

        at java.lang.ProcessBuilder.start(ProcessBuilder.java:474)

        at java.lang.Runtime.exec(Runtime.java:610)

        at java.lang.Runtime.exec(Runtime.java:448)

        at java.lang.Runtime.exec(Runtime.java:345)

        ...

Caused by: java.io.IOException: java.io.IOException: error=12, Cannot allocate memory

        at java.lang.UNIXProcess.<init>(UNIXProcess.java:164)

        at java.lang.ProcessImpl.start(ProcessImpl.java:81)

        at java.lang.ProcessBuilder.start(ProcessBuilder.java:467)

        ... 4 more

These are the status and environment for it:

Top

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND

16405 root      19   0 6484m 5.1g  10m S  101 86.7   4:09.11 java

Free

             total       used       free     shared    buffers     cached

Mem:       6108084    5616532     491552          0       2244     185100

-/+ buffers/cache:    5429188     678896

Swap:      2104472      12584    2091888

As shown before, the program has hold about 87% memory. When the memory usage is low, there is no exception throwed. It could be why java program fails to process “Runtime.exec()”.

 

 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, request for enhancement

Priority:

2-High

Related Bugs

6381152 , 6605835 , 6728616 , 6745199 , 6845504 , 6850720 , 4343908 , 4391042

Submit 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.

    See-  http://wrapper.tanukisoftware.com/doc/english/child-exec.html

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:

    a. When start/stop the main process, the “Runtime.exec” process should be started/stopped at the same time.

    b. Add an independent socket in the main process to communicate with the new process. The exec command will be delivered to the new process to execute.

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
error='Cannot allocate memory' (errno=12)是一个常见的错误,表示JVM无法分配内存。这通常是由于系统的物理内存不足或交换空间不足导致的。当JVM尝试分配一定数量的内存时,操作系统无法提供足够的空闲内存,从而导致JVM崩溃。 解决这个问题的方法有几种。首先,您可以尝试释放一些不必要的进程或内存资源,以腾出更多的可用内存。您可以通过关闭一些不使用的程序或服务来实现这一点。这样,JVM将有更多的内存可用于分配。 另一种解决方法是修改JVM的默认配置,减少其启动时所需的内存。您可以通过调整JVM的堆大小、栈大小等参数来实现这一点。具体的方法取决于您使用的是哪个JVM版本和配置工具。您可以查阅相关文档或参考其他资源来了解如何修改JVM的配置。 此外,您还可以考虑升级系统的物理内存或增加交换空间的大小,以提供更多的可用内存给JVM使用。这可能需要一些硬件或系统配置方面的操作,具体取决于您的环境和需求。 总结来说,error='Cannot allocate memory' (errno=12)错误表示JVM无法分配足够的内存。您可以通过释放不必要的进程或资源、调整JVM的配置或增加系统的物理内存和交换空间来解决这个问题。具体的解决方法取决于您的环境和需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值