Skip to content

Commit

Permalink
Clear the Bitmap Pool when the UI is hidden on M+.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 259615959
  • Loading branch information
sjudd authored and glide-copybara-robot committed Jul 23, 2019
1 parent 71359c7 commit 525e7ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ComponentCallbacks2;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
Expand Down Expand Up @@ -224,10 +225,12 @@ public void trimMemory(int level) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "trimMemory, level=" + level);
}
if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) {
if ((level >= ComponentCallbacks2.TRIM_MEMORY_BACKGROUND)
|| ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
&& (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN))) {
clearMemory();
} else if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN
|| level == android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) {
} else if ((level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN)
|| (level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL)) {
trimToSize(getMaxSize() / 2);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.mockito.Mockito.when;

import android.graphics.Bitmap;
import android.os.Build;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -98,25 +99,32 @@ public void testEvictedBitmapsAreRecycled() {
}
}

@Config(sdk = Build.VERSION_CODES.KITKAT)
@Test
public void testTrimMemoryUiHiddenOrLessRemovesHalfOfBitmaps() {
public void testTrimMemoryUiHiddenOrLessRemovesHalfOfBitmaps_preM() {
testTrimMemory(MAX_SIZE, TRIM_MEMORY_UI_HIDDEN, MAX_SIZE / 2);
}

@Config(sdk = Build.VERSION_CODES.M)
@Test
public void testTrimMemoryUiHiddenOrLessRemovesHalfOfBitmaps_postM() {
testTrimMemory(MAX_SIZE, TRIM_MEMORY_UI_HIDDEN, 0);
}

@Test
public void testTrimMemoryRunningCriticalRemovesHalfOfBitmaps() {
testTrimMemory(MAX_SIZE, TRIM_MEMORY_RUNNING_CRITICAL, MAX_SIZE / 2);
}

@Test
public void testTrimMemoryUiHiddenOrLessRemovesNoBitmapsIfPoolLessThanHalfFull() {
testTrimMemory(MAX_SIZE / 2, TRIM_MEMORY_UI_HIDDEN, 0);
public void testTrimMemoryRunningCriticalOrLessRemovesNoBitmapsIfPoolLessThanHalfFull() {
testTrimMemory(MAX_SIZE / 2, TRIM_MEMORY_RUNNING_CRITICAL, MAX_SIZE / 2);
}

@Test
public void testTrimMemoryBackgroundOrGreaterRemovesAllBitmaps() {
for (int trimLevel : new int[] {TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_COMPLETE}) {
testTrimMemory(MAX_SIZE, trimLevel, MAX_SIZE);
testTrimMemory(MAX_SIZE, trimLevel, 0);
}
}

Expand Down Expand Up @@ -161,7 +169,7 @@ private void testTrimMemory(int fillSize, int trimLevel, int expectedSize) {
LruBitmapPool pool = new LruBitmapPool(MAX_SIZE, strategy, ALLOWED_CONFIGS);
fillPool(pool, fillSize);
pool.trimMemory(trimLevel);
assertEquals("Failed level=" + trimLevel, expectedSize, strategy.numRemoves);
assertEquals("Failed level=" + trimLevel, expectedSize, strategy.bitmaps.size());
}

@Test
Expand Down

0 comments on commit 525e7ba

Please sign in to comment.