Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 3    Views: 148

AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
09/16/15 10:22 AM (8 years ago)

Android, images, menu buttons memory leak

I have been looking at a problem with android, as I have found my apps crash on certain devices. It has stopped me developing anything but the most basic Android apps; I am starting to think it is something in the menus. I have a simple test app, with two menus. The home page is a menu simple (no images), which loads a menu buttons with 12 buttons (each with an image). I can navigate backwards and forwards between the tw menus until my app crashes. This happens really quickly on my Samsung Galaxy 2, but taks much longer on my tablet. I added some code to the onCreate in the menus (found on stackoverflow http://stackoverflow.com/questions/3170691/how-to-get-current-memory-usage-in-android) to capture memory usage: final Runtime runtime = Runtime.getRuntime(); final long freeMemInMB=(runtime.freeMemory()) / 1048576L; The button image is 200px square and about 3MB in size. I've found that if I use images as URLs in the button menu, there is a memory leak of about 2 MB on each cycle of navigating back and forth. On my Samsung, I found the max memory available for an app is 48 Mb, so it does not take long until until it crashes. My other test device shows the max memory available for an app is 192Mb, appearing to be a lot more stable because it takes a lot longer to get to the crashing point. This explains why some devices seem more stable for apps than others. If I use local images, memory usage doesn't significantly change (i.e. no leaks) and my test does not crash. This explains why we all try to avoid using URL images in Android, but in certain scenario's, it is useful. I have seen the same effect on other menus that use images. I think the leak is something to do with caching bitmaps after downloading, but I'm running out of knowledge. I'm interested if any Android gurus can suggest where to look or can add anything to help us get to the bottom of this? Alan
 
Rachelle-Joanne
Aspiring developer
Profile
Posts: 65
Reg: Jan 15, 2013
Lagos
650
like
09/24/15 05:03 PM (8 years ago)
hello Alen for some reason I too have been facing this issue and saw all what you explained on stackoverflow. But Also saw this quick fix, though from what I read it's not the best but it solved the crashing issue. It was said to use as very very last resort. Add android: largeHeap="true" to the application tag in your manifest file.
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
09/26/15 04:26 AM (8 years ago)
Thanks for the suggestion, I have tried that. In my experience, it delayed the problem, but did not solve it. I believe the problem has something to do with memory management of bitmap images that are loaded remotely.
 
LeonG
Apple Fan
Profile
Posts: 694
Reg: Nov 08, 2011
Hamburg
17,740
like
09/29/15 05:50 AM (8 years ago)
thx for the heap thing, that solved my problem!
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.