多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > Android亂彈onLowMemory()和onTrimMemory()

Android亂彈onLowMemory()和onTrimMemory()

來源:程序員人生   發布時間:2014-12-07 09:33:26 閱讀次數:5786次

今天看郭哥的LitePal框架的源碼,剛打開LitePalApplication里面的源碼看到了這樣1幕

@Override public void onLowMemory() { super.onLowMemory(); mContext = getApplicationContext(); }

不太懂郭哥的意思.之前模糊記得有人說起onLowMemory()和onTrimMemory(),因而乎,我就去查了查源碼,這篇博客就來亂彈1下onLowMemory()和onTrimMemory()


首先通過郭哥的那段代碼,就看到了,以下部份

public void onLowMemory() { Object[] callbacks = collectComponentCallbacks(); if (callbacks != null) { for (int i=0; i<callbacks.length; i++) { ((ComponentCallbacks)callbacks[i]).onLowMemory(); } } }

來了個接口回調,繼續看onLowMemory()

/** * This is called when the overall system is running low on memory, and * actively running processes should trim their memory usage. While * the exact point at which this will be called is not defined, generally * it will happen when all background process have been killed. * That is, before reaching the point of killing processes hosting * service and foreground UI that we would like to avoid killing. * * <p>You should implement this method to release * any caches or other unnecessary resources you may be holding on to. * The system will perform a garbage collection for you after returning from this method. * <p>Preferably, you should implement {@link ComponentCallbacks2#onTrimMemory} from * {@link ComponentCallbacks2} to incrementally unload your resources based on various * levels of memory demands. That API is available for API level 14 and higher, so you should * only use this {@link #onLowMemory} method as a fallback for older versions, which can be * treated the same as {@link ComponentCallbacks2#onTrimMemory} with the {@link * ComponentCallbacks2#TRIM_MEMORY_COMPLETE} level.</p> */ void onLowMemory();

我去,這么多英文注釋,其實人家的英文注釋寫的很清楚了,onLowMemory()就是在內存比較緊張時,根據優先級把后臺程序殺死時,系統回調他,它用在14之前,14以后就出現了onTrimMemory()

public void onTrimMemory(int level) { Object[] callbacks = collectComponentCallbacks(); if (callbacks != null) { for (int i=0; i<callbacks.length; i++) { Object c = callbacks[i]; if (c instanceof ComponentCallbacks2) { ((ComponentCallbacks2)c).onTrimMemory(level); } } } }


/** * Level for {@link #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. */ static final int TRIM_MEMORY_COMPLETE = 80; /** * Level for {@link #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. */ static final int TRIM_MEMORY_MODERATE = 60; /** * Level for {@link #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. */ static final int TRIM_MEMORY_BACKGROUND = 40; /** * Level for {@link #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. */ static final int TRIM_MEMORY_UI_HIDDEN = 20; /** * Level for {@link #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 {@link #onLowMemory()} called to report that * nothing at all can be kept in the background, a situation that can start * to notably impact the user. */ static final int TRIM_MEMORY_RUNNING_CRITICAL = 15; /** * Level for {@link #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. */ static final int TRIM_MEMORY_RUNNING_LOW = 10; /** * Level for {@link #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. */ static final int TRIM_MEMORY_RUNNING_MODERATE = 5; /** * Called when the operating system has determined that it is a good * time for a process to trim unneeded memory from its process. This will * happen for example when it goes in the background and there is not enough * memory to keep as many background processes running as desired. You * should never compare to exact values of the level, since new intermediate * values may be added -- you will typically want to compare if the value * is greater or equal to a level you are interested in. * * <p>To retrieve the processes current trim level at any point, you can * use {@link android.app.ActivityManager#getMyMemoryState * ActivityManager.getMyMemoryState(RunningAppProcessInfo)}. * * @param level The context of the trim, giving a hint of the amount of * trimming the application may like to perform. May be * {@link #TRIM_MEMORY_COMPLETE}, {@link #TRIM_MEMORY_MODERATE}, * {@link #TRIM_MEMORY_BACKGROUND}, {@link #TRIM_MEMORY_UI_HIDDEN}, * {@link #TRIM_MEMORY_RUNNING_CRITICAL}, {@link #TRIM_MEMORY_RUNNING_LOW}, * or {@link #TRIM_MEMORY_RUNNING_MODERATE}. */ void onTrimMemory(int level);
onTrimMemory(int level)是根據級別不同做不同的操作

TRIM_MEMORY_COMPLETE:

系統處于低內存的運行狀態中如果系統現在沒有內存回收你的利用將會第1個被殺掉. 你必須釋放掉所有非關鍵的資源從而恢復利用的狀態.

TRIM_MEMORY_MODERATE

  系統處于低內存的運行狀態中并且你的利用處于緩存利用列表的中級階段. 如果系運行內存收到限制, 你的利用有被殺掉的風險.

TRIM_MEMORY_BACKGROUND:

    系統處于低內存的運行狀態中并且你的利用處于緩存利用列表的低級階段.  雖然你的利用不會處于被殺的高風險中, 但是系統已開始清除緩存列表中的其它利用, 所以你必須釋放資源使你的利用繼續存留在列表中以便用戶再次回到你的利用時能快速恢復進行使用.

TRIM_MEMORY_UI_HIDDEN

     這個進程顯示到用戶界面,提示占用內存比較大的利用和ui行將被釋放,ui不可見

TRIM_MEMORY_RUNNING_CRITICAL

    利用處于運行狀態但是系統已把大多數緩存利用殺掉了, 你必須釋放掉不是非常關鍵的資源, 如果系統不能回收足夠的運行內存, 系統會清除所有緩存利用并且會把正在活動的利用殺掉.

TRIM_MEMORY_RUNNING_LOW

    利用處于運行狀態并且不會被殺掉, 裝備可使用的內存非常低, 可以把不用的資源釋放1些提高性能(會直接影響程序的性能)

TRIM_MEMORY_RUNNING_MODERATE

   利用處于運行狀態并且不會被殺掉, 裝備使用的內存比較低, 系統級會殺掉1些其它的緩存利用.

OnLowMemory()和OnTrimMemory()的比較

1,OnLowMemory被回調時,已沒有后臺進程;而onTrimMemory被回調時,還有后臺進程。
2,OnLowMemory是在最后1個后臺進程被殺時調用,1般情況是low memory killer 殺進程后觸發;而OnTrimMemory的觸發更頻繁,每次計算進程優先級時,只要滿足條件,都會觸發。
3,通過1鍵清算后,OnLowMemory不會被觸發,而OnTrimMemory會被觸發1次。



生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 最近最新中文字幕大全免费7 | 欧美一区二区三区在线观看 | 4438x成人网最大色成网站 | 亚洲精品一区二区观看 | 中文欧美日韩 | 在线免费欧美 | 国产美女无遮挡免费网站 | 亚洲欧美专区精品久久 | 在线观看a网站 | 日韩 欧美 亚洲 | 国产a在亚洲线播放 | 欧美一级aa免费毛片 | 国产日产欧美一区二区三区 | 欧美性天堂 | 日本高清免费毛片久久看 | 尤物网站永久在线观看 | 色www| 欧美高清在线不卡免费观看 | 色一情一伦一区二区三 | 男女日日 | 国产性一交一乱一伦一色一情 | 秋霞一级片 | 国产亚洲精品热视频在线观看 | 性欧美videos俄罗斯 | 国产精品成人久久久久 | 成人精品一级毛片 | 欧美人与性动交α欧美精品图片 | 久久国产精品1区2区3区网页 | 最近免费中文字幕大全高清mv | 秋霞网亚洲成a人片 | 亚洲自偷| 五月婷婷狠狠 | 欧美亚洲国产精品蜜芽 | 国产精品自拍第一页 | 亚洲视频二区 | 精品无人区一区二区三 | 亚洲免费三级 | 免费一看一级欧美 | 欧美videos另类极品 | 日韩视频一区二区三区 | 久久国产精品自由自在 |