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.
只要環境允許,Android系統都會嚐試著保留著應用程式的進程。但是,如果記憶體很少時,它們還是會被砍殺的。
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.
為了能夠決定哪些進程要保留、哪些進程又要砍殺,Android將每個進程放入一個叫「層級優先權」的地方做管理了。這裡面記錄著哪些元件是執行中、哪些元件是在背景執行


Processes with the lowest importance are eliminated first, then those with the next lowest, and so on. 
如果是那種低重要性的(非被使用),會被第1個踢出去,系統會繼續找誰第二、誰第三、以此類推。

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
以上這五種進程,由高至低,排序了其進程在系統裡的優先權。

底下詳細說明這5種進程。
什麼是前景進程 (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.
可用進程沒會有任何的前景元件執行(像Activity),但是呢,它卻仍會影響使用者所看的螢幕畫面。

  • 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.
雖然Service進程不是對使用者所觀看到的畫面產生直接影響的元件,但是它們一般都被拿來做一些使用者在乎的事,像是在背景播mp3啦、從網路下載檔案啦…,所以,系統在記憶體不足以前,也會如同前景進程和可用進程一樣努力的保持著它的作業。


什麼是背景進程 (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).
背景進程是一個使用者沒有在使用狀態的activity(此時Activity的onStop()會被系統呼叫)
因為通常系統如果呼叫到程式的onStop時,幾乎可以說該應用程式已經不是使用者當下想執行了。所以Android可以很大膽的先處理掉這種進程,這麼做,一點都不影響使用者的使用體驗。


什麼是空進程 (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.
 
空進程是一個沒有執行任何啟動中的應用程式元件的進程。它存在唯一的理由就是保存cache,好讓下一次啟動元件時需要的元件,可以被快速的被找到和執行。
The system often kills these processes in order to balance overall system resources between process caches and the underlying kernel caches.
這種進程最容易被砍殺,為了讓系統保持良好的循環平衡。

No comments:

Post a Comment