Android中进程生命周期的优先级

时间:2023-12-14 18:41:38

“我们不是生产者,我只是大自然的搬运工。”

学习Android最好的途径当然是强大的官方文档了,其中在Processes and Threads一节中对于进程生命周期淘汰优先级,有着详细的介绍。原文如下:

Process lifecycle

The Android system tries to maintain an application process for as long as possible, but eventually needs to remove old processes to reclaim memory for new or more important processes. To determine which processes to keep and which to kill, the system places each process into an "importance hierarchy" based on the components running in the process and the state of those components. Processes with the lowest importance are eliminated first, then those with the next lowest importance, and so on, as necessary to recover system resources.

上文大致意思就是说Android系统会尽量维持进程的存在,但毕竟资源有限,当系统资源告急的时候会淘汰一部分进程。淘汰顺序的凭据就是系统进程的优先级了,优先级越高越不容易被杀死,反之亦然。系统总共为进程分了五个优先级,如下(原文后附笔者融合个人理解的简译):

  1. Foreground process

    A process that is required for what the user is currently doing. A process is considered to be in the foreground if any of the following conditions are true:

    Generally, only a few foreground processes exist at any given time. They are killed only as a last resort—if memory is so low that they cannot all continue to run. Generally, at that point, the device has reached a memory paging state, so killing some foreground processes is required to keep the user interface responsive.

  2. Visible process

    A process that doesn't have any foreground components, but still can affect what the user sees on screen. A process is considered to be visible if either of the following conditions are true:

    • It hosts an Activity that is not in the foreground, but is still visible to the user (its onPause() method has been called). This might occur, for example, if the foreground activity started a dialog, which allows the previous activity to be seen behind it.
    • It hosts a Service that's bound to a visible (or foreground) activity.

    A visible process is considered extremely important and will not be killed unless doing so is required to keep all foreground processes running.

  3. Service process

    A process that is running a service that has been started with the startService() method and does not fall into either of the two higher categories. Although service processes are not directly tied to anything the user sees, they are generally doing things that the user cares about (such as playing music in the background or downloading data on the network), so the system keeps them running unless there's not enough memory to retain them along with all foreground and visible processes.

  4. Background process

    A process holding an activity that's not currently visible to the user (the activity's onStop() method has been called). These processes have no direct impact on the user experience, and the system can kill them at any time to reclaim memory for a foreground, visible, or service process. Usually there are many background processes running, so they are kept in an LRU (least recently used) list to ensure that the process with the activity that was most recently seen by the user is the last to be killed. If an activity implements its lifecycle methods correctly, and saves its current state, killing its process will not have a visible effect on the user experience, because when the user navigates back to the activity, the activity restores all of its visible state. See the Activities document for information about saving and restoring state.

  5. Empty process

    A process that doesn't hold any active application components. The only reason to keep this kind of process alive is for caching purposes, to improve startup time the next time a component needs to run in it. The system often kills these processes in order to balance overall system resources between process caches and the underlying kernel caches.

一、前台进程(进程满足如下任一条件即为前台进程):

1. 拥有 一个执行了onresume方法正在与用户交互(获得焦点)的Activity
2. 拥有一个service,这个Service跟正在与用户交互的Activity进行了绑定
3. 拥有一个Service,这个Service调用了startForeground()方法
4. 拥有一个正在执行onCreate()、onStart()或者onDestroy()方法中的任意一个的Service
5. 拥有一个正在执行onReceive方法的BroadcastReceiver

二、可见进程:

1. 拥有一个执行了onPause方法,但仍然可见的Activity
2. 拥有一个Service,这个Service跟一个可见的或前台的Activity绑定了

三、服务进程:

拥有一个通过startService方法启动的Service的进程

四、后台进程:

拥有一个后台Activity(onStop方法被调用)的进程

五、空进程:

没有拥有任何活动的应用组件的进程,也就是没有任何Service和Activity在运行

另外,还有一些需要补充的,当一个进程满足多个进程条件时,当然是取优先级更高的为准,比如一个进程同时满足前台进程和服务进程的条件,这个进程就是个前台进程,这点很好理解。另外,进程的优先级也不是一成不变的,而且有时候会随着一些相关的因素而发生改变;比如,某进程A满足前台进程的第二个条件,进程A拥有一个service,这个Service跟正在与用户交互的Activity进行了绑定;当这个Activity变成可见状态了,进程A便不再满足前台进程的条件,进而因满足可见进程的第二个条件,进程A变成了可见进程。总之,在掌握了基本概念之后,需要细心的分析具体的情况,方能得出正确的判断。