推荐: blog.csdn.net/aquester https://github.com/eyjian https://www.cnblogs.com/aquester http://blog.chinaunix.net/uid/20682147.html
全部博文(595)
分类: C/C++
2019-09-04 17:21:06
Linux开发一般会遇到“/proc/sys/vm/overcommit_memory”,即文件/etc/sysctl.conf中的vm.overcommit_memory,Overcommit的意思如同其字面意思,即进程可申请超出可用内存大小的内存(对进程而言实为虚拟内存,一个进程占用的虚拟内存空间通常比物理空间要大,甚至可能大许多)。overcommit_memory有三种取值(注:overcommit_memory并不控制OOM,是否开启OOM由panic_on_oom控制):
overcommit_memory取值 |
含义 |
0 |
系统默认值。在程序请求分配内存,比如C++程序调用malloc或new时,先检查是否有足够的内存。如果没有足够满足请求的内存,则分配请求失败。 |
1 |
启用Overcommit,即进程可申请超出CommitLimit大小的内存。 |
2 |
关闭Overcommit,即申请的内存大小不能超过CommitLimit。行为和/proc/sys/vm/overcommit_ratio的值相关,/proc/sys/vm/overcommit_ratio的默认值为50。 |
权威参考:
https://www.kernel.org/doc/Documentation/vm/overcommit-accounting
官方原文:
overcommit_memory取值 |
含义 |
0 |
Heuristic overcommit handling. Obvious overcommits of address space are refused. Used for a typical system. It ensures a seriously wild allocation fails while allowing overcommit to reduce swap usage. root is allowed to allocate slightly more memory in this mode. This is the default. |
1 |
Always overcommit. Appropriate for some scientific applications. Classic example is code using sparse arrays and just relying on the virtual memory consisting almost entirely of zero pages. |
2 |
Don't overcommit. The total address space commit for the system is not permitted to exceed swap + a configurable amount (default is 50%) of physical RAM. Depending on the amount you use, in most situations this means a process will not be killed while accessing pages but will receive errors on memory allocation as appropriate.
Useful for applications that want to guarantee their memory allocations will be available in the future without having to initialize every page. |
系统是否行使OOM,由/proc/sys/vm/panic_on_oom的值决定,当/proc/sys/vm/panic_on_oom取值为1时表示关闭OOM,取值0时表示启用OOM。
如果将/proc/sys/vm/oom_kill_allocating_task的值设置为1,则OOM时直接KILL当前正在申请内存的进程,否则OOM根据进程的oom_adj和oom_score来决定。
oom_adj表示进程被OOM KILLER杀死的权重,取值“17~15”,值越大被KILL的概率越高,当进程的oom_adj值为-17时,表示永远不会被OOM KILLER选中。
什么是Overcommit?当已申请的内存(Committed_AS)大小超出CommitLimit值时即为Overcommit,执行命令“cat /proc/meminfo |grep CommitLimit”可查看CommitLimit的当前大小。
CommitLimit为系统当前可以申请的内存总大小,即内存分配上限,当overcommit_memory值为2时,其值等于:SwapTotal + MemTotal * overcommit_ratio。
而Committed_AS,表示所有进程已申请的内存总和。命令“sar -r”输出中的kbcommit对应/proc/meminfo中的Committed_AS,而“%commit”值等于Committed_AS/(MemTotal+SwapTotal)。
如果是大内存机器,可以考虑适当调大/proc/sys/vm/min_free_kbytes的值,但不能太大了,不然容易频繁触发内存回收,min_free_kbytes是内核保留空闲内存最小值,作用是保障必要时有足够内存使用。