前一篇博客进程管理相关流程分析(1)里,
我们介绍了AMS中updateLruProcessLocked函数相关的流程。
updateLruProcessLocked只是按照进程中运行的组件,粗略地定义了不同进程的优先级。
实际上,Android根据进程的oom_adj进行了更加细致的进程分类,
而AMS中的updateOomAdjLocked函数,就是用于更新进程的oom_adj值。
本篇博客中,我们来看看AMS中updateOomAdjLocked相关的流程。
一、ProcessList.java中的oom_adj
进程在不同场景下的oom_adj值,定义于ProcessList.java中。
考虑到updateOomAdjLocked函数极其的复杂,我们有必要先了解一下不同oom_adj的含义。
final class ProcessList {
..............
// OOM adjustments for processes in various states:
// Uninitialized value for any major or minor adj fields
// 未初始化时,定义为-10000
static final int INVALID_ADJ = -10000;
..............
// Adjustment used in certain places where we don't know it yet.
// (Generally this is something that is going to be cached, but we
// don't know the exact value in the cached range to assign yet.)
// 如注释所述,过渡态的值
static final int UNKNOWN_ADJ = 1001;
// This is a process only hosting activities that are not visible,
// so it can be killed without any disruption.
// 从注释来看,这里应该是仅持有Activity的background进程对应的值,最小为900,最大为906
// 实际上empty进程和background进程均会使用这些值
static final int CACHED_APP_MAX_ADJ = 906;
static final int CACHED_APP_MIN_ADJ = 900;
// The B list of SERVICE_ADJ -- these are the old and decrepit
// services that aren't as shiny and interesting as the ones in the A list.
// 这里应该是对应持有老旧服务的background进程
static final int SERVICE_B_ADJ = 800;
// This is the process of the previous application that the user was in.
// This process is kept above other things, because it is very common to
// switch back to the previous app.
// 当前页面对应进程的前一个进程
// 例如,从A进程的Activity跳转到B进程的Activity
// 此时B进程就是当前进程,A进程就是previous进程
// 由于用户可能在A、B Activity之间切换,因此为previous进程赋予一个单独的oom_adj
// 本质上来说,这仍是background进程
static final int PREVIOUS_APP_ADJ = 700;
// 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.
// 用户经常会和home进程交互,因此单独为home进程赋予一个oom_adj
// 一般情况下,home进程也可以归属到background进程
static final int HOME_APP_ADJ = 600;
// This is a process holding an application service -- killing it will not
// have much of an impact as far as the user is concerned.
// service进程
static final int SERVICE_ADJ = 500;
// This is a process with a heavy-weight application. It is in the
// background, but we want to try to avoid killing it. Value set in
// system/rootdir/init.rc on startup.
// 对于heavy_weight进程,虽是后台进程,但重要性甚至大于service进程
// 应该是该类型的进程,重新启动的成本太高了,因此尽量避免杀掉吧
// 这是所谓的“尾大不掉”吧
// 需要指出的是,这里的注释太老旧了,Android N里的路径已经是system/core/rootdir/init.rc
// 同时,没有看到与heavy_weight相关的配置,个人觉得注释的意思是:
// 可以按照需要自己配置吧,类似于init进程的写法:
// # Set init and its forked children's oom_adj.
// write /proc/1/oom_score_adj -1000
static final int HEAVY_WEIGHT_APP_ADJ = 400;
// This is a process currently hosting a backup operation. Killing it
// is not entirely fatal but is generally a bad idea.
// 正在执行backup的进程
// 与heavy_weight进程一样,android已经不完全按照进程的组件来划分重要性了
// 还考虑了进程当前正在执行的操作
static final int BACKUP_APP_ADJ = 300;
// 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.
// “perceptible”的意思是:可察觉到
// 从这里来看,其实这种进程已经属于visible进程的范畴了,毕竟用户可以察觉到这种进程的存在
// 不过这种进程属于不包含visible activity,但包含绑定了Activity的service
static final int PERCEPTIBLE_APP_ADJ = 200;
// This is a process only hosting activities that are visible to the
// user, so we'd prefer they don't disappear.
// visible进程, 侧重于包含visible activity
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!
// foreground进程
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.
// 系统进程或常驻进程绑定的service所在的进程
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;
// The system process runs at the default adjustment.
// 系统进程
static final int SYSTEM_ADJ = -900;
// Special code for native processes that are not being managed by the system (so
// don't have an oom adj assigned by the system).
// 框架层无法约束的native进程
static final int NATIVE_ADJ = -1000;
..............
// The minimum number of cached apps we want to be able to keep around,
// without empty apps being able to push them out of memory.
// 从注释来看,min_cached_apps不包含empty进程的数量
static final int MIN_CACHED_APPS = 2;
// The maximum number of cached processes we will keep around before killing them.
// NOTE: this constant is *only* a control to not let us go too crazy with
// keeping around processes on devices with large amounts of RAM. For devices that
// are tighter on RAM, the out of memory killer is responsible for killing background
// processes as RAM is needed, and we should *never* be relying on this limit to
// kill them. Also note that this limit only applies to cached background processes;
// we have no limit on the number of service, visible, foreground, or other such
// processes and the number of those processes does not count against the cached
// process limit.
// 这里的注释需要看一看,这里应该指系统可保留的所有后台和empty进程最大值
// MAX_CACHED_APPS不限制service, visible, foreground, or other such processes的数量
// 仅仅作为参考值,系统并不会真的保留这么多后台进程,毕竟LMK将按需看kill them
static final int MAX_CACHED_APPS = 32;
..............
// The maximum number of empty app processes we will let sit around.
//最多保留16个empty进程
private static final int MAX_EMPTY_APPS = computeEmptyProcessLimit(MAX_CACHED_APPS);
// The number of empty apps at which we don't consider it necessary to do
// memory trimming.
// Android依靠LMK杀死进程来释放内存
// 当empty进程数少于这个门限(8)时,说明LMK已经杀死了一部分empty进程
// 于是AMS主动释放一些应用的内存
static final int TRIM_EMPTY_APPS = MAX_EMPTY_APPS/2;
// The number of cached at which we don't consider it necessary to do
// memory trimming.
// 与TRIM_EMPTY_APPS的用途类似
// 当后台进程少于该门限(5)时,AMS主动释放一些应用的内存
static final int TRIM_CACHED_APPS = (MAX_CACHED_APPS-MAX_EMPTY_APPS)/3;
...................