Android Out Of Memory Exceptions
I have an application I am writing that is heavy with bitmaps. If you start the app, exit the app and come back into the app several times eventually you get a forced close because of an out of memory condition.开发者_C百科
I know where the out of memory is happening and could do a try catch. The problem is that if I don't have the memory to run, what do I do? My app just doesn't work without the bitmaps, so I can't just not show them.
I have tried setting things to null when I get the onPause. I have tried doing finish in onPause. I did a lot of research an read many postings before writing this email and have tried the many things suggested.
The bottom line is that the memory I use just doesn't seem to be getting freed or something else is going on.
I tried using the adb shell cat /proc/meminfo command in between starts and stops and here is what I found:
MemTotal: 31348
Start app, everything working
MemTotal: 18180
Exit app
MemTotal: 20160
Start app, everything working
MemTotal: 12480
Exit app
MemTotal: 13740
Start app, everything working
MemTotal: 9600
Exit app
MemTotal: 10844
Start app, first memory exception, one of 5 bitmaps just doesn't show up, but no crash
MemTotal: 6224
Exit app
MemTotal: 7244
Start app, doesn't start, gets out of memory which causes an error that I catch, so exits
MemTotal: 7836
Start app, out of memory exception and Force Close, hit force close button
MemTotal: 31648
As you can see the memory is now finally released, but only after a force close.
It doesn't matter if I start and exit really fast or wait between each start and finish the same thing happens, so I can't just introduce a delay.
Is there a way to "force" a Force Close except not really show the message and not be an error condition? I thought that is what finish() was supposed to do, but it doesn't.
Thanks for any help or ideas I can try.
We had the same problem with our app. Unfortunately there is no 'silver bullet' to solve all of memory issues. Here are a couple of things you can try:
i) Use soft reference/ weak reference for the bitmaps you use.
ii) Recycle bitmaps in your onPause and decode them again in your onResume. A cache of soft referenced bitmaps will help.
iii) Consider using RGB_565 instead of ARGB_8888 (which is now the default in Gingerbread).
iv) Make sure that there are no leaks or multiple instances of your activity/activities.
Hope this helps.
精彩评论