Merge "Unfreeze app to trim memory" into tm-qpr-dev

This commit is contained in:
Li Li
2022-09-22 06:37:00 +00:00
committed by Android (Google) Code Review

View File

@@ -44,6 +44,7 @@ import static com.android.server.am.ActivityManagerService.appendMemInfo;
import static com.android.server.am.ActivityManagerService.getKsmInfo; import static com.android.server.am.ActivityManagerService.getKsmInfo;
import static com.android.server.am.ActivityManagerService.stringifyKBSize; import static com.android.server.am.ActivityManagerService.stringifyKBSize;
import static com.android.server.am.LowMemDetector.ADJ_MEM_FACTOR_NOTHING; import static com.android.server.am.LowMemDetector.ADJ_MEM_FACTOR_NOTHING;
import static com.android.server.am.OomAdjuster.OOM_ADJ_REASON_NONE;
import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH; import static com.android.server.wm.ActivityTaskManagerDebugConfig.DEBUG_SWITCH;
import static com.android.server.wm.ActivityTaskManagerService.DUMP_ACTIVITIES_CMD; import static com.android.server.wm.ActivityTaskManagerService.DUMP_ACTIVITIES_CMD;
@@ -1047,17 +1048,7 @@ public class AppProfiler {
} }
trimMemoryUiHiddenIfNecessaryLSP(app); trimMemoryUiHiddenIfNecessaryLSP(app);
if (curProcState >= ActivityManager.PROCESS_STATE_HOME && !app.isKilledByAm()) { if (curProcState >= ActivityManager.PROCESS_STATE_HOME && !app.isKilledByAm()) {
if (trimMemoryLevel < curLevel[0] && (thread = app.getThread()) != null) { scheduleTrimMemoryLSP(app, curLevel[0], "Trimming memory of ");
try {
if (DEBUG_SWITCH || DEBUG_OOM_ADJ) {
Slog.v(TAG_OOM_ADJ,
"Trimming memory of " + app.processName
+ " to " + curLevel[0]);
}
thread.scheduleTrimMemory(curLevel[0]);
} catch (RemoteException e) {
}
}
profile.setTrimMemoryLevel(curLevel[0]); profile.setTrimMemoryLevel(curLevel[0]);
step[0]++; step[0]++;
if (step[0] >= actualFactor) { if (step[0] >= actualFactor) {
@@ -1073,31 +1064,11 @@ public class AppProfiler {
} }
} else if (curProcState == ActivityManager.PROCESS_STATE_HEAVY_WEIGHT } else if (curProcState == ActivityManager.PROCESS_STATE_HEAVY_WEIGHT
&& !app.isKilledByAm()) { && !app.isKilledByAm()) {
if (trimMemoryLevel < ComponentCallbacks2.TRIM_MEMORY_BACKGROUND scheduleTrimMemoryLSP(app, ComponentCallbacks2.TRIM_MEMORY_BACKGROUND,
&& (thread = app.getThread()) != null) { "Trimming memory of heavy-weight ");
try {
if (DEBUG_SWITCH || DEBUG_OOM_ADJ) {
Slog.v(TAG_OOM_ADJ,
"Trimming memory of heavy-weight " + app.processName
+ " to " + ComponentCallbacks2.TRIM_MEMORY_BACKGROUND);
}
thread.scheduleTrimMemory(
ComponentCallbacks2.TRIM_MEMORY_BACKGROUND);
} catch (RemoteException e) {
}
}
profile.setTrimMemoryLevel(ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); profile.setTrimMemoryLevel(ComponentCallbacks2.TRIM_MEMORY_BACKGROUND);
} else { } else {
if (trimMemoryLevel < fgTrimLevel && (thread = app.getThread()) != null) { scheduleTrimMemoryLSP(app, fgTrimLevel, "Trimming memory of fg ");
try {
if (DEBUG_SWITCH || DEBUG_OOM_ADJ) {
Slog.v(TAG_OOM_ADJ, "Trimming memory of fg " + app.processName
+ " to " + fgTrimLevel);
}
thread.scheduleTrimMemory(fgTrimLevel);
} catch (RemoteException e) {
}
}
profile.setTrimMemoryLevel(fgTrimLevel); profile.setTrimMemoryLevel(fgTrimLevel);
} }
}); });
@@ -1128,20 +1099,26 @@ public class AppProfiler {
// If this application is now in the background and it // If this application is now in the background and it
// had done UI, then give it the special trim level to // had done UI, then give it the special trim level to
// have it free UI resources. // have it free UI resources.
final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; scheduleTrimMemoryLSP(app, ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN,
"Trimming memory of bg-ui ");
app.mProfile.setPendingUiClean(false);
}
}
@GuardedBy({"mService", "mProcLock"})
private void scheduleTrimMemoryLSP(ProcessRecord app, int level, String msg) {
IApplicationThread thread; IApplicationThread thread;
if (app.mProfile.getTrimMemoryLevel() < level && (thread = app.getThread()) != null) { if (app.mProfile.getTrimMemoryLevel() < level && (thread = app.getThread()) != null) {
try { try {
if (DEBUG_SWITCH || DEBUG_OOM_ADJ) { if (DEBUG_SWITCH || DEBUG_OOM_ADJ) {
Slog.v(TAG_OOM_ADJ, "Trimming memory of bg-ui " Slog.v(TAG_OOM_ADJ, msg + app.processName + " to " + level);
+ app.processName + " to " + level);
} }
mService.mOomAdjuster.mCachedAppOptimizer.unfreezeTemporarily(app,
OOM_ADJ_REASON_NONE);
thread.scheduleTrimMemory(level); thread.scheduleTrimMemory(level);
} catch (RemoteException e) { } catch (RemoteException e) {
} }
} }
app.mProfile.setPendingUiClean(false);
}
} }
@GuardedBy("mProcLock") @GuardedBy("mProcLock")