2011-09-09 36 views
13

我想知道如何執行onLowMemory()函數onLowMemory在android上如何工作?

例如。

假設我有3個活動,其onLowMemory()函數被覆蓋以清除RAM中的數據。我也有1 Application類,這也覆蓋了onLowMemory()來清理一些全局狀態數據。

現在讓我們說我們從活動A→活動B→活動C去,而活動C我們用完內存。我的問題是之後會發生什麼?

從我的理解,活動C和應用程序類的功能將被調用,我正確嗎?活動A和B的功能onLowMemory()被叫?

另外我相信活動A和B將會被殺死(因爲他們是背景活動),但在這些活動被殺害之前,他們各自的onLowMemory()被調用?

回答

4

當手機內存不足時,後臺進程將被框架殺死。如果最後一個後臺進程被終止,框架將調用每個應用程序的LowMemory。

這是在源代碼中描述的。請參閱:https://github.com/android/platform_frameworks_base/blob/master/services/java/com/android/server/am/ActivityManagerService.java#L3088

// If there are no longer any background processes running, 
    // and the app that died was not running instrumentation, 
    // then tell everyone we are now low on memory.