From 6563a8ead90db61f57ee23ce38107937df9680e9 Mon Sep 17 00:00:00 2001 From: Jing Ji Date: Thu, 2 Sep 2021 17:49:50 -0700 Subject: [PATCH] Emit TRIM_MEMORY_UI_HIDDEN event on switching to background Previously it won't be emitted while switching from foreground to cached, but we should send it on the visibility changes. Bug: 194748319 Test: atest CtsAppTestCases Change-Id: I901d7d32790d6b888f960ca1bdfb160ea1786cde Merged-In: I901d7d32790d6b888f960ca1bdfb160ea1786cde (cherry picked from commit 88dcaf3a52020693329debd511260a6a7ba8beb2) (cherry picked from commit 2c2e0fb2c3ae68e13318a1741b1aaf0649b62caf) --- .../com/android/server/am/AppProfiler.java | 59 ++++++++----------- 1 file changed, 25 insertions(+), 34 deletions(-) diff --git a/services/core/java/com/android/server/am/AppProfiler.java b/services/core/java/com/android/server/am/AppProfiler.java index 36c0de9192793..ad0485b6df28d 100644 --- a/services/core/java/com/android/server/am/AppProfiler.java +++ b/services/core/java/com/android/server/am/AppProfiler.java @@ -1033,6 +1033,7 @@ public class AppProfiler { mService.setProcessTrackerStateLOSP(app, trackerMemFactor, now); state.setProcStateChanged(false); } + trimMemoryUiHiddenIfNecessaryLSP(app); if (curProcState >= ActivityManager.PROCESS_STATE_HOME && !app.isKilledByAm()) { if (trimMemoryLevel < curLevel[0] && (thread = app.getThread()) != null) { try { @@ -1075,24 +1076,6 @@ public class AppProfiler { } profile.setTrimMemoryLevel(ComponentCallbacks2.TRIM_MEMORY_BACKGROUND); } else { - if ((curProcState >= ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND - || state.isSystemNoUi()) && profile.hasPendingUiClean()) { - // If this application is now in the background and it - // had done UI, then give it the special trim level to - // have it free UI resources. - final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; - if (trimMemoryLevel < level && (thread = app.getThread()) != null) { - try { - if (DEBUG_SWITCH || DEBUG_OOM_ADJ) { - Slog.v(TAG_OOM_ADJ, "Trimming memory of bg-ui " - + app.processName + " to " + level); - } - thread.scheduleTrimMemory(level); - } catch (RemoteException e) { - } - } - profile.setPendingUiClean(false); - } if (trimMemoryLevel < fgTrimLevel && (thread = app.getThread()) != null) { try { if (DEBUG_SWITCH || DEBUG_OOM_ADJ) { @@ -1119,28 +1102,36 @@ public class AppProfiler { mService.setProcessTrackerStateLOSP(app, trackerMemFactor, now); state.setProcStateChanged(false); } - if ((state.getCurProcState() >= ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND - || state.isSystemNoUi()) && profile.hasPendingUiClean()) { - if (profile.getTrimMemoryLevel() < ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN - && (thread = app.getThread()) != null) { - try { - if (DEBUG_SWITCH || DEBUG_OOM_ADJ) { - Slog.v(TAG_OOM_ADJ, - "Trimming memory of ui hidden " + app.processName - + " to " + ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); - } - thread.scheduleTrimMemory(ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN); - } catch (RemoteException e) { - } - } - profile.setPendingUiClean(false); - } + trimMemoryUiHiddenIfNecessaryLSP(app); profile.setTrimMemoryLevel(0); }); } return allChanged; } + @GuardedBy({"mService", "mProcLock"}) + private void trimMemoryUiHiddenIfNecessaryLSP(ProcessRecord app) { + if ((app.mState.getCurProcState() >= ActivityManager.PROCESS_STATE_IMPORTANT_BACKGROUND + || app.mState.isSystemNoUi()) && app.mProfile.hasPendingUiClean()) { + // If this application is now in the background and it + // had done UI, then give it the special trim level to + // have it free UI resources. + final int level = ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; + IApplicationThread thread; + if (app.mProfile.getTrimMemoryLevel() < level && (thread = app.getThread()) != null) { + try { + if (DEBUG_SWITCH || DEBUG_OOM_ADJ) { + Slog.v(TAG_OOM_ADJ, "Trimming memory of bg-ui " + + app.processName + " to " + level); + } + thread.scheduleTrimMemory(level); + } catch (RemoteException e) { + } + } + app.mProfile.setPendingUiClean(false); + } + } + @GuardedBy("mProcLock") long getLowRamTimeSinceIdleLPr(long now) { return mLowRamTimeSinceLastIdle + (mLowRamStartTime > 0 ? (now - mLowRamStartTime) : 0);