Do not hold direct ref BatteryStatsImpl$Uid$Proc
BatteryStatsImpl can reset its collected data, including
removing a BatteryStatsImpl$Uid$Proc object. If a ProcessRecord
has a direct reference, then the battery stats for a process
will be recorded in an old Proc object and prevent GC, causing
a memory leak.
bug:11087238
Change-Id: I19a9cd9d8361c10446a8ebdd5c0860b56c442209
diff --git a/services/java/com/android/server/am/ActivityManagerService.java b/services/java/com/android/server/am/ActivityManagerService.java
index fe83e9f..9201b1d 100644
--- a/services/java/com/android/server/am/ActivityManagerService.java
+++ b/services/java/com/android/server/am/ActivityManagerService.java
@@ -2149,7 +2149,8 @@
totalUTime += otherUTime;
totalSTime += otherSTime;
if (pr != null) {
- BatteryStatsImpl.Uid.Proc ps = pr.batteryStats;
+ BatteryStatsImpl.Uid.Proc ps = bstats.getProcessStatsLocked(
+ st.name, st.pid);
ps.addCpuTimeLocked(st.rel_utime-otherUTime,
st.rel_stime-otherSTime);
ps.addSpeedStepTimes(cpuSpeedTimes);
@@ -2769,10 +2770,10 @@
app.processName, uid, uid, gids, debugFlags, mountExternal,
app.info.targetSdkVersion, app.info.seinfo, null);
- BatteryStatsImpl bs = app.batteryStats.getBatteryStats();
+ BatteryStatsImpl bs = mBatteryStatsService.getActiveStatistics();
synchronized (bs) {
if (bs.isOnBattery()) {
- app.batteryStats.incStartsLocked();
+ bs.getProcessStatsLocked(app.uid, app.processName).incStartsLocked();
}
}
@@ -8150,10 +8151,7 @@
}
}
}
- synchronized (stats) {
- ps = stats.getProcessStatsLocked(info.uid, proc);
- }
- return new ProcessRecord(ps, info, proc, uid);
+ return new ProcessRecord(stats, info, proc, uid);
}
final ProcessRecord addAppLocked(ApplicationInfo info, boolean isolated) {
diff --git a/services/java/com/android/server/am/ProcessRecord.java b/services/java/com/android/server/am/ProcessRecord.java
index 187cd44..217a8d61 100644
--- a/services/java/com/android/server/am/ProcessRecord.java
+++ b/services/java/com/android/server/am/ProcessRecord.java
@@ -46,7 +46,7 @@
* is currently running.
*/
final class ProcessRecord {
- final BatteryStatsImpl.Uid.Proc batteryStats; // where to collect runtime statistics
+ private final BatteryStatsImpl mBatteryStats; // where to collect runtime statistics
final ApplicationInfo info; // all about the first app in the process
final boolean isolated; // true if this is a special isolated process
final int uid; // uid of process; may be different from 'info' if isolated
@@ -273,8 +273,8 @@
}
if (!keeping) {
long wtime;
- synchronized (batteryStats.getBatteryStats()) {
- wtime = batteryStats.getBatteryStats().getProcessWakeTime(info.uid,
+ synchronized (mBatteryStats) {
+ wtime = mBatteryStats.getProcessWakeTime(info.uid,
pid, SystemClock.elapsedRealtime());
}
long timeUsed = wtime - lastWakeTime;
@@ -359,9 +359,9 @@
}
}
- ProcessRecord(BatteryStatsImpl.Uid.Proc _batteryStats, ApplicationInfo _info,
+ ProcessRecord(BatteryStatsImpl _batteryStats, ApplicationInfo _info,
String _processName, int _uid) {
- batteryStats = _batteryStats;
+ mBatteryStats = _batteryStats;
info = _info;
isolated = _info.uid != _uid;
uid = _uid;