Skip to content

Commit

Permalink
Add a clearMemory method to ImageManager and BitmapPool
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Nov 22, 2013
1 parent db3e9b8 commit 6244968
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 5 deletions.
5 changes: 5 additions & 0 deletions library/src/com/bumptech/glide/resize/ImageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,11 @@ public void releaseBitmap(final Bitmap b) {
bitmapReferenceCounter.releaseBitmap(b);
}

public void clearMemory() {
memoryCache.clearMemory();
bitmapPool.clearMemory();
}

/**
* Shuts down all of the background threads used by the ImageManager including the executor service
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
public interface BitmapPool {
public boolean put(Bitmap bitmap);
public Bitmap get(int width, int height, Bitmap.Config config);
public void clearMemory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public Bitmap get(int width, int height, Bitmap.Config config) {
return null;
}

@Override
public void clearMemory() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ public synchronized boolean put(Bitmap bitmap) {
}

private void evict() {
while (currentSize > maxSize) {
final Bitmap removed = pool.removeLast();
currentSize -= getSize(removed);
removed.recycle();
}
trimToSize(maxSize);
}

@Override
Expand All @@ -52,6 +48,19 @@ public synchronized Bitmap get(int width, int height, Bitmap.Config config) {
return result;
}

@Override
public void clearMemory() {
trimToSize(0);
}

private void trimToSize(int size) {
while (currentSize > size) {
final Bitmap removed = pool.removeLast();
currentSize -= getSize(removed);
removed.recycle();
}
}

private static int getSize(Bitmap bitmap) {
return bitmap.getHeight() * bitmap.getRowBytes();
}
Expand Down
43 changes: 43 additions & 0 deletions library/tests/src/com/bumptech/glide/ImageManagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.bumptech.glide;

import android.graphics.Bitmap;
import android.test.AndroidTestCase;
import com.bumptech.glide.resize.ImageManager;
import com.bumptech.glide.resize.bitmap_recycle.BitmapPool;
import com.bumptech.glide.resize.bitmap_recycle.BitmapPoolAdapter;
import com.bumptech.glide.resize.cache.MemoryCache;
import com.bumptech.glide.resize.cache.MemoryCacheAdapter;

import java.util.concurrent.atomic.AtomicInteger;

public class ImageManagerTest extends AndroidTestCase {

public void testClearMemory() {
final AtomicInteger clearsCalled = new AtomicInteger();
// The pool's clear method must be called after the cache's so that the bitmaps from the cache do not just
// refill the pool
BitmapPool bitmapPool = new BitmapPoolAdapter() {
@Override
public void clearMemory() {
assertEquals(2, clearsCalled.incrementAndGet());
}
};
MemoryCache memoryCache = new MemoryCacheAdapter() {
@Override
public void clearMemory() {
super.clearMemory();
assertEquals(1, clearsCalled.incrementAndGet());
}
};

ImageManager im = new ImageManager.Builder(getContext())
.setBitmapPool(bitmapPool)
.setMemoryCache(memoryCache)
.build();

im.clearMemory();

assertEquals(2, clearsCalled.get());
}
}

59 changes: 59 additions & 0 deletions library/tests/src/com/bumptech/glide/LruBitmapPoolTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.bumptech.glide;

import android.graphics.Bitmap;
import android.test.AndroidTestCase;
import com.bumptech.glide.resize.bitmap_recycle.LruBitmapPool;

import java.util.ArrayList;
import java.util.List;

public class LruBitmapPoolTest extends AndroidTestCase {
private static final int SIZE = 1024 * 1024;
private LruBitmapPool pool;

@Override
protected void setUp() throws Exception {
super.setUp();
pool = new LruBitmapPool(SIZE);
}

public void testClearMemoryRemovesAllBitmaps() {
List<Bitmap> bitmaps = fillPool();
assertTrue(bitmaps.size() >= 2);

Bitmap first = bitmaps.get(0);
assertNotNull(pool.get(first.getWidth(), first.getHeight(), first.getConfig()));
pool.clearMemory();
assertNull(pool.get(first.getWidth(), first.getHeight(), first.getConfig()));
}

public void testClearMemoryCallsRecycleOnRemovedBitmaps() {
List<Bitmap> bitmaps = fillPool();
pool.clearMemory();
for (Bitmap bitmap : bitmaps) {
assertTrue(bitmap.isRecycled());
}
}

public List<Bitmap> fillPool() {
List<Bitmap> bitmaps = new ArrayList<Bitmap>();
Bitmap toPut = getBitmap();
int bitmapSize = getSize(toPut);
for (int i = 0; i < (SIZE / bitmapSize); i++) {
bitmaps.add(Bitmap.createBitmap(toPut));
}
for (Bitmap bitmap : bitmaps) {
pool.put(bitmap);
}
assertTrue(bitmaps.size() > 0);
return bitmaps;
}

private static int getSize(Bitmap bitmap) {
return bitmap.getRowBytes() * bitmap.getHeight();
}

private static Bitmap getBitmap() {
return Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
}
}

0 comments on commit 6244968

Please sign in to comment.