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

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

Issue 2865963003: [Suggestions UI] Drop Bitmap references from articles under memory pressure. (Closed)
Patch Set: remove annotation 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
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.util.Collections;
10 import java.util.Set;
11 import java.util.WeakHashMap;
12
13 /**
14 * A DiscardableReferencePool allows handing out typed references to objects ("p ayloads") that can
15 * be dropped in one batch ("drained"), e.g. under memory pressure. In contrast to {@link
16 * java.lang.ref.WeakReference}s, which drop their referents when they get garba ge collected, a
17 * reference pool gives more precise control over when exactly it is drained.
18 *
19 * <p>Internally it uses a {@link WeakHashMap} with the reference itself as a ke y to allow the
20 * payloads to be garbage collected regularly when the last reference goes away before the pool is
21 * drained.
22 *
23 * <p>This class and its references are not thread-safe and should not be used s imultaneously by
24 * multiple threads.
25 */
26 public class DiscardableReferencePool {
27 /**
28 * The underlying data storage. The wildcard type parameter allows using a s ingle pool for
29 * references of any type.
30 */
31 private final Set<DiscardableReference<?>> mPool;
32
33 public DiscardableReferencePool() {
34 WeakHashMap<DiscardableReference<?>, Boolean> map = new WeakHashMap<>();
35 mPool = Collections.newSetFromMap(map);
36 }
37
38 /**
39 * A reference to an object in the pool. Will be nulled out when the pool is drained.
40 * @param <T> The type of the object.
41 */
42 public static class DiscardableReference<T> {
43 @Nullable
44 private T mPayload;
45
46 private DiscardableReference(T payload) {
47 assert payload != null;
48 mPayload = payload;
49 }
50
51 /**
52 * @return The referent, or null if the pool has been drained.
53 */
54 @Nullable
55 public T get() {
56 return mPayload;
57 }
58
59 /**
60 * Clear the referent.
61 */
62 private void discard() {
63 assert mPayload != null;
64 mPayload = null;
65 }
66 }
67
68 /**
69 * @param <T> The type of the object.
70 * @param payload The payload to add to the pool.
71 * @return A new reference to the {@code payload}.
72 */
73 public <T> DiscardableReference<T> put(T payload) {
74 assert payload != null;
75 DiscardableReference<T> reference = new DiscardableReference<>(payload);
76 mPool.add(reference);
77 return reference;
78 }
79
80 /**
81 * Drains the pool, removing all references to objects in the pool and there fore allowing them
82 * to be garbage collected.
83 */
84 public void drain() {
85 for (DiscardableReference<?> ref : mPool) {
86 ref.discard();
87 }
88 mPool.clear();
89 }
90 }
OLDNEW
« no previous file with comments | « base/BUILD.gn ('k') | base/android/junit/src/org/chromium/base/DiscardableReferencePoolTest.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698