android service onlowmemory,Android乱弹onLowMemory()和onTrimMemory()
今天看郭哥的LitePal框架的源碼,剛打開LitePalApplication里面的源碼看到了這樣一幕
@Override
public void onLowMemory() {
super.onLowMemory();
mContext = getApplicationContext();
}
不太懂郭哥的意思.之前依稀記得有人說起onLowMemory()和onTrimMemory(),于是乎,我就去查了查源碼,這篇博客就來亂彈一下onLowMemory()和onTrimMemory()
首先通過郭哥的那段代碼,就看到了,如下部分
public void onLowMemory() {
Object[] callbacks = collectComponentCallbacks();
if (callbacks != null) {
for (int i=0; i
來了個(gè)接口回調(diào),繼續(xù)看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.
*
*
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.
*
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.
*/
void onLowMemory();
我去,這么多英文注釋,其實(shí)人家的英文注釋寫的很清楚了,onLowMemory()就是在內(nèi)存比較緊張時(shí),根據(jù)優(yōu)先級(jí)把后臺(tái)程序殺死時(shí),系統(tǒng)回調(diào)他,它用在14之前,14之后就出現(xiàn)了onTrimMemory()
public void onTrimMemory(int level) {
Object[] callbacks = collectComponentCallbacks();
if (callbacks != null) {
for (int i=0; i
/**
* 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.
*
*
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)是根據(jù)級(jí)別不同做不同的操作
TRIM_MEMORY_COMPLETE:
系統(tǒng)處于低內(nèi)存的運(yùn)行狀態(tài)中如果系統(tǒng)現(xiàn)在沒有內(nèi)存回收你的應(yīng)用將會(huì)第一個(gè)被殺掉. 你必須釋放掉所有非關(guān)鍵的資源從而恢復(fù)應(yīng)用的狀態(tài).
TRIM_MEMORY_MODERATE
系統(tǒng)處于低內(nèi)存的運(yùn)行狀態(tài)中并且你的應(yīng)用處于緩存應(yīng)用列表的中級(jí)階段. 如果系運(yùn)行內(nèi)存收到限制, 你的應(yīng)用有被殺掉的風(fēng)險(xiǎn).
TRIM_MEMORY_BACKGROUND:
系統(tǒng)處于低內(nèi)存的運(yùn)行狀態(tài)中并且你的應(yīng)用處于緩存應(yīng)用列表的初級(jí)階段. ?雖然你的應(yīng)用不會(huì)處于被殺的高風(fēng)險(xiǎn)中, 但是系統(tǒng)已經(jīng)開始清除緩存列表中的其它應(yīng)用, 所以你必須釋放資源使你的應(yīng)用繼續(xù)存留在列表中以便用戶再次回到你的應(yīng)用時(shí)能快速恢復(fù)進(jìn)行使用.
TRIM_MEMORY_UI_HIDDEN
這個(gè)過程顯示到用戶界面,提示占用內(nèi)存比較大的應(yīng)用和ui即將被釋放,ui不可見
TRIM_MEMORY_RUNNING_CRITICAL
應(yīng)用處于運(yùn)行狀態(tài)但是系統(tǒng)已經(jīng)把大多數(shù)緩存應(yīng)用殺掉了, 你必須釋放掉不是非常關(guān)鍵的資源, 如果系統(tǒng)不能回收足夠的運(yùn)行內(nèi)存, 系統(tǒng)會(huì)清除所有緩存應(yīng)用并且會(huì)把正在活動(dòng)的應(yīng)用殺掉.
TRIM_MEMORY_RUNNING_LOW
應(yīng)用處于運(yùn)行狀態(tài)并且不會(huì)被殺掉, 設(shè)備可以使用的內(nèi)存非常低, 可以把不用的資源釋放一些提高性能(會(huì)直接影響程序的性能)
TRIM_MEMORY_RUNNING_MODERATE
應(yīng)用處于運(yùn)行狀態(tài)并且不會(huì)被殺掉, 設(shè)備使用的內(nèi)存比較低, 系統(tǒng)級(jí)會(huì)殺掉一些其它的緩存應(yīng)用.
OnLowMemory()和OnTrimMemory()的比較
1,OnLowMemory被回調(diào)時(shí),已經(jīng)沒有后臺(tái)進(jìn)程;而onTrimMemory被回調(diào)時(shí),還有后臺(tái)進(jìn)程。
2,OnLowMemory是在最后一個(gè)后臺(tái)進(jìn)程被殺時(shí)調(diào)用,一般情況是low memory killer 殺進(jìn)程后觸發(fā);而OnTrimMemory的觸發(fā)更頻繁,每次計(jì)算進(jìn)程優(yōu)先級(jí)時(shí),只要滿足條件,都會(huì)觸發(fā)。
3,通過一鍵清理后,OnLowMemory不會(huì)被觸發(fā),而OnTrimMemory會(huì)被觸發(fā)一次。
總結(jié)
以上是生活随笔為你收集整理的android service onlowmemory,Android乱弹onLowMemory()和onTrimMemory()的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mapperscan注解_SpringB
- 下一篇: SpringBoot编写sh脚本进行启停