前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux OOM一二三

Linux OOM一二三

作者头像
一见
发布2019-09-05 16:18:31
1.5K0
发布2019-09-05 16:18:31
举报
文章被收录于专栏:蓝天蓝天

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是内核保留空闲内存最小值,作用是保障必要时有足够内存使用。

window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档