LoginSignup
18
12

More than 3 years have passed since last update.

【out of memory kill process】OOM Killerさんに殺されたくない

Last updated at Posted at 2019-09-12

はじめに

APIのレスポンスで500エラーが返ってきていました。
調べると/var/log/messageout of memory kill processとログがありました。
プロセスがOOM Killerさんに殺されたせいでエラーが起きていたようです。
私のCentOS環境では、以下コマンドで殺されたプロセスが確認できました。

$ sudo cat /var/log/messages | grep Killed

大事なプロセスが殺されては困るので殺されないようにします。

OOM Killerとは

Linuxでシステムが実メモリーと仮想メモリー空間(スワップ領域)を使い切り、メモリ不足(OOM(Out of Memory))に陥った場合、1つ以上のプロセスを強制終了して空きメモリを確保するLinuxカーネルの機能のことです。

OOM Killerに殺されないようにする

OOM Killerは、各プロセスのoom_score値が高いものを、殺すプロセスとして選択します。
殺される可能性の高いプロセスはdstatで確認できます。
dstatのインストール

$ yum install dstat

殺される可能性の高いプロセスの確認

$ dstat --top-oom

各プロセスの oom_score値は、/proc/[pid]/oom_scoreで確認できます。
次のコマンドを実行すると、まとめて確認できます。

$ grep "" /proc/*/oom_score

oom_score値は、oom_adj値によって調整され、oom_adj値は -17~15の範囲の値をとるようです。
-17は特別な意味を持ち、そのプロセスは OOM Killerの対象から外れます。
E.3.12. /proc/PID - Red Hat Customer Portal

そのため、プロセスが殺されないようにするには、そのプロセスのoom_adj値に-17を指定すれば良いことになります。
殺されたくないプロセスのPIDを指定して以下コマンドを実行します。

$ echo "-17" > /proc/[pid]/oom_adj

追記(2019/09/13)

殺されたくない処理を$ /your/heavy/command arg1 arg2 arg3 &のように起動する場合、以下のようにシェルスクリプトにして実行すると便利です。

non_oomkiller_do.sh
#!/bin/bash
$@ &
echo "-17" > /proc/$!/oom_adj
$ ./non_oomkiller_do.sh /your/heavy/command arg1 arg2 arg3

これによって、コマンド実行時にそのプロセスが殺されないようにできます。

おわりに

これでOOM Killerさんに殺されなくなりました。

参考

https://vogel.at.webry.info/201605/article_1.html
https://qiita.com/konpyu/items/20d1989d1251d805cf3b
https://qiita.com/laikuaut/items/1daa06900ad045d119b4

18
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
12