Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between System.exit() and killProcess()

Tags:

android

What's the difference if I call System.exit() vs. killProcess().
I am interested in difference only

like image 559
CommonMan Avatar asked Sep 30 '11 00:09

CommonMan


3 Answers

I dont think There is any difference. although with System.exit(), you should call runFinalizersOnExit first

like image 112
user973931 Avatar answered Nov 19 '22 19:11

user973931


What should we use?

No one, read this Is quitting an application frowned upon?

like image 38
aromero Avatar answered Nov 19 '22 18:11

aromero


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.

like image 2
Jesse Gordon Avatar answered Nov 19 '22 17:11

Jesse Gordon