Android乱弹onLowMemory()和onTrimMemory()

今天看郭哥的LitePal框架的源码,刚打开LitePalApplication里面的源码看到了这样一幕

@Overridepublic void onLowMemory() {super.onLowMemory();mContext = getApplicationContext();}不太懂郭哥的意思.之前依稀记得有人说起onLowMemory()和onTrimMemory(),于是乎,我就去查了查源码,这篇博客就来乱弹一下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:

TRIM_MEMORY_MODERATE

系统处于低内存的运行状态中并且你的应用处于缓存应用列表的中级阶段. 如果系运行内存收到限制, 你的应用有被杀掉的风险.

TRIM_MEMORY_BACKGROUND:

也就越容易失败,还不如怀揣一颗平常心,

Android乱弹onLowMemory()和onTrimMemory()

相关文章:

你感兴趣的文章:

标签云: