In my application I keep receiving crash reports where the root cause is an OutOfMemoryError.

The app is quite image heavy (I use Picasso for all the image loading) and also has a WebView component that is used to display all kinds of content from text and images to embedded videos and animated GIFs.

I used Square's LeakCanary in some places but was unable to find consistent leaks. With my understanding of Android's runtimes being quite incomplete, this leads me to my main question:

Does an OutOfMemory always need to have a leak as the root cause or is it possible/probable that one occurs for other reasons? Which other reasons would that be? The garbage collector working not fast enough? Simply using too much heap space for (e.g.) Bitmaps? Is there anything that can be done about these problems?

EDIT: To make it more clear what I need to know: I am quite certain that no single allocation I am doing is exceeding the total available heap space. It is just that occasionally I seem to get too much space used up so that some random allocation after that fails; at least the crashes are actually quite rare and not easily reproducable. Is that a reasonably certain sign of an earlier leak or is it still possible that I just have "too much going on" at once and there really is nothing the GC could have freed to make the current allocation work?END EDIT

If you think I should keep looking for memory leaks: Apart from Activitys, which LeakCanary tracks automatically what are some good places to look for leaks?

  • Consider the trivial case: Device has 256MB of memory and you try to load an image into memory which is 257MB - you'll get OOM with no leak.
    – Daenyth
    Jul 1 '15 at 15:07
  • Yes, of course. But my app is far from that trivial case: It actually crashes very rarely and the crashes are not easily reproducable. When it does crash the OOMs occur in various different places of the code. And it happens on all kinds of devices, including newer ones. Jul 5 '15 at 7:27
  • I was responding to Does an OutOfMemory always need to have a leak as the root cause or is it possible/probable that one occurs for other reasons? Which other reasons would that be?
    – Daenyth
    Jul 5 '15 at 18:23
  • Yes, and sorry if I sounded dismissive - it is just that now that it said it, of course my question was somewhat naive. I am editing my question to make it more clear what I was after... but thanks regardless! Jul 5 '15 at 18:31

Though compressed for storage, images can take up much more space internally as they are mapped into the display memory. Combine this with the wide variation in device capacities among Android devices, and you're likely to hit the memory limits of the less capable devices.

If possible, you can try to keep track of images that are not currently in the visible screen area and unload them. You would reload them as needed as they get scrolled into view (or get close to it). The WebView may or may not do this for you.

  • Thank you for your reply! That is why I mentioned that I am using Picasso. I think - in combination with RecyclerViews - this should handle the unloading of Bitmap data and reusing of ImageViews? Jul 5 '15 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.