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

查看:170
本文介绍了如何解决“java.io.IOException:error = 12,无法分配内存”调用Runtime #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)

编辑:Re。你的overcommit解决方案,它允许过度使用系统内存,可能允许进程分配(但不使用)比实际可用内存更多的内存。所以我猜这个 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,无法分配内存”调用Runtime #exec()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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