一、通过杀empty进程和cache进程来释放内存
- 如果空进程的个数大于mConstants.CUR_TRIM_EMPTY_PROCESSES(CUR_TRIM_EMPTY_PROCESSES 的这个依据配置值的不同而不同,具体的可以见updateMaxCachedProcesses这个函数),并且该应用的上次活跃的时间,大于半个小时,这里的活跃是指该应用有相关组件在活动,比如:在处理广播
- 空进程个数大于了最大限制
- 当cache进程达到阈值的时候,就开始杀cache进程
具体的代码位置如下:
文件路径:frameworks/base/services/core/java/com/android/server/am/OomAdjuster.java
方法:updateOomAdjLocked(String oomAdjReason)
代码片段:
.....
final long oldTime = now - ProcessList.MAX_EMPTY_TIME; // static final long
MAX_EMPTY_TIME = 30 * 60 * 1000;
.....
switch (app.getCurProcState()) {
case PROCESS_STATE_CACHED_ACTIVITY:
case ActivityManager.PROCESS_STATE_CACHED_ACTIVITY_CLIENT:
mNumCachedHiddenProcs++;
numCached++;
if (app.connectionGroup != 0) {
if (lastCachedGroupUid == app.info.uid
&& lastCachedGroup == app.connectionGroup) {
// If this process is the next in the same group, we don't
// want it to count against our limit of the number of cached
// processes, so bump up the group count to account for it.
numCachedExtraGroup++;
} else {
lastCachedGroupUid = app.info.uid;
lastCachedGroup = app.connectionGroup;
}
} else {
lastCachedGroupUid = lastCachedGroup = 0;
}
if ((numCached - numCachedExtraGroup) > cachedProcessLimit) {
app.kill("cached #" + numCached, true);
}
break;
case PROCESS_STATE_CACHED_EMPTY: