diff --git a/library/src/com/bumptech/glide/resize/cache/LruMemoryCache.java b/library/src/com/bumptech/glide/resize/cache/LruMemoryCache.java index 5980904482..5bb048eb5c 100644 --- a/library/src/com/bumptech/glide/resize/cache/LruMemoryCache.java +++ b/library/src/com/bumptech/glide/resize/cache/LruMemoryCache.java @@ -4,9 +4,13 @@ package com.bumptech.glide.resize.cache; -import android.graphics.Bitmap; +import static android.content.ComponentCallbacks2.TRIM_MEMORY_BACKGROUND; +import static android.content.ComponentCallbacks2.TRIM_MEMORY_MODERATE; +import android.graphics.Bitmap; +import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.Map; /** */ @@ -47,9 +51,33 @@ public void setImageRemovedListener(ImageRemovedListener listener) { this.imageRemovedListener = listener; } - private void evict() { - LinkedHashMap.Entry last; - while (currentSize > maxSize) { + @Override + public void clearMemory() { + final Iterator> iterator = cache.entrySet().iterator(); + while (iterator.hasNext()) { + final Bitmap bitmap = iterator.next().getValue(); + bitmap.recycle(); + iterator.remove(); + } + currentSize = 0; + } + + @Override + public void trimMemory(int level) { + if (level >= TRIM_MEMORY_MODERATE) { + // Nearing middle of list of cached background apps + // Evict our entire bitmap cache + clearMemory(); + } else if (level >= TRIM_MEMORY_BACKGROUND) { + // Entering list of cached background apps + // Evict oldest half of our bitmap cache + trimToSize(currentSize / 2); + } + } + + private void trimToSize(int size) { + Map.Entry last; + while (currentSize > size) { last = cache.entrySet().iterator().next(); final Bitmap toRemove = last.getValue(); currentSize -= getSize(toRemove); @@ -60,4 +88,8 @@ private void evict() { } } } + + private void evict() { + trimToSize(maxSize); + } } diff --git a/library/src/com/bumptech/glide/resize/cache/MemoryCache.java b/library/src/com/bumptech/glide/resize/cache/MemoryCache.java index 403cb6c529..09aaf519fe 100644 --- a/library/src/com/bumptech/glide/resize/cache/MemoryCache.java +++ b/library/src/com/bumptech/glide/resize/cache/MemoryCache.java @@ -1,5 +1,6 @@ package com.bumptech.glide.resize.cache; +import android.content.ComponentCallbacks2; import android.graphics.Bitmap; /** @@ -40,4 +41,15 @@ public interface ImageRemovedListener { * @param listener The listener */ public void setImageRemovedListener(ImageRemovedListener listener); + + /** + * Evict all items from the memory cache. + */ + public void clearMemory(); + + /** + * Trim the memory cache to the appropriate level. Typically called on the callback onTrimMemory. + * @param level This integer represents a trim level as specified in {@link ComponentCallbacks2} + */ + public void trimMemory(int level); } diff --git a/library/src/com/bumptech/glide/resize/cache/MemoryCacheAdapter.java b/library/src/com/bumptech/glide/resize/cache/MemoryCacheAdapter.java index 7ca832da29..43d2356823 100644 --- a/library/src/com/bumptech/glide/resize/cache/MemoryCacheAdapter.java +++ b/library/src/com/bumptech/glide/resize/cache/MemoryCacheAdapter.java @@ -26,4 +26,10 @@ public Bitmap put(String key, Bitmap bitmap) { public void setImageRemovedListener(ImageRemovedListener listener) { this.listener = listener; } + + @Override + public void clearMemory() {} + + @Override + public void trimMemory(int level) {} }