Adrian Roos | 9125068 | 2017-02-06 14:48:15 -0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.util.leak; |
| 18 | |
| 19 | |
| 20 | import android.os.Build; |
| 21 | import android.os.Handler; |
| 22 | import android.os.Looper; |
| 23 | import android.support.annotation.VisibleForTesting; |
| 24 | |
| 25 | import com.android.systemui.Dependency; |
| 26 | import com.android.systemui.SystemUI; |
| 27 | |
| 28 | public class GarbageMonitor { |
| 29 | |
| 30 | private static final String TAG = "GarbageMonitor"; |
| 31 | |
| 32 | private static final long GARBAGE_INSPECTION_INTERVAL = 5 * 60 * 1000; // 5min |
| 33 | private static final int GARBAGE_ALLOWANCE = 5; |
| 34 | |
| 35 | private final Handler mHandler; |
| 36 | private final TrackedGarbage mTrackedGarbage; |
| 37 | private final LeakReporter mLeakReporter; |
| 38 | |
| 39 | public GarbageMonitor(Looper bgLooper, LeakDetector leakDetector, |
| 40 | LeakReporter leakReporter) { |
| 41 | mHandler = bgLooper != null ? new Handler(bgLooper): null; |
| 42 | mTrackedGarbage = leakDetector.getTrackedGarbage(); |
| 43 | mLeakReporter = leakReporter; |
| 44 | } |
| 45 | |
| 46 | public void start() { |
| 47 | if (mTrackedGarbage == null) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | scheduleInspectGarbage(this::inspectGarbage); |
| 52 | } |
| 53 | |
| 54 | @VisibleForTesting |
| 55 | void scheduleInspectGarbage(Runnable runnable) { |
| 56 | mHandler.postDelayed(runnable, GARBAGE_INSPECTION_INTERVAL); |
| 57 | } |
| 58 | |
| 59 | private void inspectGarbage() { |
| 60 | if (mTrackedGarbage.countOldGarbage() > GARBAGE_ALLOWANCE) { |
| 61 | Runtime.getRuntime().gc(); |
| 62 | |
| 63 | // Allow some time to for ReferenceQueue to catch up. |
| 64 | scheduleReinspectGarbage(this::reinspectGarbageAfterGc); |
| 65 | } |
| 66 | scheduleInspectGarbage(this::inspectGarbage); |
| 67 | } |
| 68 | |
| 69 | @VisibleForTesting |
| 70 | void scheduleReinspectGarbage(Runnable runnable) { |
| 71 | mHandler.postDelayed(runnable, (long) 100); |
| 72 | } |
| 73 | |
| 74 | private void reinspectGarbageAfterGc() { |
| 75 | int count = mTrackedGarbage.countOldGarbage(); |
| 76 | if (count > GARBAGE_ALLOWANCE) { |
| 77 | mLeakReporter.dumpLeak(count); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public static class Service extends SystemUI { |
| 82 | |
| 83 | private GarbageMonitor mGarbageMonitor; |
| 84 | |
| 85 | @Override |
| 86 | public void start() { |
| 87 | if (!Build.IS_DEBUGGABLE) { |
| 88 | return; |
| 89 | } |
| 90 | mGarbageMonitor = Dependency.get(GarbageMonitor.class); |
| 91 | mGarbageMonitor.start(); |
| 92 | } |
| 93 | } |
| 94 | } |