Android 內存相關 onTrimMemory,onLowMemory,MemoryInfo()


 

參考:

Android Application生命周期學習

Android中如何查看內存(上) 

Android OnLowMemory和OnTrimMemory

 

OnLowMemory

OnLowMemory是Android提供的API,在系統內存不足,所有后台程序(優先級為background的進程,不是指后台運行的進程)都被殺死時,系統會調用OnLowMemory。系統提供的回調有:

  • Application.onLowMemory()

  • Activity.OnLowMemory()

  • Fragement.OnLowMemory()

  • Service.OnLowMemory()

  • ContentProvider.OnLowMemory()

除了上述系統提供的API,還可以自己實現ComponentCallbacks,通過API注冊,這樣也能得到OnLowMemory回調。例如:

public static class MyCallback implements ComponentCallbacks {
         @Override
        public void onConfigurationChanged(Configuration arg) {
         }
 
        @Override
        public void onLowMemory() {
            //do release operation
        }
    }

然后,通過Context.registerComponentCallbacks ()在合適的時候注冊回調就可以了。通過這種自定義的方法,可以在很多地方注冊回調,而不需要局限於系統提供的組件。

onLowMemory 當后台程序已經終止資源還匱乏時會調用這個方法。好的應用程序一般會在這個方法里面釋放一些不必要的資源來應付當后台程序已經終止,前台應用程序內存還不夠時的情況。

 

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_BACKGROUND
Added in API level 14
Level 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 }

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM