malloc的最大大小可以分配吗?

use*_*385 7 c linux-kernel

我的os是arch linux 64位.

free -m结果如下

             total       used       free     shared    buffers     cached
Mem:         32172      31909        262          0        119      13054
-/+ buffers/cache:      18735      13436
Swap:          258        258          0
ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 20
file size               (blocks, -f) unlimited
pending signals                 (-i) 257329
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 257329
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

kenerl选项overcommit_memroy为0

但是当我尝试使用malloc来分配2G字节时,我失败的原因是:无法分配内存?

Lel*_*ran 3

实际上,它可以是任何东西。它以前可能有效,因为可用内存都是连续的,而现在可用内存不是连续的,或者可能是您的内存请求未能通过正在使用的启发式方法。

请参阅此处的链接 - http://linuxtoolkit.blogspot.com/2011/08/tweaking-linux-kernel-overcommit.html - 有关转向过度使用的更多信息。

基本上:

  • 0 = 系统启发式确定是否允许过量使用
  • 1 = 总是过量使用(即使超出地址范围,如果你问我的话,那就太愚蠢了)
  • 2 = 永远不要过度承诺超过某些限制