一般,low memory killer会首先选择adj value徘徊在9~15的process去结束生,所以这个主要就是通过提高adj value的值来降低被杀概率;
private final boolean updateOomAdjLocked(ProcessRecord app, int hiddenAdj, int clientHiddenAdj, int emptyAdj, ProcessRecord TOP_APP, boolean doingAll) { app.hiddenAdj = hiddenAdj; app.clientHiddenAdj = clientHiddenAdj; app.emptyAdj = emptyAdj; if (app.thread == null) { return false; } final boolean wasKeeping = app.keeping; boolean success = true; boolean isWLProc = false; boolean isThirdPartyAppWhiteProcess = false; //add int mThirdPartyAdj = ProcessList.HIDDEN_APP_MIN_ADJ; computeOomAdjLocked(app, hiddenAdj, clientHiddenAdj, emptyAdj, TOP_APP, false, doingAll); if (app.curRawAdj != app.setRawAdj) { if (wasKeeping && !app.keeping) { BatteryStatsImpl stats = mBatteryStatsService.getActiveStatistics(); synchronized (stats) { app.lastWakeTime = stats.getProcessWakeTime(app.info.uid, app.pid, SystemClock.elapsedRealtime()); } app.lastCpuTime = app.curCpuTime; } app.setRawAdj = app.curRawAdj; } if (app.curAdj != app.setAdj) { /// M: LTK @{ if (FeatureOption.MTK_MEMORY_COMPRESSION_SUPPORT) { if(app.curAdj > app.setAdj && (app.curAdj >= ProcessList.PREVIOUS_APP_ADJ) && app.hasShownUi) { try { IAmsPlus amsplus = MediatekClassFactory.createInstance(IAmsPlus.class); amsplus.afterAdjAdjustment(mMainStack.convertProcessRecord(app), app.setAdj, app.curAdj, mMainStack.convertLaunchRecord(mMainStack.topRunningActivityLocked(null))); } catch (Exception e) { Log.w(TAG, Exception thrown during afterAdjAdjustment failed:", e); } } } //add 如下的code if(FeatureOption.MTK_LCA_RAM_OPTIMIZE) { if (mThirdPartyAppWhiteList.length != mThirdPartyAppAdj.length) { throw new Exception("mThirdPartyAppWhiteList is not match mThirdPartyAppAdj"); } for(int num = 0; num <= mThirdPartyAppWhiteList.length -1 ;num++) { if(mThirdPartyAppWhiteList[num].equals(app.processName) && app.curAdj > mThirdPartyAppAdj[num]) { isThirdPartyAppWhiteProcess = true; mThirdPartyAdj = mThirdPartyAppAdj[num]; } } } //add code end if(isWLProc) { if (Process.setOomAdj(app.pid, ProcessList.HIDDEN_APP_MIN_ADJ)) { if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( TAG, "Set " + app.pid + " " + app.processName + " adj " + ProcessList.HIDDEN_APP_MIN_ADJ + ": " + app.adjType); app.setAdj = ProcessList.HIDDEN_APP_MIN_ADJ; } else { success = false; Slog.w(TAG, "Failed setting oom adj of " + app + " to " + ProcessList.HIDDEN_APP_MIN_ADJ); } } //添加如下的判断: else if(isThirdPartyAppWhiteProcess) { if (Process.setOomAdj(app.pid, mThirdPartyAdj)) { if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( TAG, "Set " + app.pid + " " + app.processName + " adj " + mThirdPartyAdj + ": " + app.adjType); app.setAdj = mThirdPartyAdj; } else { success = false; Slog.w(TAG, "Failed setting oom adj of " + app + " to " + mThirdPartyAdj); } } else { if (Process.setOomAdj(app.pid, app.curAdj)) { if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v( TAG, "Set " + app.pid + " " + app.processName + " adj " + app.curAdj + ": " + app.adjType); app.setAdj = app.curAdj; } else { success = false; Slog.w(TAG, "Failed setting oom adj of " + app + " to " + app.curAdj); } } /// @} } if (app.setSchedGroup != app.curSchedGroup) { app.setSchedGroup = app.curSchedGroup; if (DEBUG_SWITCH || DEBUG_OOM_ADJ) Slog.v(TAG, "Setting process group of " + app.processName + " to " + app.curSchedGroup); if (app.waitingToKill != null && app.setSchedGroup == Process.THREAD_GROUP_BG_NONINTERACTIVE) { Slog.i(TAG, "Killing " + app.toShortString() + ": " + app.waitingToKill); EventLog.writeEvent(EventLogTags.AM_KILL, app.userId, app.pid, app.processName, app.setAdj, app.waitingToKill); app.killedBackground = true; Process.killProcessQuiet(app.pid); success = false; } else { if (true) { long oldId = Binder.clearCallingIdentity(); try { Process.setProcessGroup(app.pid, app.curSchedGroup); } catch (Exception e) { Slog.w(TAG, "Failed setting process group of " + app.pid + " to " + app.curSchedGroup); e.printStackTrace(); } finally { Binder.restoreCallingIdentity(oldId); } } else { if (app.thread != null) { try { app.thread.setSchedulingGroup(app.curSchedGroup); } catch (RemoteException e) { } } } } } return success; }