Linux OOM一二三
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是内核保留空闲内存最小值,作用是保障必要时有足够内存使用。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
2018-09-04 统计UPD丢包工具