Fix a memory leak in UidState.

Test: atest ProcessStatsTest
Bug: 271320793
Change-Id: I22e0e266a346f24c7ca0a95bf450320c8b68cbb1
(cherry picked from commit on googleplex-android-review.googlesource.com host: 1635233cd4)
Merged-In: I22e0e266a346f24c7ca0a95bf450320c8b68cbb1
This commit is contained in:
Yu-Ting Tseng
2023-03-02 14:20:11 -08:00
committed by Cherrypicker Worker
parent 1986f2a0f2
commit 942ff8eb8b
2 changed files with 16 additions and 0 deletions

View File

@@ -150,6 +150,7 @@ public final class UidState {
public void resetSafely(long now) { public void resetSafely(long now) {
mDurations.resetTable(); mDurations.resetTable();
mStartTime = now; mStartTime = now;
mProcesses.removeIf(p -> !p.isInUse());
} }
/** /**

View File

@@ -156,4 +156,19 @@ public class ProcessStatsTest extends TestCase {
eq(0), eq(0),
eq(APP_1_PROCESS_NAME)); eq(APP_1_PROCESS_NAME));
} }
@SmallTest
public void testSafelyResetClearsProcessInUidState() throws Exception {
ProcessStats processStats = new ProcessStats();
ProcessState processState =
processStats.getProcessStateLocked(
APP_1_PACKAGE_NAME, APP_1_UID, APP_1_VERSION, APP_1_PROCESS_NAME);
processState.makeActive();
UidState uidState = processStats.mUidStates.get(APP_1_UID);
assertTrue(uidState.isInUse());
processState.makeInactive();
uidState.resetSafely(NOW_MS);
processState.makeActive();
assertFalse(uidState.isInUse());
}
} }