Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: content/public/android/java/src/org/chromium/content/common/CleanupReference.java

Issue 2255253002: Revert of android: Switch CleanupReference to PhantomReference (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698