Monday, December 13, 2010

進程與生命週期(Process and lifecycles)

以下文章來至Application Fundamentals :
The Android system tries to maintain an application process for as long as possible, but eventually

it will need to remove old processes when memory runs low.
To determine which processes to keep and which to kill, Android places each process

into an "importance hierarchy" based on the components running in it and the state of those components.

Processes with the lowest importance are eliminated first, then those with the next lowest, and so on. 

There are

five levels
 in the hierarchy. The following list presents them in order of importance:

1.A foreground process
2.A visible process
3.A service process
4.A background process
5.An empty process

什麼是前景進程 (foreground process)?

  • It is running an activity that the user is interacting with (the Activity object's onResume() method has been called). - Activity正在被使用者使用的當下(onResume()事件發生時)
  • It hosts a service that's bound to the activity that the user is interacting with. - 一個activity有互動的Service
  • It has a Service object that's executing one of its lifecycle callbacks (onCreate(), onStart(), or onDestroy()). - 有一個Service物件,而且這個Service物件還執行callback回呼函式(像Service裡的onCreate()、onStart()或onDestroy()之類的)
  • It has a BroadcastReceiver object that's executing its onReceive() method. - 有一個廣播監聽物件,而且該廣播還執行了onReceive這個callback回呼函式。
Only a few foreground processes will exist at any given time. They are killed only as a last resort.

什麼是可用進程 (visible process)?

A visible process is one that

doesn't have any foreground components, but still can affect what the user sees on screen.

  • It hosts an activity that is not in the foreground, but is still visible to the user (its onPause() method has been called). This may occur, for example, if the foreground activity is a dialog that allows the previous activity to be seen behind it. - 像是我們叫出了一個畫面裡的訊息對話視窗,此時原程式的onPause()被呼叫,我們稱此時該進程為可用進程
  • It hosts a service that's bound to a visible activity. - 一個綁到可用activity中的Service

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

什麼是Service進程 (service process)?
  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 an mp3 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 background process is

one holding an activity that's not currently visible to the user (the Activity object's
onStop() method has been called).

什麼是空進程 (empty process)?
An empty process is one that

doesn't hold any active application components.
The only reason to keep such a process around is
as a cache 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.

No comments: