目录
- overcommit_memory
- Overcommit Accounting
- CommitLimit and Committed_AS
- overcommit_kbytes or overcommit_ratio
overcommit_memory: Documentation for /proc/sys/vm/ — The Linux Kernel documentation
overcommit_memory这个vm参数用于控制malloc、mmap这些用于在进程虚拟地址空间申请虚拟内存的系统调用,例如一次性申请大量的虚拟地址是否允许成功,就取决于这个参数的配置。
参数位置:/proc/sys/vm/overcommit_memory(运行时配置内核参数,那你绝对要关注sysctl指令和/proc/sys目录)
overcommit_memory
This value contains a flag that enables memory overcommitment.
When this flag is 0, the kernel attempts to estimate the amount of free memory left when userspace requests more memory.
When this flag is 1, the kernel pretends there is always enough memory until it actually runs out.
When this flag is 2, the kernel uses a “never overcommit” policy that attempts to prevent any overcommit of memory. Note that user_reserve_kbytes affects this policy.
This feature can be very useful because there are a lot of programs that malloc() huge amounts of memory “just-in-case” and don’t use much of it.
The default value is 0.
See Overcommit Accounting and mm/util.c::__vm_enough_memory() for more information.
更多详情,需要去转看Overcommit Accounting。
补充:默认情况下,当malloc一次申请的虚拟内存大于128K则会转成mmap,此时不会在堆内存中通过brk指针申请虚拟内存,而是通过mmap在内存映射区划分内存。
1.先看malloc API文档中的MMAP_THRESHOLD:
Normally, malloc() allocates memory from the heap, and adjusts the size of the heap as required, using sbrk(2). When allocating blocks of memory larger than MMAP_THRESHOLD bytes, the glibc malloc() implementation allocates the memory as a private anonymous mapping using mmap(2). MMAP_THRESHOLD is 128 kB by default, but is adjustable using