OnLowMemoryhtml
OnLowMemory是Android提供的API,在系統內存不足,全部後臺程序(優先級爲background的進程,不是指後臺運行的進程)都被殺死時,系統會調用OnLowMemory。系統提供的回調有:android
Application.onLowMemory()app
Activity.OnLowMemory()ide
Fragement.OnLowMemory()ui
Service.OnLowMemory()this
ContentProvider.OnLowMemory()spa
除了上述系統提供的API,還能夠本身實現ComponentCallbacks,經過API註冊,這樣也能獲得OnLowMemory回調。例如:code
public static class MyCallback implements ComponentCallbacks {
@Override public void onConfigurationChanged(Configuration arg) {
}
@Override public void onLowMemory() { //do release operation }
}
而後,經過Context.registerComponentCallbacks ()在合適的時候註冊回調就能夠了。經過這種自定義的方法,能夠在不少地方註冊回調,而不須要侷限於系統提供的組件。orm
onLowMemory 當後臺程序已經終止資源還匱乏時會調用這個方法。好的應用程序通常會在這個方法裏面釋放一些沒必要要的資源來應付當後臺程序已經終止,前臺應用程序內存還不夠時的狀況。htm
OnTrimMemory
OnTrimMemory是Android 4.0以後提供的API,系統會根據不一樣的內存狀態來回調。系統提供的回調有:
Application.onTrimMemory()
Activity.onTrimMemory()
Fragement.OnTrimMemory()
Service.onTrimMemory()
ContentProvider.OnTrimMemory()
OnTrimMemory的參數是一個int數值,表明不一樣的內存狀態:
TRIM_MEMORY_COMPLETE:內存不足,而且該進程在後臺進程列表最後一個,立刻就要被清理
TRIM_MEMORY_MODERATE:內存不足,而且該進程在後臺進程列表的中部。
TRIM_MEMORY_BACKGROUND:內存不足,而且該進程是後臺進程。
TRIM_MEMORY_UI_HIDDEN:內存不足,而且該進程的UI已經不可見了。
以上4個是4.0增長
TRIM_MEMORY_RUNNING_CRITICAL:內存不足(後臺進程不足3個),而且該進程優先級比較高,須要清理內存
TRIM_MEMORY_RUNNING_LOW:內存不足(後臺進程不足5個),而且該進程優先級比較高,須要清理內存
TRIM_MEMORY_RUNNING_MODERATE:內存不足(後臺進程超過5個),而且該進程優先級比較高,須要清理內存
以上3個是4.1增長
系統也提供了一個ComponentCallbacks2,經過Context.registerComponentCallbacks()註冊後,就會被系統回調到。
以上int值對應關係:
http://developer.android.com/reference/android/content/ComponentCallbacks2.html
Constants
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_BACKGROUNDAdded in API level 14Level for onTrimMemory(int): the process has gone on to the LRU list. This is a good opportunity to clean up resources that can efficiently and quickly be re-built if the user returns to the app.Constant Value: 40 (0x00000028)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_COMPLETE
Added in API level 14
Level for onTrimMemory(int): the process is nearing the end of the background LRU list, and if more memory isn't found soon it will be killed.
Constant Value: 80 (0x00000050)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_MODERATE
Added in API level 14 Level for onTrimMemory(int): the process is around the middle of the background LRU list; freeing memory can help the system keep other processes running later in the list for better overall performance.
Constant Value: 60 (0x0000003c)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_RUNNING_CRITICAL
Added in API level 16
Level for onTrimMemory(int): the process is not an expendable background process, but the device is running extremely low on memory and is about to not be able to keep any background processes running. Your running process should free up as many non-critical resources as it can to allow that memory to be used elsewhere. The next thing that will happen after this is onLowMemory() called to report that nothing at all can be kept in the background, a situation that can start to notably impact the user.
Constant Value: 15 (0x0000000f)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_RUNNING_LOW
Added in API level 16
Level for onTrimMemory(int): the process is not an expendable background process, but the device is running low on memory. Your running process should free up unneeded resources to allow that memory to be used elsewhere.
Constant Value: 10 (0x0000000a)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_RUNNING_MODERATE
Added in API level 16
Level for onTrimMemory(int): the process is not an expendable background process, but the device is running moderately low on memory. Your running process may want to release some unneeded resources for use elsewhere. Constant Value: 5 (0x00000005)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public static final int TRIM_MEMORY_UI_HIDDEN
Added in API level 14
Level for onTrimMemory(int): the process had been showing a user interface, and is no longer doing so. Large allocations with the UI should be released at this point to allow memory to be better managed.Constant Value: 20 (0x00000014)
OnLowMemory和OnTrimMemory的比較
1,OnLowMemory被回調時,已經沒有後臺進程;而onTrimMemory被回調時,還有後臺進程。
2,OnLowMemory是在最後一個後臺進程被殺時調用,通常狀況是low memory killer 殺進程後觸發;而OnTrimMemory的觸發更頻繁,每次計算進程優先級時,只要知足條件,都會觸發。
3,經過一鍵清理後,OnLowMemory不會被觸發,而OnTrimMemory會被觸發一次。
使用舉例:
1 @Override 2 public void onTrimMemory(int level) { 3 Log.e(TAG, " onTrimMemory ... level:" + level); 6 } 7 8 @Override 9 public void onLowMemory() { 11 Log.e(TAG, " onLowMemory ... "); 13 }
經過 ActivityManager查看進程的內存:
1 private void displayBriefMemory() { 2 final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); 3 ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo(); 4 activityManager.getMemoryInfo(info); 5 Log.i(TAG, "系統剩餘內存:" + (info.availMem >> 10) + "k"); 6 Log.i(TAG, "系統是否處於低內存運行:" + info.lowMemory); 7 Log.i(TAG, "當系統剩餘內存低於" + (info.threshold >> 10) + "k" + "時就當作低內存運行"); 8 9 util.SavedToText(TAG, "meminfo 系統剩餘內存:" + (info.availMem >> 10) + "k"10 + " " + "系統是否處於低內存運行:" + info.lowMemory + " " + "當系統剩餘內存低於"11 + (info.threshold >> 10) + "k" + "時就當作低內存運行");12 }