@SuppressLint("InlinedApi") @Override public void trimMemory(int level) { if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) { // Entering list of cached background apps // Evict our entire bitmap cache clearMemory(); } 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); } } }
@Test public void clearMemory_afterPutWithExistingItem_evictsOnlyNewItem() { PutWithExistingEntryHarness harness = new PutWithExistingEntryHarness(); harness.cache.put(harness.key, harness.first); harness.cache.put(harness.key, harness.second); verify(harness.listener).onResourceRemoved(harness.first); verify(harness.listener, never()).onResourceRemoved(harness.second); harness.cache.clearMemory(); verify(harness.listener, times(1)).onResourceRemoved(harness.first); verify(harness.listener).onResourceRemoved(harness.second); }
@SuppressLint("InlinedApi") @Override public void trimMemory(int level) { if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_MODERATE) { // Nearing middle of list of cached background apps // Evict our entire bitmap cache clearMemory(); } else if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) { // Entering list of cached background apps // Evict oldest half of our bitmap cache trimToSize(getCurrentSize() / 2); } } }
@SuppressLint("InlinedApi") @Override public void trimMemory(int level) { if (level >= android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND) { // Nearing middle of 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 // Evict oldest half of our bitmap cache trimToSize(getCurrentSize() / 2); } } }