Why oom-killer kills?!
OOM-killer functionality is controlled by function oom_kill.c[2] and if you check source code it grew from 700+ lines of code to 1100+ lines from 2.6 till 5.4 kernel version. Sadly I was able to find only on this resource[3] and this information dates 2009 year.
In 2 words if Linux free memory is depleting, oom-killer sends SIGKILL to the process with maximum badness score. Badness is defined by oom_kill.c and can be checked in /proc/$pid/oom_score file for each process ID. For example you can see OOM score for top 20 processes:
|
|
Top process details:
|
|
What can we do if we don’t want oom-killer in Linux to kill our process? For example we are running programs A and B in userspace of asterisk user.
Both programs consume almost identical amount of memory but if you can tolerate process B downtime, process A is business critical.
And in case of oom-killer you can’t be sure which process is killed if memory consumption is grown rapidly.
We can raise priority of process A by setting negative oom_score_adj value:
|
|
Commands: Find pagesize of Linux OS
|
|
Find memory size used by the process:
|
|
Links:
- https://www.percona.com/blog/2019/08/02/out-of-memory-killer-or-savior/
- https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/oom_kill.c?id=HEAD&h=master#n172 (fix unreclaimble typo)
- old kernel http://catap.ru/blog/2009/05/03/about-memory-oom-killer/
- about Linux memory management https://www.tldp.org/LDP/tlk/mm/memory.html
- https://sysctl-explorer.net/vm/
- https://sysctl-explorer.net/vm/oom_kill_allocating_task/
- https://sysctl-explorer.net/vm/panic_on_oom/