What's the difference if I call System.exit()
vs. killProcess()
.
I am interested in difference only
4 Answers
It looks like System.exit() is just as good in every respect as kill -- but much simpler and less dependent on other things.
Some have suggested that runFinalizersOnExit be set but according to the docs that is considered unsafe and is phased out as of 1.0 -- so I guess ignore that part.
Contrary to other suggestions, finish() does not end the Linux process which is running the app and does not free up all the memory used by the app.
Granted, android is designed so that for many cases there's no particular need to actually exit an app (At the cost of a slight pause later, android will kill your old apps when it needs their memory) -- however if you do want for any reason to kill your app System.exit() seems to be the idea way. It shuts down the java virtual machine which is running your app - so all resources, memory, and threads will be completely flushed out.
(Note that you can specify in your manifest file that some threads should run in different linux processes -- in which case System.exit() would probably only kill part of your app - but that's more advanced stuff.)
As a matter of fact, I just ran adb shell ps|grep app
and I see the com.example.android.lunarlander sample app which I haven't run in about a week -- still in memory, still taking up almost 100000 bytes of memory.

- 1,455
- 15
- 16
What should we use?
No one, read this Is quitting an application frowned upon?
-
9Let the dev do what he's going to do. If he makes a bad program no one will use it or else I'm sure the dev is doing the right thing. Just don't put in a non-answers to the question, use comments instead. – MinceMan Jun 30 '12 at 02:26
-
1Also, sometimes it's not up to the dev, but someone higher-up demanding it – Joe Plante Nov 05 '13 at 15:27
I dont think There is any difference. although with System.exit(), you should call runFinalizersOnExit first

- 515
- 1
- 4
- 13
-
2'runFinalizersOnExit' is unsafe: http://developer.android.com/reference/java/lang/Runtime.html#runFinalizersOnExit(boolean) – Budda Nov 26 '12 at 21:33