今天在一台机器上查看java进行时,运行jps -m时报如下错误:

Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.
但其实服务器的内存是充足的:
free -m
             total       used       free     shared    buffers     cached
Mem:         64412      27418      36994          0        174      13226
-/+ buffers/cache:      14018      50394
Swap:            0          0          0
这其实是由于一个系统的参数导致:
vm.overcommit_memory
这个可以用来控制对应用分配内存的限制,默认是0,关于这个参数
0 — The default setting. The kernel performs heuristic memory overcommit
handling by estimating the amount of memory available and failing requests
that are blatantly invalid. Unfortunately, since memory is allocated using
a heuristic rather than a precise algorithm, this setting can sometimes allow
available memory on the system to be overloaded.
1 — The kernel performs no memory overcommit handling. Under this setting,
the potential for memory overload is increased, but so is performance for
memory-intensive tasks.
2 — The kernel denies requests for memory equal to or larger than the sum of
total available swap and the percentage of physical RAM specified in overcommit_ratio.
This setting is best if you want a lesser risk of memory overcommitment.

可以看出2是比较严格的限制,1是最宽松的限制,对于不同的应用对这个参数的要求页不一样。

比如对于greenplum来说,是不允许使用swap的,所以参数要设置为2.
而对于redis应用,因为在bgsave的时候,需要2倍的使用内存,所以建议设置为1.
java的应用设置为0或1都是可以的。