mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 12:23:58 +01:00
bug 50733: Fix OOM error on low-memory devices
LRU bitmap cache ate too much memory on low-memory devices like the old Nexus One and some of our testers' devices. Use the in memory cache only for higher end devices Change-Id: Ibe238c82d5891a8a25bddcab1ae3d3738a044c41
This commit is contained in:
parent
c5042b0066
commit
3400d6e426
1 changed files with 30 additions and 17 deletions
|
|
@ -15,6 +15,7 @@ import android.net.Uri;
|
|||
import android.os.Build;
|
||||
|
||||
import android.support.v4.util.LruCache;
|
||||
import android.util.Log;
|
||||
import com.android.volley.RequestQueue;
|
||||
import com.nostra13.universalimageloader.cache.disc.impl.TotalSizeLimitedDiscCache;
|
||||
import com.nostra13.universalimageloader.cache.memory.impl.LimitedAgeMemoryCache;
|
||||
|
|
@ -112,29 +113,41 @@ public class CommonsApplication extends Application {
|
|||
// Initialize EventLogging
|
||||
EventLog.setApp(this);
|
||||
|
||||
|
||||
// based off https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
|
||||
// Cache for 1/8th of available VM memory
|
||||
long maxMem = Runtime.getRuntime().maxMemory();
|
||||
if (maxMem < 48L * 1024L * 1024L) {
|
||||
// Cache only one bitmap if VM memory is too small (such as Nexus One);
|
||||
Log.d("Commons", "Skipping bitmap cache; max mem is: " + maxMem);
|
||||
imageCache = new LruCache<String, Bitmap>(1);
|
||||
} else {
|
||||
int cacheSize = (int) (maxMem / (1024 * 8));
|
||||
Log.d("Commons", "Bitmap cache size " + cacheSize + " from max mem " + maxMem);
|
||||
imageCache = new LruCache<String, Bitmap>(cacheSize) {
|
||||
@Override
|
||||
protected int sizeOf(String key, Bitmap bitmap) {
|
||||
// bitmap.getByteCount() not available on older androids
|
||||
int bitmapSize;
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
|
||||
bitmapSize = bitmap.getRowBytes() * bitmap.getHeight();
|
||||
} else {
|
||||
bitmapSize = bitmap.getByteCount();
|
||||
}
|
||||
// The cache size will be measured in kilobytes rather than
|
||||
// number of items.
|
||||
return bitmapSize / 1024;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
DiskBasedCache cache = new DiskBasedCache(getCacheDir(), 16 * 1024 * 1024);
|
||||
volleyQueue = new RequestQueue(cache, new BasicNetwork(new HurlStack()));
|
||||
volleyQueue.start();
|
||||
}
|
||||
|
||||
private com.android.volley.toolbox.ImageLoader imageLoader;
|
||||
// based off https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
|
||||
// Cache for 1/8th of available VM memory
|
||||
private LruCache<String, Bitmap> imageCache = new LruCache<String, Bitmap>((int) (Runtime.getRuntime().maxMemory() / (1024 * 8))) {
|
||||
@Override
|
||||
protected int sizeOf(String key, Bitmap bitmap) {
|
||||
// bitmap.getByteCount() not available on older androids
|
||||
int bitmapSize;
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
|
||||
bitmapSize = bitmap.getRowBytes() * bitmap.getHeight();
|
||||
} else {
|
||||
bitmapSize = bitmap.getByteCount();
|
||||
}
|
||||
// The cache size will be measured in kilobytes rather than
|
||||
// number of items.
|
||||
return bitmapSize / 1024;
|
||||
}
|
||||
};
|
||||
private LruCache<String, Bitmap> imageCache;
|
||||
|
||||
public com.android.volley.toolbox.ImageLoader getImageLoader() {
|
||||
if(imageLoader == null) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue