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

Side by Side Diff: base/android/java/src/org/chromium/base/ReferencePool.java

Issue 2865963003: [Suggestions UI] Drop Bitmap references from articles under memory pressure. (Closed)
Patch Set: review Created 3 years, 7 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 | « base/BUILD.gn ('k') | chrome/android/java/src/org/chromium/chrome/browser/ChromeActivity.java » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.base;
6
7 import android.support.annotation.Nullable;
8
9 import java.lang.ref.WeakReference;
10 import java.util.WeakHashMap;
11
12 /**
13 * A ReferencePool allows handing out typed references to objects ("payloads") t hat can be dropped
14 * in one batch ("drained"), e.g. under memory pressure. In contrast to {@link W eakReference}s,
15 * which drop their referents when they get garbage collected, a reference pool gives more precise
16 * control over when exactly it is drained.
17 *
18 * Internally it uses a {@link WeakHashMap} with the reference itself as a key t o allow the payloads
19 * to be garbage collected regularly when the last reference goes away before th e pool is drained.
20 */
21 public class ReferencePool {
22 /**
23 * The underlying data storage. The wildcard type parameter allows using a s ingle pool for
24 * references of any type.
25 */
26 private final WeakHashMap<Reference<?>, Object> mPool = new WeakHashMap<>();
27
28 /**
29 * A reference to an object in the pool. Will be nulled out when the pool ha s been drained.
30 * @param <T> The type of the object.
31 */
32 public class Reference<T> {
33 /**
34 * @return The referent, or null if the pool has been drained.
35 */
36 @SuppressWarnings("unchecked")
37 @Nullable
38 public T get() {
39 return (T) mPool.get(this);
40 }
41 }
42
43 /**
44 * @param <T> The type of the object.
45 * @param payload The payload to add to the pool.
46 * @return A new reference to the {@code payload}.
47 */
48 public <T> Reference<T> put(T payload) {
49 assert payload != null;
50 Reference<T> reference = new Reference<>();
51 mPool.put(reference, payload);
Michael van Ouwerkerk 2017/05/09 11:35:36 As only the keys are weakly referenced by WeakHash
Bernhard Bauer 2017/05/09 12:37:17 As discussed offline, the WeakHashMap usually is t
52 return reference;
53 }
54
55 /**
56 * Drains the pool, removing all references to objects in the pool and there fore allowing them
57 * to be garbage collected.
58 */
59 public void drain() {
60 mPool.clear();
61 }
62 }
OLDNEW
« no previous file with comments | « base/BUILD.gn ('k') | chrome/android/java/src/org/chromium/chrome/browser/ChromeActivity.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698