From d939314e04b26709e7e890ba8f88494551111e23 Mon Sep 17 00:00:00 2001 From: Yuval Gnessin Date: Tue, 13 Feb 2018 07:09:58 -0800 Subject: [PATCH] Evict oldest half of cache on TRIM_MEMORY_RUNNING_CRITICAL (#2889) * LruResourceCache: Evict oldest half of cache on TRIM_MEMORY_RUNNING_CRITICAL * LruBitmapPool: Evict oldest half of cache on TRIM_MEMORY_RUNNING_CRITICAL * LruArrayPool: Evict oldest half of cache on TRIM_MEMORY_RUNNING_CRITICAL --- .../glide/load/engine/bitmap_recycle/LruArrayPool.java | 3 ++- .../load/engine/bitmap_recycle/LruBitmapPool.java | 5 +++-- .../glide/load/engine/cache/LruResourceCache.java | 8 +++++--- .../load/engine/bitmap_recycle/LruArrayPoolTest.java | 6 ++++++ .../load/engine/bitmap_recycle/LruBitmapPoolTest.java | 6 ++++++ .../glide/load/engine/cache/LruResourceCacheTest.java | 10 ++++++++++ 6 files changed, 32 insertions(+), 6 deletions(-) diff --git a/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruArrayPool.java b/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruArrayPool.java index f6838dd269..596e155748 100644 --- a/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruArrayPool.java +++ b/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruArrayPool.java @@ -140,7 +140,8 @@ public synchronized void clearMemory() { public synchronized void trimMemory(int level) { if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) { clearMemory(); - } else if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { + } else if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN + || level == android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) { evictToSize(maxSize / 2); } } diff --git a/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPool.java b/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPool.java index 0540e469e3..d233c9130a 100644 --- a/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPool.java +++ b/library/src/main/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPool.java @@ -219,8 +219,9 @@ public void trimMemory(int level) { } if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) { clearMemory(); - } else if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { - trimToSize(maxSize / 2); + } else if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN + || level == android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) { + trimToSize(getMaxSize() / 2); } } diff --git a/library/src/main/java/com/bumptech/glide/load/engine/cache/LruResourceCache.java b/library/src/main/java/com/bumptech/glide/load/engine/cache/LruResourceCache.java index 9c26b83dff..75c5329e10 100644 --- a/library/src/main/java/com/bumptech/glide/load/engine/cache/LruResourceCache.java +++ b/library/src/main/java/com/bumptech/glide/load/engine/cache/LruResourceCache.java @@ -47,11 +47,13 @@ protected int getSize(@Nullable Resource item) { @Override public void trimMemory(int level) { if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) { - // Nearing middle of list of cached background apps + // Entering list of cached background apps // Evict our entire bitmap cache clearMemory(); - } else if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) { - // Entering list of cached background apps + } else if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN + || level == android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL) { + // The app's UI is no longer visible, or app is in the foreground but system is running + // critically low on memory // Evict oldest half of our bitmap cache trimToSize(getMaxSize() / 2); } diff --git a/library/test/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruArrayPoolTest.java b/library/test/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruArrayPoolTest.java index 1c037b2f50..6b7be90f69 100644 --- a/library/test/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruArrayPoolTest.java +++ b/library/test/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruArrayPoolTest.java @@ -2,6 +2,7 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; import static android.content.ComponentCallbacks2.TRIM_MEMORY_COMPLETE; +import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; import static android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; @@ -74,6 +75,11 @@ public void testTrimMemoryUiHiddenOrLessRemovesHalfOfArrays() { testTrimMemory(MAX_SIZE, TRIM_MEMORY_UI_HIDDEN, MAX_SIZE / 2); } + @Test + public void testTrimMemoryRunningCriticalRemovesHalfOfBitmaps() { + testTrimMemory(MAX_SIZE, TRIM_MEMORY_RUNNING_CRITICAL, MAX_SIZE / 2); + } + @Test public void testTrimMemoryUiHiddenOrLessRemovesNoArraysIfPoolLessThanHalfFull() { testTrimMemory(MAX_SIZE / 2, TRIM_MEMORY_UI_HIDDEN, MAX_SIZE / 2); diff --git a/library/test/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPoolTest.java b/library/test/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPoolTest.java index 0b452c214e..e199738c97 100644 --- a/library/test/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPoolTest.java +++ b/library/test/src/test/java/com/bumptech/glide/load/engine/bitmap_recycle/LruBitmapPoolTest.java @@ -2,6 +2,7 @@ import static android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; import static android.content.ComponentCallbacks2.TRIM_MEMORY_COMPLETE; +import static android.content.ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL; import static android.content.ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; @@ -102,6 +103,11 @@ public void testTrimMemoryUiHiddenOrLessRemovesHalfOfBitmaps() { testTrimMemory(MAX_SIZE, TRIM_MEMORY_UI_HIDDEN, MAX_SIZE / 2); } + @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); diff --git a/library/test/src/test/java/com/bumptech/glide/load/engine/cache/LruResourceCacheTest.java b/library/test/src/test/java/com/bumptech/glide/load/engine/cache/LruResourceCacheTest.java index 8d8e43b1fd..22b76cfc1f 100644 --- a/library/test/src/test/java/com/bumptech/glide/load/engine/cache/LruResourceCacheTest.java +++ b/library/test/src/test/java/com/bumptech/glide/load/engine/cache/LruResourceCacheTest.java @@ -105,6 +105,16 @@ public void testTrimMemoryUiHidden() { verify(harness.listener, never()).onResourceRemoved(harness.second); } + @Test + public void testTrimMemoryRunningCritical() { + TrimClearMemoryCacheHarness harness = new TrimClearMemoryCacheHarness(); + + harness.resourceCache.trimMemory(ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL); + + verify(harness.listener).onResourceRemoved(harness.first); + verify(harness.listener, never()).onResourceRemoved(harness.second); + } + @Test public void testResourceRemovedListenerIsNotifiedWhenResourceIsRemoved() { LruResourceCache resourceCache = new LruResourceCache(100);