Skip to content

Commit

Permalink
Evict oldest half of cache on TRIM_MEMORY_RUNNING_CRITICAL (#2889)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ygnessin authored and sjudd committed Feb 13, 2018
1 parent c042d11 commit d939314
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d939314

Please sign in to comment.