OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.common; | 5 package org.chromium.content.common; |
6 | 6 |
7 import android.os.Handler; | 7 import android.os.Handler; |
8 import android.os.Looper; | 8 import android.os.Looper; |
9 import android.os.Message; | 9 import android.os.Message; |
10 | 10 |
11 import org.chromium.base.Log; | 11 import org.chromium.base.Log; |
12 import org.chromium.base.ThreadUtils; | 12 import org.chromium.base.ThreadUtils; |
13 import org.chromium.base.TraceEvent; | 13 import org.chromium.base.TraceEvent; |
14 | 14 |
15 import java.lang.ref.PhantomReference; | |
16 import java.lang.ref.ReferenceQueue; | 15 import java.lang.ref.ReferenceQueue; |
| 16 import java.lang.ref.WeakReference; |
17 import java.util.HashSet; | 17 import java.util.HashSet; |
18 import java.util.Set; | 18 import java.util.Set; |
19 | 19 |
20 /** | 20 /** |
21 * Handles running cleanup tasks when an object becomes eligible for GC. Cleanup
tasks | 21 * Handles running cleanup tasks when an object becomes eligible for GC. Cleanup
tasks |
22 * are always executed on the main thread. In general, classes should not have | 22 * are always executed on the main thread. In general, classes should not have |
23 * finalizers and likewise should not use this class for the same reasons. The | 23 * finalizers and likewise should not use this class for the same reasons. The |
24 * exception is where public APIs exist that require native side resources to be | 24 * exception is where public APIs exist that require native side resources to be |
25 * cleaned up in response to java side GC of API objects. (Private/internal | 25 * cleaned up in response to java side GC of API objects. (Private/internal |
26 * interfaces should always favor explicit resource releases / destroy() | 26 * interfaces should always favor explicit resource releases / destroy() |
27 * protocol for this rather than depend on GC to trigger native cleanup). | 27 * protocol for this rather than depend on GC to trigger native cleanup). |
28 * | 28 * NOTE this uses WeakReference rather than PhantomReference, to avoid delaying
the |
29 * NOTE Using PhantonReference instead of WeakReference is required for correctn
ess. | 29 * cleanup processing until after finalizers (if any) have run. In general usage
of |
30 * WeakReferences are enqueued before finalizers are called, and finalizers can | 30 * this class indicates the client does NOT use finalizers anyway (Good), so thi
s should |
31 * resurrect the referent object. PhantomReference does delay clean up more comp
ared | 31 * not be a visible difference in practice. |
32 * to WeakReference. | |
33 */ | 32 */ |
34 public class CleanupReference extends PhantomReference<Object> { | 33 public class CleanupReference extends WeakReference<Object> { |
35 private static final String TAG = "cr.CleanupReference"; | 34 private static final String TAG = "cr.CleanupReference"; |
36 | 35 |
37 private static final boolean DEBUG = false; // Always check in as false! | 36 private static final boolean DEBUG = false; // Always check in as false! |
38 | 37 |
39 // The VM will enqueue CleanupReference instance onto sGcQueue when it becom
es eligible for | 38 // The VM will enqueue CleanupReference instance onto sGcQueue when it becom
es eligible for |
40 // garbage collection (i.e. when all references to the underlying object are
nullified). | 39 // garbage collection (i.e. when all references to the underlying object are
nullified). |
41 // |sReaperThread| processes this queue by forwarding the references on to t
he UI thread | 40 // |sReaperThread| processes this queue by forwarding the references on to t
he UI thread |
42 // (via REMOVE_REF message) to perform cleanup. | 41 // (via REMOVE_REF message) to perform cleanup. |
43 private static ReferenceQueue<Object> sGcQueue = new ReferenceQueue<Object>(
); | 42 private static ReferenceQueue<Object> sGcQueue = new ReferenceQueue<Object>(
); |
44 private static Object sCleanupMonitor = new Object(); | 43 private static Object sCleanupMonitor = new Object(); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 if (DEBUG) Log.d(TAG, "runCleanupTaskInternal"); | 160 if (DEBUG) Log.d(TAG, "runCleanupTaskInternal"); |
162 sRefs.remove(this); | 161 sRefs.remove(this); |
163 if (mCleanupTask != null) { | 162 if (mCleanupTask != null) { |
164 if (DEBUG) Log.i(TAG, "--- CLEANING ONE REF"); | 163 if (DEBUG) Log.i(TAG, "--- CLEANING ONE REF"); |
165 mCleanupTask.run(); | 164 mCleanupTask.run(); |
166 mCleanupTask = null; | 165 mCleanupTask = null; |
167 } | 166 } |
168 clear(); | 167 clear(); |
169 } | 168 } |
170 } | 169 } |
OLD | NEW |