监控 GC
参考:com.android.internal.os.BinderInternal
里的监控 GC 操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public class GcWatcherInternal { private static WeakReference<GcWatcher> sGcWatcher; private static ArrayList<Runnable> sGcWatchers = new ArrayList<>(); static Runnable[] sTmpWatchers = new Runnable[1]; private static long sLastGcTime;
private static final class GcWatcher { @Override protected void finalize() throws Throwable { sLastGcTime = SystemClock.uptimeMillis(); synchronized (sGcWatchers) { sTmpWatchers = sGcWatchers.toArray(sTmpWatchers); } for (int i=0; i<sTmpWatchers.length; i++) { if (sTmpWatchers[i] != null) { sTmpWatchers[i].run(); } } sGcWatcher = new WeakReference<GcWatcher>(new GcWatcher()); }
} public static void addGcWatcher(Runnable watcher) { synchronized (sGcWatchers) { sGcWatchers.add(watcher); } } public static void removeGcWatcher(Runnable watcher) { synchronized (sGcWatchers) { sGcWatchers.remove(watcher); } } }
|