c - malloc 可以分配的最大大小?

标签 c linux-kernel

我的操作系统是 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 字节时,我失败了,原因是:无法分配内存?

最佳答案

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

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

基本上:

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

关于c - malloc 可以分配的最大大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13339509/

相关文章:

c - 在 Ubuntu 上进行 C 编程的手册页

c - 不知道如何初始化这个局部变量

linux - 为什么在中断中使用 mutex_trylock 不安全?

linux - Linux中,任何用户的组名和其他用户的组名可以相同吗?

c - 初始化和控制语句可能是什么?

c - 如何清除C中数组的所有元素?

C 中的命令行参数

linux-kernel - 如何在 Linux 中分配大的连续内存区域

linux-kernel - ebpf 加载器之间的主要区别

c - 了解用于处理文件描述符的linux内核数据结构