Issue
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.
Solution
- Do not rely that
onDestroy
will 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. ;-)
- If your
Activity.onDestroy
is called, that means your process is in one of two states:
-- there's no other component (Service
or a ContentProvider
) 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 calling exit(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. :-)
- Killing voluntarily your process will not help other applications, if they have exhausted their internal Dalvik heap limit. No matter how much physical memory a device has, the OS has a limit on how much memory Dalvik is allowed to use in any process for heap allocations. Thus, it is possible that the system has free half of its memory and a particular application still hits OOM.
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) . :-)
- If your application has problems with its memory usage (implied by the linked question) and you are trying to solve it by forcing a "clean" slate on the next opening of the activity, this approach would work. Kind of. But it's going to work for as long as your application is using activities only. The moment you throw in a
ContentProvider
or 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. :-)
Answered By - Franci Penov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.