Reputation: 1564
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.
Upvotes: 2
Views: 3904
Reputation: 76001
onDestroy
will be called. There are situations where the OS will kill your application without calling your onDestroy
.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.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. :-)
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) . :-)
ContentProvider
or a Service
, you can't afford calling exit()
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 calling Bitmap.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. :-)Upvotes: 4
Reputation: 1996
Short cut solution: Call finish() instead of System.exit(0) on activity when you are done with your app.
proper way is to optimize bitmap usage in your app. See this
http://developer.android.com/training/displaying-bitmaps/index.html
Upvotes: 1
Reputation: 6319
You'd probably better take a look at options like: http://developer.android.com/reference/android/app/Application.html#onLowMemory%28%29
Upvotes: 0
Reputation: 3322
You should not call sytem.exit(0), let android manage applications. If I understand you have memory leak problem. I guess because of bitmaps. So have a look at this for efficiently displaying bitmaps http://developer.android.com/training/displaying-bitmaps/index.html
If you call system.exit(0) in onDestroy() method, you won't be able to flip between landscape and portrait.
Upvotes: 3