1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
package com.bumptech.glide.resize.bitmap_recycle;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND;
import static android.content.ComponentCallbacks2.TRIM_MEMORY_MODERATE;
import android.graphics.Bitmap;
import android.util.Log;
import android.os.Build;
public class LruBitmapPool implements BitmapPool {
private static final String TAG = "LruBitmapPool";
private final LruPoolStrategy strategy;
private final int maxSize;
private int currentSize = 0;
private int hits;
private int misses;
private int puts;
private int evictions;
// Exposed for testing only.
LruBitmapPool(int maxSize, LruPoolStrategy strategy) {
this.maxSize = maxSize;
this.strategy = strategy;
}
public LruBitmapPool(int maxSize) {
this.maxSize = maxSize;
if (Build.VERSION.SDK_INT >= 19) {
strategy = new SizeStrategy();
} else {
strategy = new AttributeStrategy();
}
}
@Override
public synchronized boolean put(Bitmap bitmap) {
if (!bitmap.isMutable() || strategy.getSize(bitmap) > maxSize) {
return false;
}
final int size = strategy.getSize(bitmap);
strategy.put(bitmap);
puts++;
currentSize += size;
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Put bitmap in pool=" + strategy.logBitmap(bitmap));
}
dump();
evict();
return true;
}
private void evict() {
trimToSize(maxSize);
}
@Override
public synchronized Bitmap get(int width, int height, Bitmap.Config config) {
final Bitmap result = strategy.get(width, height, config);
if (result == null) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Missing bitmap=" + strategy.logBitmap(width, height, config));
}
misses++;
} else {
hits++;
currentSize -= strategy.getSize(result);
}
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Get bitmap=" + strategy.logBitmap(width, height, config));
}
dump();
return result;
}
@Override
public void clearMemory() {
trimToSize(0);
}
@Override
public void trimMemory(int level) {
if (level >= TRIM_MEMORY_MODERATE) {
clearMemory();
} else if (level >= TRIM_MEMORY_BACKGROUND) {
trimToSize(maxSize / 2);
}
}
private void trimToSize(int size) {
while (currentSize > size) {
final Bitmap removed = strategy.removeLast();
currentSize -= strategy.getSize(removed);
removed.recycle();
evictions++;
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Evicting bitmap=" + strategy.logBitmap(removed));
}
dump();
}
}
private void dump() {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Hits=" + hits + " misses=" + misses + " puts=" + puts + " evictions=" + evictions + " currentSize="
+ currentSize + " maxSize=" + maxSize + "\nStrategy=" + strategy);
}
}
}
|