概述
什么是进程?
在 Android 中,进程是指正在运行的应用程序的实例。每个应用程序在 Android 系统中运行时都会被分配一个独立的进程,进程之间相互隔离,互不干扰。每个进程都有自己的内存空间和资源分配,包括堆、栈、文件描述符、虚拟机等。
Android进程管理架构
Android 进程管理架构涉及多个组件和系统服务,用于管理应用程序的进程和资源分配,下面是 Android 进程管理的架构概述。
(1) Linux 内核层
Android 是基于 Linux 内核的操作系统,进程管理的基础是由 Linux 内核提供的进程管理机制,包括进程创建、调度、终止等功能。Android 使用 Linux 内核的进程管理特性来管理应用程序的进程。
(2) Android系统服务
- ActivityManagerService(AMS):AMS 负责管理应用程序的生命周期和任务栈,它负责启动、调度和销毁应用程序的 Activity,并管理应用程序的进程。
- PackageManagerService(PMS):PMS 负责管理应用程序的安装、卸载和包信息,包括应用程序的进程信息。
- WindowManagerService(WMS):WMS 负责管理应用程序的窗口和界面显示,它处理窗口的创建、切换、隐藏等操作。
- ContentProviderService:提供了跨进程共享数据的机制,它管理数据的访问和共享,涉及进程间的通信。
(3) 应用程序进程
应用程序进程:每个应用程序在 Android 系统中运行时都会被分配一个独立的进程。应用程序进程包含应用程序的代码、资源、堆、栈等,并通过 AMS 和其他系统服务进行管理。
进程优先级:Android 系统根据进程的优先级来管理资源分配和进程终止。不同优先级的进程在系统资源的分配和终止机制上有所不同。
(4) 进程间通信 (IPC)
Android 系统提供了多种进程间通信(IPC)机制,例如使用 Intent、Binder、共享文件、Socket、ContentProvider 等方式进行跨进程通信。
以上是 Android 进程管理的主要组件和机制,通过这些组件和机制,Android 系统能够有效管理应用程序的进程、分配资源、处理界面显示以及实现进程间的通信。这样可以确保系统资源的高效利用、应用程序的稳定运行和良好的用户体验。
Android进程状态和级别
Android系统中的进程可以处于不同的状态,进程状态在ProcessStateEnum.aidl中定义,数字越小则优先级越高。
enum ProcessStateEnum {
/** @hide Not a real process state. */
UNKNOWN = -1,
/** @hide Process is a persistent system process. */
PERSISTENT = 0,
/** @hide Process is a persistent system process and is doing UI. */
PERSISTENT_UI = 1,
/** @hide Process is hosting the current top activities. Note that this covers
* all activities that are visible to the user. */
TOP = 2,
/** @hide Process is bound to a TOP app. */
BOUND_TOP = 3,
/** @hide Process is hosting a foreground service. */
FOREGROUND_SERVICE = 4,
/** @hide Process is hosting a foreground service due to a system binding. */
BOUND_FOREGROUND_SERVICE = 5,
/** @hide Process is important to the user, and something they are aware of. */
IMPORTANT_FOREGROUND = 6,
/** @hide Process is important to the user, but not something they are aware of. */
IMPORTANT_BACKGROUND = 7,
/** @hide Process is in the background transient so we will try to keep running. */
TRANSIENT_BACKGROUND = 8,
/** @hide Process is in the background running a backup/restore operation. */
BACKUP = 9,
/** @hide Process is in the background running a service. Unlike oom_adj, this level
* is used for both the normal running in background state and the executing
* operations state. */
SERVICE = 10,
/** @hide Process is in the background running a receiver. Note that from the
* perspective of oom_adj, receivers run at a higher foreground level, but for our
* prioritization here that is not necessary and putting them below services means
* many fewer changes in some process states as they receive broadcasts. */
RECEIVER = 11,
/** @hide Same as {@link #PROCESS_STATE_TOP} but while device is sleeping. */
TOP_SLEEPING = 12,
/** @hide Process is in the background, but it can't restore its state so we want
* to try to avoid killing it. */
HEAVY_WEIGHT = 13,
/** @hide Process is in the background but hosts the home activity. */
HOME = 14,
/** @hide Process is in the background but hosts the last shown activity. */
LAST_ACTIVITY = 15,
/** @hide Process is being cached for later use and contains activities. */
CACHED_ACTIVITY = 16,
/** @hide Process is being cached for later use and is a client of another cached
* process that contains activities. */
CACHED_ACTIVITY_CLIENT = 17,
/** @hide Process is being cached for later use and has an activity that corresponds
* to an existing recent task. */
CACHED_RECENT = 18,
/** @hide Process is being cached for later use and is empty. */
CACHED_EMPTY = 19,
/** @hide Process does not exist. */
NONEXISTENT = 20,
}
以上这些值并不会直接使用,而是在ActivityManager中引用并赋值给PROCESS_STATE_XXX。
public static final int PROCESS_STATE_UNKNOWN = ProcessStateEnum.UNKNOWN;
public static final int PROCESS_STATE_PERSISTENT = ProcessStateEnum.PERSISTENT;
public static final int PROCESS_STATE_PERSISTENT_UI = ProcessStateEnum.PERSISTENT_UI;
按用户角度进程可以粗略划分为以下几种。
(1) 前台进程(Foreground process)
- 拥有用户正在交互的 Activity(已调用onResume())
- 拥有某个 Service,后者绑定到用户正在交互的 Activity
- 拥有正在“前台”运行的 Service(服务已调用 startForeground())
- 拥有正执行一个生命周期回调的 Service(onCreate()、onStart() 或 onDestroy())
- 拥有正执行其 onReceive() 方法的 BroadcastReceiver
(2) 可见进程(Visible process)
- 拥有不在前台、但仍对用户可见的 Activity(已调用onPause())。
- 拥有绑定到可见(或前台)Activity 的 Service
(3) 服务进程(Service process)
正在运行startService()方法启动的服务,且不属于上述两个更高类别进程的进程。
(4) 后台进程(Background process)
对用户不可见的Activity的进程(已调用Activity的onStop()方法)
(5) 空进程(Empty process)
不含任何活动应用组件的进程
Adj算法
而进程具体的优先级数值则是通过Adj算法确定的。通过进程Adj的取值,可以在资源紧张是选择终止或保活进程。
进程管理相关类
相关类简介
| 类 | 说明 |
|---|---|
| AMS | 负责管理应用程序的生命周期和任务栈,它负责启动、调度和销毁应用程序的 Activity,并管理应用程序的进程 |
| ApplicationInfo | ApplicationInfo 类包含有关应用程序的信息,包括进程名、包名、UID 等。通过 ApplicationInfo 可以获取应用程序的进程信息 |
| Process | 一个工具类,提供了一些静态方法,用于执行与进程相关的操作。例如,可以使用 Process.myPid() 获取当前进程的进程 ID,Process.killProcess() 杀死指定进程等。 |
| ProcessList | 管理和跟踪进程的一个辅助类,提供了一些方法和变量,用于获取进程列表、查询进程信息和进行进程管理 |
| RunningAppProcessInfo | RunningAppProcessInfo 是一个包含运行中进程信息的类。它提供了进程的一些属性,如进程名、进程 ID、进程状态等 |
ActivityThread类
ActivityThread不是线程,只不过它是在主线程的main()方法中创建的对象,自然它也是运行在主线程中。只能说ActivityThread是主线程的一部分,但不并能代表主线程。
ApplicationThread类
ApplicationThread是ActivityThread的内部类,是一个Binder对象。它是作为Activitythread和AMS通信的桥梁。
private class ApplicationThread extends IApplicationThread.Stub {...}
ApplicationThread中的所有schedule方法内部都会调用一个sendMessage()将消息发送到ActivityThread中的内部Handler对象ActivityThread.H中,而H中定义了很多消息类型。
class H extends Handler {...}
APP(ActivityThread)和AMS就是通过这种方式来进行交互的,AMS通过Binder调用,实际调用到ActivityThread中的方法 。即:
ActivityThread <---> ApplicationThread(IApplicationThread对象) <---> ActivityManagerService
在AMS代码中通常能看到以下方式获取到APP进程:
final IApplicationThread procThread = proc.getThread();
ProcessList
在 Android 源代码中,ProcessList是一个辅助类,用于管理和跟踪系统中正在运行的进程。它包含了一些静态方法和变量,用于获取进程列表、查询进程信息和进行进程管理。
(1) 获取进程列表
ProcessList提供了静态方法 getProcessList(),用于获取系统中当前运行的进程列表。该方法返回一个 List
(2) 查询进程信息
ProcessList提供了一些静态方法,例如 getProcessName(int pid)、getProcessUid(int pid)等,用于查询指定进程的名称、UID 等信息。
(3) 进程管理
ProcessList中包含了一些静态变量,用于定义进程状态、优先级等属性。它还提供了一些静态方法,如 isInterestingProcess()、killProcess()等,用于判断进程是否为关键进程、终止指定进程等操作。
(4) 进程优先级
ProcessList定义了一些静态常量,用于表示进程的优先级,如 PROCESS_STATE_NONEXISTENT、PROCESS_STATE_TOP等。这些常量用于在进程管理过程中进行优先级调度和资源分配。