Gangadhar Nimballi Gangadhar Nimballi - 9 days ago 6
Android Question

FATAL EXCEPTION: Thread-18 java.lang.OutOfMemoryError: in android volley with lower version devices

I am getting frequently OOM with volley library in lower versions of android devices.

I am using BitmapLruCache

public class BitmapLruCache extends LruCache<String, Bitmap> implements ImageCache {

public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;

return cacheSize;
}

public BitmapLruCache() {
this(getDefaultLruCacheSize());
}

public BitmapLruCache(int sizeInKiloBytes) {
super(sizeInKiloBytes);
}

@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight() / 1024;
}

@Override
public Bitmap getBitmap(String url) {
return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}


}

Your suggestions will be appreciated. Thank You.

03-11 12:24:02.768: E/AndroidRuntime(18454): FATAL EXCEPTION: Thread-18
03-11 12:24:02.768: E/AndroidRuntime(18454): java.lang.OutOfMemoryError: (Heap Size=19463KB, Allocated=14480KB, Bitmap Size=10107KB)
03-11 12:24:02.768: E/AndroidRuntime(18454): at com.android.volley.toolbox.ByteArrayPool.getBuf(ByteArrayPool.java:101)
03-11 12:24:02.768: E/AndroidRuntime(18454): at com.android.volley.toolbox.PoolingByteArrayOutputStream.<init>(PoolingByteArrayOutputStream.java:53)
03-11 12:24:02.768: E/AndroidRuntime(18454): at com.android.volley.toolbox.BasicNetwork.entityToBytes(BasicNetwork.java:203)
03-11 12:24:02.768: E/AndroidRuntime(18454): at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:104)
03-11 12:24:02.768: E/AndroidRuntime(18454): at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)

Answer

It was getting Out Of Memory exception when I was trying to download 10 images with size 2 MB.

As volley itself says that It can not download or upload large amount of data.

Here, I fixed OOM by re-sizing images to lower size as possible as i want in server.

Example: Actually I need images with sizes 400x200 but It was with sizes 1500x1600 in server, so I made images with sizes 400x200 in server and my list view is easily downloading images from server.