日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android service onlowmemory,Android乱弹onLowMemory()和onTrimMemory()

發布時間:2025/3/15 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

來了個接口回調,繼續看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();

我去,這么多英文注釋,其實人家的英文注釋寫的很清楚了,onLowMemory()就是在內存比較緊張時,根據優先級把后臺程序殺死時,系統回調他,它用在14之前,14之后就出現了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)是根據級別不同做不同的操作

TRIM_MEMORY_COMPLETE:

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

TRIM_MEMORY_MODERATE

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

TRIM_MEMORY_BACKGROUND:

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

TRIM_MEMORY_UI_HIDDEN

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

TRIM_MEMORY_RUNNING_CRITICAL

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

TRIM_MEMORY_RUNNING_LOW

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

TRIM_MEMORY_RUNNING_MODERATE

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

OnLowMemory()和OnTrimMemory()的比較

1,OnLowMemory被回調時,已經沒有后臺進程;而onTrimMemory被回調時,還有后臺進程。

2,OnLowMemory是在最后一個后臺進程被殺時調用,一般情況是low memory killer 殺進程后觸發;而OnTrimMemory的觸發更頻繁,每次計算進程優先級時,只要滿足條件,都會觸發。

3,通過一鍵清理后,OnLowMemory不會被觸發,而OnTrimMemory會被觸發一次。

總結

以上是生活随笔為你收集整理的android service onlowmemory,Android乱弹onLowMemory()和onTrimMemory()的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 久久综合一本 | 欧美老少做受xxxx高潮 | 做暧暧视频在线观看 | 夜夜超碰| 亚洲AV无码成人精品一区 | 日本高清www免费视频 | 四虎4hu永久免费网站影院 | 成年人网站免费视频 | 亚洲综合色在线 | 一个人看的www日本高清视频 | 亚洲av无码专区在线 | 欧美日韩人妻精品一区 | 午夜影院日本 | 国产精华一区二区三区 | 欧美日韩一区二区视频在线观看 | 麻豆蜜桃wwww精品无码 | 欧美r级在线观看 | 欧美精品99久久久 | 久久国产成人精品国产成人亚洲 | 日韩视频中文字幕 | 蜜桃av一区二区 | 观看av免费 | 超碰2 | 深夜久久| 4438x在线观看 | 亚洲精品午夜 | 六月丁香啪啪 | 成人免费在线小视频 | japanese中文字幕 | wwwwxxxx国产 | 一本大道熟女人妻中文字幕在线 | 国产麻豆交换夫妇 | 亚洲30p | 免费播放黄色片 | 秋霞99| 在线观看久草 | 在线不卡一区 | 中文字幕在线观看视频免费 | 久久在线一区二区 | 91在线国产观看 | 图片区偷拍区小说区 | 国产综合在线观看视频 | 国产第五页 | 欧美多人猛交狂配 | 韩国bj大尺度vip福利网站 | 美女被啪啪 | 色5月婷婷 | 激情爱爱网站 | 破处视频在线观看 | juliaannxxxxx高清 黄页网站在线播放 | 中文字幕在线资源 | 日韩免费精品视频 | 波多野42部无码喷潮在线 | 岛国精品在线 | 欧美777| aaaa级片| 一级片黄色 | 91插插插插| 成人国产亚洲 | 亚洲天堂热 | 亚洲精品三级 | 激情六月 | 日韩av无码久久 | 天堂va在线 | 91色综合 | 97在线免费视频观看 | 国产在线免费观看 | 99热都是精品 | 欧美拍拍 | 你懂的在线观看视频 | 穿情趣内衣被c到高潮视频 欧美性猛交xxxx黑人猛交 | 亚洲制服丝袜av | 风韵少妇spa私密视频 | 国产精品2018| 免费av在线网址 | 久草福利在线视频 | 先锋影音一区二区三区 | 欧美bbbbbbbbbbbb精品 | 九色91丨porny丨丝袜 | 国产伦精品一区二区三区免.费 | 人与动物2免费观看完整版电影高清 | 欧美日韩午夜爽爽 | 欧美日韩在线精品 | 日本www高清 | 精品欧美国产 | 韩日毛片| 成人免费黄色片 | 少妇25p| 精品视频在线观看免费 | 91精品国自产 | 最新高清无码专区 | 91精品国产自产在线观看 | 欧美特级黄色片 | 大尺度在线观看 | 理论片午夜 | 亚洲精品你懂的 | 欧美三级视频网站 | 97xxxx| 99国产精品人妻噜啊噜 |