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

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: alternative approach 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.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>Note that certain kinds of reference cycles that would be garbage-collecte d when using weak
DmitrySkiba 2017/05/10 22:04:19 This is not accurate anymore, right? Since now the
Bernhard Bauer 2017/05/11 08:43:02 True. Removed (and added a comment about thread sa
24 * (or even strong) references could lead to leaks with a DiscardableReferencePo ol: For example, if
25 * a {@link DiscardableReference} is held by its own payload (including indirect ly) but is otherwise
26 * not referenced, it will not get garbage-collected, because the DiscardableRef erencePool will
27 * still hold a strong reference to the payload.
28 */
29 public class DiscardableReferencePool {
30 /**
31 * The underlying data storage. The wildcard type parameter allows using a s ingle pool for
32 * references of any type.
33 */
34 private final Set<DiscardableReference<?>> mPool;
35
36 public DiscardableReferencePool() {
37 WeakHashMap<DiscardableReference<?>, Boolean> map = new WeakHashMap<>();
38 mPool = Collections.newSetFromMap(map);
39 }
40
41 /**
42 * A reference to an object in the pool. Will be nulled out when the pool is drained.
43 * @param <T> The type of the object.
44 */
45 public static class DiscardableReference<T> {
46 @Nullable
47 private T mPayload;
48
49 private DiscardableReference(T payload) {
50 assert payload != null;
51 mPayload = payload;
52 }
53
54 /**
55 * @return The referent, or null if the pool has been drained.
56 */
57 @Nullable
58 public T get() {
59 return mPayload;
60 }
61
62 /**
63 * Clear the referent.
64 */
65 private void discard() {
66 assert mPayload != null;
67 mPayload = null;
68 }
69 }
70
71 /**
72 * @param <T> The type of the object.
73 * @param payload The payload to add to the pool.
74 * @return A new reference to the {@code payload}.
75 */
76 public <T> DiscardableReference<T> put(T payload) {
77 assert payload != null;
78 DiscardableReference<T> reference = new DiscardableReference<>(payload);
79 mPool.add(reference);
80 return reference;
81 }
82
83 /**
84 * Drains the pool, removing all references to objects in the pool and there fore allowing them
85 * to be garbage collected.
86 */
87 public void drain() {
88 for (DiscardableReference<?> ref : mPool) {
89 ref.discard();
90 }
91 mPool.clear();
92 }
93 }
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