I know that it is not recomended to use system.exit(0) in an Android application, but I was wondering if it would be ok to use it in onDestroy() since the application is allowed to be killed at that point?
The reason I am asking is in relation to this question.
onDestroywill be called. There are situations where the OS will kill your application without calling youronDestroy.Just a word of advice, of course, as it does not directly apply to your scenario. If
system.exit(0)was not called because the app is already killed, I’d say you’re OK. 😉Activity.onDestroyis called, that means your process is in one of two states:— there’s no other component (
Serviceor aContentProvider) running in the app that the OS is aware of. This means that your process is most likely going to be killed by the OS immediately anyway, or will be the first one to be reclaimed if other parts of the system/other apps need physical memory. Thus callingexit(0)will achieve not much.— there’s another component running in your process that the system is aware of. Calling
exit()in this case would terminate the process, killing your other component and potentially corrupting your data. The OS could care less of course, but your users might not appreciate it. 🙂The Dalvik heap limit is OEM configurable, though very few OEMs actually do make the effort to tune it up for their devices, and just go with the OS defaults. I don’t remember the specific defaults, but it’s safe to assume each app is allowed between 16MB on low-end Froyo/Gingerbread and 48MB on high-end ICS/JB phones. Heck, I’d be optimistic and bump the upper limit to 128MB (though I am yet to hear of a such a device) . 🙂
ContentProvideror aService, you can’t afford callingexit()anymore (as I mentioned above), and you will be forced to solve the problem properly. You might as well just bite the bullet and do it now. The simplest approach would be to ensure you are callingBitmap.recycle()when you’re done with the particular bitmap. Of course, that works as long as you don’t have to keep more bitmaps in memory than you have memory, but that’s a totally separate beast altogether anyway. 🙂