如何解决“java.io.IOException:error=12,无法分配内存"调用运行时#exec()? [英] How to solve "java.io.IOException: error=12, Cannot allocate memory" calling Runtime#exec()?

查看:77
本文介绍了如何解决“java.io.IOException:error=12,无法分配内存"调用运行时#exec()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的系统上,我无法运行启动进程的简单 Java 应用程序.我不知道如何解决.

On my system I can't run a simple Java application that start a process. I don't know how to solve.

你能给我一些提示如何解决吗?

Could you give me some hints how to solve?

程序是:

[root@newton sisma-acquirer]# cat prova.java
import java.io.IOException;

public class prova {

   public static void main(String[] args) throws IOException {
        Runtime.getRuntime().exec("ls");
    }

}

结果是:

[root@newton sisma-acquirer]# javac prova.java && java -cp . prova
Exception in thread "main" java.io.IOException: Cannot run program "ls": 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)
        at prova.main(prova.java:6)
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

系统配置:

[root@newton sisma-acquirer]# java -version
java version "1.6.0_0"
OpenJDK Runtime Environment (IcedTea6 1.5) (fedora-18.b16.fc10-i386)
OpenJDK Client VM (build 14.0-b15, mixed mode)
[root@newton sisma-acquirer]# cat /etc/fedora-release
Fedora release 10 (Cambridge)

解决方案这解决了我的问题,我不知道为什么:

Solution This solves my problem, I don't know exactly why:

echo 0 >/proc/sys/vm/overcommit_memory

echo 0 > /proc/sys/vm/overcommit_memory

为能够解释的人点赞:)

Up-votes for who is able to explain :)

附加信息,顶部输出:

top - 13:35:38 up 40 min,  2 users,  load average: 0.43, 0.19, 0.12
Tasks: 129 total,   1 running, 128 sleeping,   0 stopped,   0 zombie
Cpu(s):  1.5%us,  0.5%sy,  0.0%ni, 94.8%id,  3.2%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1033456k total,   587672k used,   445784k free,    51672k buffers
Swap:  2031608k total,        0k used,  2031608k free,   188108k cached

附加信息,免费输出:

[root@newton sisma-acquirer]# free
             total       used       free     shared    buffers     cached
Mem:       1033456     588548     444908          0      51704     188292
-/+ buffers/cache:     348552     684904
Swap:      2031608          0    2031608

推荐答案

您机器的内存配置文件是什么?例如如果你运行 top,你有多少空闲内存?

What's the memory profile of your machine ? e.g. if you run top, how much free memory do you have ?

我怀疑 UnixProcess 执行了 fork() 并且它根本没有从操作系统获得足够的内存(如果内存可用,它会 fork() 复制进程,然后 exec() 在新的内存进程中运行 ls,但它没有达到那个程度)

I suspect UnixProcess performs a fork() and it's simply not getting enough memory from the OS (if memory serves, it'll fork() to duplicate the process and then exec() to run the ls in the new memory process, and it's not getting as far as that)

重新.您的过量使用解决方案,它允许过量使用系统内存,可能允许进程分配(但不使用)比实际可用内存更多的内存.所以我猜 fork() 复制了 Java 进程内存,如下面的评论中所讨论的.当然,您不使用内存,因为ls"替换了重复的 Java 进程.

Re. your overcommit solution, it permits overcommitting of system memory, possibly allowing processes to allocate (but not use) more memory than is actually available. So I guess that the fork() duplicates the Java process memory as discussed in the comments below. Of course you don't use the memory since the 'ls' replaces the duplicate Java process.

这篇关于如何解决“java.io.IOException:error=12,无法分配内存"调用运行时#exec()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆