まくまくAndroidノート
LowMemoryKiller によるプロセス kill の優先順位の仕組み
2011-09-07

Android 独自の仕組みである Low Memory Killer は、空きメモリが一定以下になると、アプリプロセスを自動的に kill します。 実体は下記のカーネルに含まれているネイティブプログラムです。

• kernel/drivers/staging/android/lowmemorykiller.c

どのプロセスが kill されるかは、プロセスごとに設定された adj の値によって決まります。 この値が小さい方が優先度の高いプロセスであることを示し、Low Memory Killer によって殺されにくくなります。 メモリサイズと、adj の関連は、init.rc の中で以下のような感じで設定されます。

# Write value must be consistent with the above properties.
write /sys/module/lowmemorykiller/parameters/minfree 2048,4096,8192,8192,16384,20000,25000,30000,35000,40000


ActivityManagerService 内変数 System property Default Description
CORE_SERVER_ADJ   -12 This is a process running a core server, such as telephony. Definitely don’t want to kill it, but doing so is not completely fatal.
PERCEPTIBLE_APP_ADJ ro.PERCEPTIBLE_APP_ADJ 2 This is a process only hosting components that are perceptible to the user, and we really want to avoid killing them, but they are not immediately visible. An example is background music playback.
HEAVY_WEIGHT_APP_ADJ ro.HEAVY_WEIGHT_APP_ADJ 3 This is a process with a heavy-weight application. It is in the background, but we want to try to avoid killing it.
SECONDARY_SERVER_ADJ ro.SECONDARY_SERVER_ADJ 4 This is a process holding a secondary server – killing tit will not have much of an impact as far as the user is concerned.
BACKUP_APP_ADJ ro.BACKUP_APP_ADJ 5 This is a process currently hosting a backup operation. Killing it is not entirely fatal but is generally a bad idea.
HOME_APP_ADJ ro.HOME_APP_ADJ 6 This is a process holding the home application – we want to try avoiding killing it, even if it would normally be in the background, because the user interacts with it so much.
HIDDEN_APP_MIN_ADJ ro.HIDDEN_APP_MIN_ADJ 7 This is a process only hosting activities that are not visible, so it can be killed without any disruption.
EMPTY_APP_ADJ ro.EMPTY_APP_ADJ 15 This is a process without anything currently running in it. Definitely the first to go!

プログラム内で参照する adj に関するシステムプロパティは、init.rc で以下のような感じで設定されています。

on boot
...
...


Log.i("HOGE", "[" + i + "] " + app.processName + "(pid=" + app.pid + ", uid=" + app.info.uid + ")");
", hidden=" + (app.hidden ? 1 : 0) + ", keeping=" + (app.keeping ? 1 : 0));


出力結果

08-30 10:07:30.023   433   451 I HOGE    : [25] com.android.inputmethod.latin(pid=553, uid=10046)