[笔记]Android进程优先级

/ 0评 / 0

Android进程优先级

在Android中将进程大致可以分为5类,前台进程、可见进程、服务进程、后台进程、空进程。如下图所示

前台进程:指正在与用户进行交互的应用进程,该进程数量较少,是最高优先级进程,系统一般不会终止该进程,而判断为前台进程的因素有以下这些

1、进程中包含处于前台的正与用户交互的activity;

2、进程中包含与前台activity绑定的service;

3、进程中包含调用了startForeground()方法的service;

4、进程中包含正在执行onCreate(), onStart(), 或onDestroy()方法的service;

5、进程中包含正在执行onReceive()方法的BroadcastReceiver.

可视进程:能被用户看到,但不能根据根据用户的动作做出相应的反馈,

1、进程中包含可见但不处于前台进程的activity(如:弹出对话窗时activity处于可见状态,但并不处于前台进程中)

2、该进程有一个与可见/前台的activity绑定数据的service

3、进程有一个startForeground的Service

服务进程:没有可见界面仍在不断的执行任务的进程,除非在可视进程和前台进程紧缺资源(如:内存资源)才会被终止

1、除前台进程和可视进程的service外的service的进程

后台进程:通常系统中有大量的后台进程,终止后台进程不会影响用户体验,随时为优先级更高的进程腾出资源而被终止,优先回收长时间没用使用过的进程。

1、一个进程只包含一个Application了

包含不在前台或可视进程的activity的进程,也就是已经调用onStop()方法后的activity

空进程:为提高整体系统性能,系统会保存已经完成生命周期的应用程序 ,存在与内存当中,也就是缓存,为下次的启动更加迅速而设计。通常会被定期地终止

查看进程优先级的方法

1、获取进程pid

> adb shell
> ps | grep 包名

2、获取进程优先级

> cat proc/pid/oom_adj

通过读取oom_adj中的整数,我们就可以很清楚的明白进程所在的优先级。oom_adj的值越小,其优先级越高

这些值的具体定义在ProcessList.java这个文件中。

frameworks/base/services/java/com/android/server/am/ProcessList.java

/**
 * Activity manager code dealing with processes.
 */
final class ProcessList {
    // OOM adjustments for processes in various states:

    // 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 = 16;

    // This is a process only hosting activities that are not visible,
    // so it can be killed without any disruption.
    static final int CACHED_APP_MAX_ADJ = 15;
    static final int CACHED_APP_MIN_ADJ = 9;

    // 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.
    static final int SERVICE_B_ADJ = 8;

    // 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.  This is important both for recent
    // task switch (toggling between the two top recent apps) as well as normal
    // UI flow such as clicking on a URI in the e-mail app to view in the browser,
    // and then pressing back to return to e-mail.
    static final int PREVIOUS_APP_ADJ = 7;

    // 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.
    static final int HOME_APP_ADJ = 6;

    // This is a process holding an application service -- killing it will not
    // have much of an impact as far as the user is concerned.
    static final int SERVICE_ADJ = 5;

    // 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.
    static final int HEAVY_WEIGHT_APP_ADJ = 4;

    // This is a process currently hosting a backup operation.  Killing it
    // is not entirely fatal but is generally a bad idea.
    static final int BACKUP_APP_ADJ = 3;

    // 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.
    static final int PERCEPTIBLE_APP_ADJ = 2;

    // This is a process only hosting activities that are visible to the
    // user, so we'd prefer they don't disappear.
    static final int 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 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 = -12;

    // The system process runs at the default adjustment.
    static final int SYSTEM_ADJ = -16;

    // Special code for native processes that are not being managed by the system (so
    // don't have an oom adj assigned by the system).
    static final int NATIVE_ADJ = -17;
}

参考:Android adb 查看进程优先级

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注