参考
frameworks\base\services\core\java\com\android\server\am\ProcessList.java
static final int VISIBLE_APP_ADJ = 100;
static final int VISIBLE_APP_LAYER_MAX = PERCEPTIBLE_APP_ADJ - VISIBLE_APP_ADJ - 1;
// This is the process running the current foreground app. We'd really
// rather not kill it!
static final int FOREGROUND_APP_ADJ = 0;
// This is a process that the system or a persistent process has bound to,
// and indicated it is important.
static final int PERSISTENT_SERVICE_ADJ = -700;
// This is a system persistent process, such as telephony. Definitely
// don't want to kill it, but doing so is not completely fatal.
static final int PERSISTENT_PROC_ADJ = -800;
定义应用不同情况下的 adj 。
private int[] mOomAdj;
private int[] mOomMinFree;
分别对应adj 与 内存,是对应的
if (mTotalMemMb <= LOW_LEVEL_SIZE) {
mOomAdj = new int[] { FOREGROUND_APP_ADJ, VISIBLE_APP_ADJ, PERCEPTIBLE_APP_ADJ, BACKUP_APP_ADJ, CACHED_APP_MIN_ADJ, CACHED_APP_MAX_ADJ };
mOomMinFree = new int[] { 24576, // 24 * 1024 (ADJ 0 -> 24MB) 31744, // 31 * 1024 (ADJ 1 -> 31MB) 38912, // 38 * 1024 (ADJ 2 -> 38MB) 49152, // 48 * 1024 (ADJ 3 -> 48MB) 122880, // 120 * 1024 (ADJ 9 -> 120MB) (based on AMR performance test) 122880 // 120 * 1024 (ADJ 15 -> 120MB) (based on AMR performance test) };
一组默认的对应,第一个元素对应:当内存小于24MB 时,对应adj 大于 FOREGROUND_APP_ADJ 应用均可能被杀掉。
最后一个元素对应:当内存小于120MB时,对应adj 大于 CACHED_APP_MAX_ADJ 的应用会被杀掉。
这里adj 是 -16 ~ 15 似乎跟 mOomAdj 对应不上,这里其实是同一个意思,只是有一个换算。
kernel-3.18/include/uapi/linux/oom.h
#ifndef _UAPI__INCLUDE_LINUX_OOM_H
#define _UAPI__INCLUDE_LINUX_OOM_H
/*
* /proc/<pid>/oom_score_adj set to OOM_SCORE_ADJ_MIN disables oom killing for
* pid.
*/
#define OOM_SCORE_ADJ_MIN (-1000)
#define OOM_SCORE_ADJ_MAX 1000
/*
* /proc/<pid>/oom_adj set to -17 protects from the oom killer for legacy
* purposes.
*/
#define OOM_DISABLE (-17)
/* inclusive */
#define OOM_ADJUST_MIN (-16)
#define OOM_ADJUST_MAX 15
#endif /* _UAPI__INCLUDE_LINUX_OOM_H */
kernel-3.18\drivers\staging\android\lowmemorykiller.c
static short lowmem_oom_adj_to_oom_score_adj(short oom_adj) {
if (oom_adj == OOM_ADJUST_MAX) return OOM_SCORE_ADJ_MAX; else return (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE; }
这个函数说明了
adj 是 -16 ~ 15 跟 mOomAdj 换算关系。
/sys/module/lowmemorykiller/parameters/adj
/sys/module/lowmemorykiller/parameters/minfree
对应的是 mOomAdj 与 mOomMinFree转换成页之后的值
/proc/1052{$pid}/下
oom_adj oom_score oom_score_adj
oom_adj 与oom_score_adj 是 adj -16 ~ 15 跟 mOomAdj 对应一样的
system/core/lmkd/lmkd.c
https://www.jianshu.com/p/b5a8a1d09712
kernel_4.19
system/memory/lmkd/
README.md
Android Low Memory Killer Daemon
Introduction
Android Low Memory Killer Daemon (lmkd) is a process monitoring memory state of a running Android system and reacting to high memory pressure by killing the least essential process(es) to keep system performing at acceptable levels.
Background
Historically on Android systems memory monitoring and killing of non-essential processes was handled by a kernel lowmemorykiller driver. Since Linux Kernel 4.12 the lowmemorykiller driver has been removed and instead userspace lmkd daemon performs these tasks.
Android Properties
lmkd can be configured on a particular system using the following Android properties:
ro.config.low_ram: choose between low-memory vs high-performance device. Default = false.
ro.lmk.use_minfree_levels: use free memory and file cache thresholds for making decisions when to kill. This mode works the same way kernel lowmemorykiller driver used to work. Default = false
ro.lmk.low: min oom_adj score for processes eligible to be killed at low vmpressure level. Default = 1001 (disabled)
ro.lmk.medium: min oom_adj score for processes eligible to be killed at medium vmpressure level. Default = 800 (non-essential processes)
ro.lmk.critical: min oom_adj score for processes eligible to be killed at critical vmpressure level. Default = 0 (all processes)
ro.lmk.critical_upgrade: enables upgrade to critical level. Default = false
ro.lmk.upgrade_pressure: max mem_pressure at which level will be upgraded because system is swapping too much. Default = 100 (disabled)
ro.lmk.downgrade_pressure: min mem_pressure at which vmpressure event will be ignored because enough free memory is still available. Default = 100 (disabled)
ro.lmk.kill_heaviest_task: kill heaviest eligible task (best decision) vs. any eligible task (fast decision). Default = false
ro.lmk.kill_timeout_ms: duration in ms after a kill when no additional kill will be done. Default = 0 (disabled)
ro.lmk.debug: enable lmkd debug logs, Default = false
ro.lmk.swap_free_low_percentage: level of free swap as a percentage of the total swap space used as a threshold to consider the system as swap space starved. Default for low-RAM devices = 10, for high-end devices = 20
ro.lmk.thrashing_limit: number of workingset refaults as a percentage of the file-backed pagecache size used as a threshold to consider system thrashing its pagecache. Default for low-RAM devices = 30, for high-end devices = 100
ro.lmk.thrashing_limit_decay: thrashing threshold decay expressed as a percentage of the original threshold used to lower the threshold when system does not recover even after a kill. Default for low-RAM devices = 50, for high-end devices = 10
ro.lmk.psi_partial_stall_ms: partial PSI stall threshold in milliseconds for triggering low memory notification. Default for low-RAM devices = 200, for high-end devices = 70
ro.lmk.psi_complete_stall_ms: complete PSI stall threshold in milliseconds for triggering critical memory notification. Default = 700
lmkd will set the following Android properties according to current system configurations:
sys.lmk.minfree_levels: minfree:oom_adj_score pairs, delimited by comma
sys.lmk.reportkills: whether or not it supports reporting process kills to clients. Test app should check this property before testing low memory kill notification. Default will be unset.
system/memory/lmkd/lmkd.cpp