Android中的內存管理 – Android移動開發技術文章_手機開發 Android移動開發教學課程

先從我們剛接觸Android的一些疑惑開始:
1. 我們退出瞭全部Activity後,應用還在後臺運行。為什麼不能真正的退出應用?
2. 似乎線程也能完成service的功能,為什麼不能用線程代替service?

要回答第一個問題,就要從Android的內存管理機制說起。
Android的內存管理有一些不同尋常,如同Java或.net,Android有自己的運行時狀態和虛擬機(Dalvik)去管理程序內存。但不像其它框架(比如Java,程序打開時該程序的進程開始,程序退出時該程序的進程也就結束),android同時也管理進程的生命周期。Android會通過停止或殺死低優先級的進程來確保高優先級的進程能夠正常工作。換句話說進程的清理是由Android說瞭算的,它會在系統遭遇性能瓶頸時去殺死低優先級的進程以確保系統和高優先級進程能夠正常運行,而不是簡單的程序退出後該程序的進程也同進結束。在Android中如果系統有足夠的可用內存,那麼應用的進程就不會被銷毀,效果就是你在重新運行該應用時速度會很快。
 
每一個Android的應用都運行在一個獨立的進程和虛擬機實例中,所有的應用進程都由Android運行時環境進行統一管理(在需要的時候停止或殺死某個進程)。
 
前面提到瞭一個概念:進程的優先級。Android進程的優先級主要劃分為:活動進程或前臺進程(Foreground)、可見進程、已啟動的服務進程(Service)、後臺進程、空進程。其中前臺進程優先級最高,空進程優先級最低。文章最後附有所有進程優先級的詳細解釋。
 
那麼現在我們來回答第二個問題:為什麼應用中啟動的後臺線程不能代替Service。
一個應用變為不可見後(比如所有Activity都退出)就會從可見進程變為後臺進程,如果系統沒有足夠的內存去運行更高優先級的進程,後臺進程就很有可能被銷毀。在應用中啟動的線程其實是由應用進程派生的,如果應用進程被銷毀那麼其派生的所有線程也會一並被銷毀。通過前面關於進程優先級的介紹我們可以見到,服務進程的優先級大於後臺進程。事實上服務進程(Service)會被看做前臺進程,有很高的優先級,除非前臺進程或和用戶交互的可見進程沒有內存可用,服務進程是不會被銷毀的。
 
說到這裡我們應該已經明白瞭,我們的應用一旦變為後臺進程,那麼當內存告急時,我們的應用進程很可能被銷毀以釋放內存給更高優先級的進程,其派生的線程也當然一並被銷毀。所以有可能你在應用中啟動的想長時間運行的一個後臺線程莫名其妙的就消失瞭。但Service有很高的優先級,一般不會發生這種情況。
 
當然Service還有其它的作用,比如進程間通信和其封裝的一些功能,生命周期管理等。那是另外一個話題瞭。
 
附:進程優先級說明:
 
Active Processes Active (foreground) processes are those hosting applications with components currently interacting with the user. These are the processes Android is trying to keep responsive by reclaiming resources. There are generally very few of these processes, and they will be killed only as a last resort.

Active processes include:

Activities in an “active” state; that is, they are in the foreground and responding to user events. You will explore Activity states in greater detail later in this chapter.
Activities, Services, or Broadcast Receivers that are currently executing an onReceive event handler.
Services that are executing an onStart, onCreate, or onDestroy event handler.
Visible Processes Visible, but inactive processes are those hosting “visible” Activities. As the name suggests, visible Activities are visible, but they aren’t in the foreground or responding to user events. This happens when an Activity is only partially obscured (by a non-full-screen or transparent Activity). There are generally very few visible processes, and they’ll only be killed in extreme circumstances to allow active processes to continue.

Started Service Processes Processes hosting Services that have been started. Services support ongoing processing that should continue without a visible interface. Because Services don’t interact directly with the user, they receive a slightly lower priority than visible Activities. They are still considered to be foreground processes and won’t be killed unless resources are needed for active or visible processes.

Background Processes Processes hosting Activities that aren’t visible and that don’t have any Services that have been started are considered background processes. There will generally be a large number of background processes that Android will kill using a last-seen-first-killed pat- tern to obtain resources for foreground processes.

Empty Processes To improve overall system performance, Android often retains applications in memory after they have reached the end of their lifetimes. Android maintains this cache to improve the start-up time of applications when they’re re-launched. These processes are rou- tinely killed as required.

作者“菠菜湯,自己嘗。”
 

發佈留言