博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
执行Runtime.exec异常: error=12,Cannot allocate memory
阅读量:6844 次
发布时间:2019-06-26

本文共 3341 字,大约阅读时间需要 11 分钟。

hot3.png

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

 ,  ,  ,  ,  ,  ,  , 

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.

 

 

 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.

转载于:https://my.oschina.net/heguangdong/blog/40007

你可能感兴趣的文章
mahout所实现的算法
查看>>
MFC中的CListCtrl网格控件添加行
查看>>
orzdba安装与使用
查看>>
LVS的三种负载均衡技术,八大负载调度算法
查看>>
linux 常用小命令
查看>>
AIX系列------ISO挂载
查看>>
重启citrix服务器,无法打开发布的程序
查看>>
按照文件名中包含的版本号信息对文件名列表进行排序
查看>>
jQuery学习笔记3:过滤器
查看>>
第四课:单用户及救援模式(一)
查看>>
zabbix企业应用:利用自动发现监控IIS站点
查看>>
table 去掉 td之间间距
查看>>
根据status信息对MySQL服务器进行优化-1
查看>>
sui picker,datetimepicker,citypicker代码整理
查看>>
Redis基础教程第2节 Redis和NoSql 介绍与应用场景
查看>>
CentOS6.6下设置grub密码方法
查看>>
Linux下DHCP服务器配置
查看>>
创建数据库恢复
查看>>
一步一步教你使用AgileEAS.NET基础类库进行应用开发-基础篇-使用UDA操纵SQL语句...
查看>>
VS2010与IIS Express
查看>>