无法为对象堆保留足够的空间

时间:2014-02-04 16:40:02

标签: java memory-limit

我用java来达到一个奇怪的内存限制。下面我用不同的堆大小执行“java -version”。一旦我将堆设置为超过30M的任何值,java退出时会出错,抱怨没有空闲内存。

# /usr/java/default/bin/java -Xmx30m -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

# /usr/java/default/bin/java -Xmx40m -version
#
# There is insufficient memory for the Java Runtime Environment to continue.
# pthread_getattr_np
# An error report file with more information is saved as:
# /root/hs_err_pid6689.log

# /usr/java/default/bin/java -Xmx50m -version
Error occurred during initialization of VM
java.lang.OutOfMemoryError: unable to create new native thread

# /usr/java/default/bin/java -Xmx60m -version
#
# There is insufficient memory for the Java Runtime Environment to continue.
# pthread_getattr_np
# An error report file with more information is saved as:
# /root/hs_err_pid6703.log

根据“免费”,我有很多免费的内存:

# free -m
             total       used       free     shared    buffers     cached
Mem:          2909       1051       1858          0         17        618
-/+ buffers/cache:        415       2494
Swap:          511          0        511

但从我看来,这显然是内存限制问题。当我杀死一些后台进程时,我可以为“java -version”命令分配更多堆。

有谁知道发生了什么事?

1 个答案:

答案 0 :(得分:1)

我回答了我自己的问题(以防有人碰到类似的问题)。

在我的系统上,我在/etc/sysctl.conf

中有这个
vm.overcommit_memory = 2

将其恢复为默认值(0)已解决问题:

vm.overcommit_memory = 0