blob: ba9e60a9c9273b3c9553b5053d6795beef2f54c5 [file] [log] [blame]
Adrian Roos91250682017-02-06 14:48:15 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.systemui.util.leak;
18
19
20import android.os.Build;
21import android.os.Handler;
22import android.os.Looper;
Adrian Roosfb2bb3f2017-02-14 17:19:16 +010023import android.os.SystemProperties;
Adrian Roos91250682017-02-06 14:48:15 -080024import android.support.annotation.VisibleForTesting;
25
26import com.android.systemui.Dependency;
27import com.android.systemui.SystemUI;
28
29public class GarbageMonitor {
30
31 private static final String TAG = "GarbageMonitor";
32
33 private static final long GARBAGE_INSPECTION_INTERVAL = 5 * 60 * 1000; // 5min
34 private static final int GARBAGE_ALLOWANCE = 5;
35
36 private final Handler mHandler;
37 private final TrackedGarbage mTrackedGarbage;
38 private final LeakReporter mLeakReporter;
39
40 public GarbageMonitor(Looper bgLooper, LeakDetector leakDetector,
41 LeakReporter leakReporter) {
42 mHandler = bgLooper != null ? new Handler(bgLooper): null;
43 mTrackedGarbage = leakDetector.getTrackedGarbage();
44 mLeakReporter = leakReporter;
45 }
46
47 public void start() {
48 if (mTrackedGarbage == null) {
49 return;
50 }
51
52 scheduleInspectGarbage(this::inspectGarbage);
53 }
54
55 @VisibleForTesting
56 void scheduleInspectGarbage(Runnable runnable) {
57 mHandler.postDelayed(runnable, GARBAGE_INSPECTION_INTERVAL);
58 }
59
60 private void inspectGarbage() {
61 if (mTrackedGarbage.countOldGarbage() > GARBAGE_ALLOWANCE) {
62 Runtime.getRuntime().gc();
63
64 // Allow some time to for ReferenceQueue to catch up.
65 scheduleReinspectGarbage(this::reinspectGarbageAfterGc);
66 }
67 scheduleInspectGarbage(this::inspectGarbage);
68 }
69
70 @VisibleForTesting
71 void scheduleReinspectGarbage(Runnable runnable) {
72 mHandler.postDelayed(runnable, (long) 100);
73 }
74
75 private void reinspectGarbageAfterGc() {
76 int count = mTrackedGarbage.countOldGarbage();
77 if (count > GARBAGE_ALLOWANCE) {
78 mLeakReporter.dumpLeak(count);
79 }
80 }
81
82 public static class Service extends SystemUI {
83
Adrian Roosfb2bb3f2017-02-14 17:19:16 +010084 // TODO(b/35345376): Turn this back on for debuggable builds after known leak fixed.
85 private static final boolean ENABLED = Build.IS_DEBUGGABLE
86 && SystemProperties.getBoolean("debug.enable_leak_reporting", false);
87
Adrian Roos91250682017-02-06 14:48:15 -080088 private GarbageMonitor mGarbageMonitor;
89
90 @Override
91 public void start() {
Adrian Roosfb2bb3f2017-02-14 17:19:16 +010092 if (!ENABLED) {
Adrian Roos91250682017-02-06 14:48:15 -080093 return;
94 }
95 mGarbageMonitor = Dependency.get(GarbageMonitor.class);
96 mGarbageMonitor.start();
97 }
98 }
99}