Android中進程生命周期的優先順序
“我們不是生產者,我只是大自然的搬運工。”
學習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系統會儘量維持進程的存在,但畢竟資源有限,當系統資源告急的時候會淘汰一部分進程。淘汰順序的憑據就是系統進程的優先順序了,優先順序越高越不容易被殺死,反之亦然。系統總共為進程分了五個優先順序,如下(原文後附筆者融合個人理解的簡譯):
- 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:
- It hosts an
Activity
that the user is interacting with (theActivity
'sonResume()
method has been called). - It hosts a
Service
that's bound to the activity that the user is interacting with. - It hosts a
Service
that's running "in the foreground"—the service has calledstartForeground()
. - It hosts a
Service
that's executing one of its lifecycle callbacks (onCreate()
,onStart()
, oronDestroy()
). - It hosts a
BroadcastReceiver
that's executing itsonReceive()
method.
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.
- It hosts an
- 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 (itsonPause()
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.
- It hosts an
- 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. - 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. - 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變成了可見進程。總之,在掌握了基本概念之後,需要細心的分析具體的情況,方能得出正確的判斷。