如何在 linux 中禁用 oom killer?

How to disable the oom killer in linux?

我当前的配置是:

> cat /proc/sys/vm/panic_on_oom
0
> cat /proc/sys/vm/oom_kill_allocating_task
0
> cat /proc/sys/vm/overcommit_memory
1

但是当我 运行 一个任务时,它无论如何都被杀死了。

> ./test/mem.sh
Killed
> dmesg | tail -2
[24281.788131] Memory cgroup out of memory: Kill process 10565 (bash) score 1001 or sacrifice child
[24281.788133] Killed process 10565 (bash) total-vm:12601088kB, anon-rss:5242544kB, file-rss:64kB

更新

我的任务习惯于科学计算,需要很多记忆,看来overcommit_memory=1可能是最好的选择。

更新 2

实际上,我正在做一个数据分析项目,它的内存消耗超过 16G,但我被要求将它们限制在大约 5G 以内。通过程序本身的优化可能无法实现这个需求,因为该项目使用了很多子命令,而且大多数子命令不包含Java中的XmsXmx等选项。

更新 3

My project should be an overcommited system. Exacetly as what a3f 说,当内存分配失败时,我的应用程序似乎更喜欢 xmalloc 崩溃。

> cat /proc/sys/vm/overcommit_memory
2
> ./test/mem.sh
./test/mem.sh: xmalloc: .././subst.c:3542: cannot allocate 1073741825 bytes (4295237632 bytes allocated)

我不想投降,虽然这么多可怕的考验让我疲惫不堪。 所以请告诉我一条通向光明的路; )

OOM 杀手不会消失。如果没有记忆,就得有人付出代价。您可以做的是设置内存分配失败的限制。 这正是将 vm.overcommit_memory 设置为 2 的目的。

来自the docs

The Linux kernel supports the following overcommit handling modes

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.

通常情况下,内核会很乐意分配虚拟内存(overcommit)。仅当您引用页面时,内核才必须将页面映射到真实的物理框架。如果它不能为该请求提供服务,则需要由 OOM 杀手杀死一个进程才能使 space。

禁用过度使用意味着例如如果内核无法提交请求的内存量,malloc(3) 将 return NULL。这使得事情更容易预测,尽管是有限的(许多应用程序分配的比他们需要的更多)。

oom_adj 的可能值范围从 -17 到 +15。分数越高,关联的进程越有可能被 OOM-killer 杀死。如果oom_adj设置为-17,进程不考虑OOM-killing。

但是,增加 ram 是更好的选择,如果增加 ram 是不可能的,那么添加交换内存。

要增加交换内存,请尝试 this link