I am developing an application where user sells or buys used items. In my first activity, I have grid view where I show thumbnail images and when user clicks on an image and then it takes him to detail activity. I use back button to go back to main activity page. It works fine but crashes if I click on 8-9 images and getting out of memory(OOM).
CustomVolleyRequest Class
public class CustomVolleyRequest {
private static CustomVolleyRequest customVolleyRequest;
private static Context context;
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private CustomVolleyRequest(Context context) {
this.context = context;
this.requestQueue = getRequestQueue();
imageLoader = new ImageLoader(requestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized CustomVolleyRequest getInstance(Context context) {
if (customVolleyRequest == null) {
customVolleyRequest = new CustomVolleyRequest(context);
}
return customVolleyRequest;
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
Cache cache = new DiskBasedCache(context.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
requestQueue = new RequestQueue(cache, network);
requestQueue.start();
}
return requestQueue;
}
public ImageLoader getImageLoader() {
return imageLoader;
}
}
Memory Monitoring, each click increments, it does free the memory when I go back to main activity!
Stack Trace:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask E/AndroidRuntime:
Process: PID: 5700
java.lang.RuntimeException: An error occured while executing
doInBackground() E/AndroidRuntime: at
android.os.AsyncTask$3.done(AsyncTask.java:300) E/AndroidRuntime: at
java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
E/AndroidRuntime: at
java.util.concurrent.FutureTask.setException(FutureTask.java:222)
E/AndroidRuntime: at
java.util.concurrent.FutureTask.run(FutureTask.java:242)
E/AndroidRuntime: at
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime: at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime: at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime: at java.lang.Thread.run(Thread.java:841)
E/AndroidRuntime: Caused by: java.lang.OutOfMemoryError
E/AndroidRuntime: at
android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
E/AndroidRuntime: at
android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:613)
E/AndroidRuntime: at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:589)
E/AndroidRuntime: at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:627)
E/AndroidRuntime: at
com.example.xxx.xxx.DownloadImageTask.doInBackground(DownloadImageTask.java:26)
E/AndroidRuntime: at
com.example.xxx.xxx.DownloadImageTask.doInBackground(DownloadImageTask.java:14)
E/AndroidRuntime: at
android.os.AsyncTask$2.call(AsyncTask.java:288)
E/AndroidRuntime: at
java.util.concurrent.FutureTask.run(FutureTask.java:237)
E/AndroidRuntime: at
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
E/AndroidRuntime: at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
E/AndroidRuntime: at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
E/AndroidRuntime: at java.lang.Thread.run(Thread.java:841)