0

I am getting random out of memory exceptions in my app caused by inflate exceptions.

I have 7 fragment (difficulty) activities which launch their own activities via buttons. Each fragment has a scroll view with 30 buttons (levels). I have set it up so that i can swipe across to each fragment and the fragment takes up the entire screen.

Occasionally when i swipe a few times and then select a random button from a group of 30 it will crash. It tries to load the activity and gives an out of memory exception with an inflate exception on a random line. The line always falls on an imageView or imageButton in the xml file. The activities that load are a grid of imageViews and imageButtons.

I do not get the exception much but it is something i want to fix. I have looked at many other out of memory exception questions although none have helped me. I have done a Memory analyser test and it shows nothing out of the ordinary.

I believe that the imageViews and imageButtons are using too much memory, although i only ever have one activity open at once.

RhysBailey21
  • 117
  • 3
  • 13

3 Answers3

2

It IS because of your images that are loading. When you load an image and you move around the page and view another image the heap increases. As you continue the process of viewing random images the heap grows even more until your app crashes. It's like stacking books on a glass table. You either move(cache) a book(image) or the glass(app) breaks. You should use an imageloader to load your images.

https://github.com/nostra13/Android-Universal-Image-Loader

SmulianJulian
  • 801
  • 11
  • 33
  • I see what you mean. Very good description. Although, wouldn't it just be better to 'refresh' every change in activity? – RhysBailey21 May 14 '15 at 06:58
  • No because YOUR doing the work instead of imageloader. Less work is good. I have seen 0 out of memory errors with image loader. I like to move on to other more dynamic aspects of what I am building, maybe you like the work. :) – SmulianJulian May 14 '15 at 08:13
1

You've mentioned that it always falls on an ImageView and ImageButton - and this is the clue to solve this problem. You get OOM 'cause background resource of this view has high resolution and takes a lot of memory. Try to lower resolution of this image.

Also you've mentioned that you have a ScrollView and this means that you keep in memory every 30 items. Probably you'd better change it to RecyclerView backed by adapter.

Oleg Osipenko
  • 2,409
  • 1
  • 20
  • 28
  • So changing the images to a lower resolution worked.... sort of. The crash is almost non-existent unless i really try to cause it. Which most users wont push it to the limit i am. Although, i want it completely gone. The scrollView is used because i want my app accessible to android 3.0 or higher. Using recyclerView will move that up to android 5.0. Is their a way i can reduce the scrollView's memory demand without using recyclerView? – RhysBailey21 May 14 '15 at 06:57
  • RecyclerView is a part of Support library that is accessible for APIs starting from SDK 7 and higher, that is mean you have backwards compatibility in case of using RecyclerView. Also there were number of Adapter-backed views such as ListView, GridView etc. since the SDK v1. So that's not the point for preferring ScrollView over RecyclerView – Oleg Osipenko May 14 '15 at 07:04
0

Just had the same problem and I'd like to simplify all the things said here:

Simply: reduce your images sizes.

Don't use 1080X1920 images... It's too high res.

Such image, even if compressed, when deployed will catch about 1080X1920X4B = 8.2MB (The GPU has to deploy it to it's full original resolution... That's why compression won't help but reducing the needed memory size...) and this i RAM that we're talking about..

Take Gimp or Photoshop and down scale the image to, say, 1/4: 540X960, and you won't feel the difference.. Belive me, been there already.

Beware of the memory consumption of images and videos.

Hope this helps,

James

JamesC
  • 356
  • 4
  • 8