1

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!

enter image description here

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)
casillas
  • 16,351
  • 19
  • 115
  • 215
  • share the code please – Muhammad Younas Jan 12 '16 at 06:12
  • I have shared the code – casillas Jan 12 '16 at 06:12
  • 1
    What I understand from your log is that you are downloading images and showing them in an `ImageView` and the crash happens because they are too large in size and the app cannot handle it. Try resizing them to lower resolutions before setting them to `ImageView`s – camelCaseCoder Jan 12 '16 at 06:13
  • I resized them in the server already. – casillas Jan 12 '16 at 06:15
  • 1
    _"Caused by: java.lang.OutOfMemoryError"_ Either add memory or reduce the size of the image you are loading. – Jim Garrison Jan 12 '16 at 06:20
  • Resizing them at client side really helps. Have a look at this answer: http://stackoverflow.com/a/10127787/5352802 – camelCaseCoder Jan 12 '16 at 06:20
  • @JimGarrison, I have just added my `CustomVolley Request` Class as well in my question. Do you see anything wrong over there? How could I increase memory ? – casillas Jan 12 '16 at 06:24
  • Well I have never used Volley so can't say about that. The answer I gave you the link to explains how to downscale an image based on it's original resolution so as to it doesn't throw `OutOfMemoryException` while settings those images to `ImageView`s – camelCaseCoder Jan 12 '16 at 06:27
  • But it does not happen for certain image, it happens randomly after I click 8-9 images. It seems it is caching clicked images somewhere and then becomes out of memory error. When user click on back button , I guess it stores all the visited pages, I believe I should clear them up. Do not you think so? – casillas Jan 12 '16 at 06:31
  • Please see my memory plot in my updated question, it clearly indicates each time I go back it does not free up the memory. It stays over there – casillas Jan 12 '16 at 06:40

1 Answers1

2

As per the logs, look at this line:

E/AndroidRuntime:  Caused by: java.lang.OutOfMemoryError

It clearly says that app is crashing just because of OutOfMemoryError exception. One possible reason might be you are handling large size bitmaps without downscaling them. You need to resize and downscale your bitmap before setting them to image views.

For more details on how to manage bitmap effectively, see official docs

Geeky Singh
  • 1,003
  • 9
  • 20
  • But it does not happen for certain image, it happens randomly after I click 8-9 images. It seems it is caching clicked images somewhere and then throws out of memory error. When I click on back button , I guess it stores all the visited pages, I believe I should clear them up. Do not you think so? If you agree, how I should clear them up? – casillas Jan 12 '16 at 06:34
  • It totally depends upon how much available RAM you have to handle your images. If you have enough memory available, you would not get any crash. Still suggesting you to downscale bitmaps before processing them as there are high probability of getting crash on low-end devices. – Geeky Singh Jan 12 '16 at 06:36
  • Please see my memory plot in my updated question, it clearly indicates each time I go back it does not free up the memory. It stays over there. – casillas Jan 12 '16 at 06:40
  • As you can see, memory keeps growing on and at time 1m5sec, GC called to free-up some memory. – Geeky Singh Jan 12 '16 at 06:44
  • As I already mentioned, you need to downscale your images before processing them. Or, if you just wanna show images in image views, you can simply use image processing libraries like Picasso, Glide etc. Just define the image size (optional) and rest of the thing will be done by the library itself. – Geeky Singh Jan 12 '16 at 06:51
  • I am using Volley library. – casillas Jan 12 '16 at 07:03
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/100440/discussion-between-gagan-singh-and-texas). – Geeky Singh Jan 12 '16 at 07:06