Skip to content

Commit 525e7ba

Browse files
sjuddglide-copybara-robot
authored andcommitted
Clear the Bitmap Pool when the UI is hidden on M+.
PiperOrigin-RevId: 259615959
1 parent 71359c7 commit 525e7ba

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPool.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.annotation.SuppressLint;
44
import android.annotation.TargetApi;
5+
import android.content.ComponentCallbacks2;
56
import android.graphics.Bitmap;
67
import android.graphics.Color;
78
import android.os.Build;
@@ -224,10 +225,12 @@ public void trimMemory(int level) {
224225
if (Log.isLoggable(TAG, Log.DEBUG)) {
225226
Log.d(TAG, "trimMemory, level=" + level);
226227
}
227-
if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) {
228+
if ((level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND)
229+
|| ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
230+
&& (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN))) {
228231
clearMemory();
229-
} else if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN
230-
|| level == android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) {
232+
} else if ((level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN)
233+
|| (level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL)) {
231234
trimToSize(getMaxSize() / 2);
232235
}
233236
}

library/test/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPoolTest.java

+13-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import static org.mockito.Mockito.when;
1515

1616
import android.graphics.Bitmap;
17+
import android.os.Build;
1718
import java.util.ArrayDeque;
1819
import java.util.ArrayList;
1920
import java.util.Collections;
@@ -98,25 +99,32 @@ public void testEvictedBitmapsAreRecycled() {
9899
}
99100
}
100101

102+
@Config(sdk = Build.VERSION_CODES.KITKAT)
101103
@Test
102-
public void testTrimMemoryUiHiddenOrLessRemovesHalfOfBitmaps() {
104+
public void testTrimMemoryUiHiddenOrLessRemovesHalfOfBitmaps_preM() {
103105
testTrimMemory(MAX_SIZE, TRIM_MEMORY_UI_HIDDEN, MAX_SIZE / 2);
104106
}
105107

108+
@Config(sdk = Build.VERSION_CODES.M)
109+
@Test
110+
public void testTrimMemoryUiHiddenOrLessRemovesHalfOfBitmaps_postM() {
111+
testTrimMemory(MAX_SIZE, TRIM_MEMORY_UI_HIDDEN, 0);
112+
}
113+
106114
@Test
107115
public void testTrimMemoryRunningCriticalRemovesHalfOfBitmaps() {
108116
testTrimMemory(MAX_SIZE, TRIM_MEMORY_RUNNING_CRITICAL, MAX_SIZE / 2);
109117
}
110118

111119
@Test
112-
public void testTrimMemoryUiHiddenOrLessRemovesNoBitmapsIfPoolLessThanHalfFull() {
113-
testTrimMemory(MAX_SIZE / 2, TRIM_MEMORY_UI_HIDDEN, 0);
120+
public void testTrimMemoryRunningCriticalOrLessRemovesNoBitmapsIfPoolLessThanHalfFull() {
121+
testTrimMemory(MAX_SIZE / 2, TRIM_MEMORY_RUNNING_CRITICAL, MAX_SIZE / 2);
114122
}
115123

116124
@Test
117125
public void testTrimMemoryBackgroundOrGreaterRemovesAllBitmaps() {
118126
for (int trimLevel : new int[] {TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_COMPLETE}) {
119-
testTrimMemory(MAX_SIZE, trimLevel, MAX_SIZE);
127+
testTrimMemory(MAX_SIZE, trimLevel, 0);
120128
}
121129
}
122130

@@ -161,7 +169,7 @@ private void testTrimMemory(int fillSize, int trimLevel, int expectedSize) {
161169
LruBitmapPool pool = new LruBitmapPool(MAX_SIZE, strategy, ALLOWED_CONFIGS);
162170
fillPool(pool, fillSize);
163171
pool.trimMemory(trimLevel);
164-
assertEquals("Failed level=" + trimLevel, expectedSize, strategy.numRemoves);
172+
assertEquals("Failed level=" + trimLevel, expectedSize, strategy.bitmaps.size());
165173
}
166174

167175
@Test

0 commit comments

Comments
 (0)