Why is calling Process.killProcess(Process.myPid()) a bad idea?

<rant>

In a perfect world, with perfect code and libraries, you shouldn't need to call Process.killProcess(Process.myPid()) and the OS will correctly kill your application as appropriate. Also there will be peace in the Middle East, pigs will fly, and the halting problem will be solved.

Because all of these things haven't happened yet there are times when you need to execute such 'forbidden' code.

Most recently for an Android game I made, the free version used an Ad library which would keep the application alive and also leak memory. The paid version didn't have this problem as there were no ad libraries linked. My solution was to add a Quit button on the main menu that executed such code. My hopes were that the majority of people would hit this button when done and I don't have to worry about it eating up memory. The paid version I just executed finish() and was done. (This was before In-app purchases for Google were available, so I had to make a paid and free version, also they may have fixed the issue by now and I could update said game but it really didn't do too well and I doubt any time spent on it would be worth it)

Its kind of like in elementary/middle school they tell you that you can't take the square root of a negative number. Then later in a higher level algebra class they say... well you can take the square root of a negative number but you get weird results but it's consistent and solves the problem.

In other words, don't execute the 'forbidden' code unless you know what you're doing.

</rant>


Well, Unit3d is most probably using native code, and they are killing the process as an insurance -- they don't want to leak memory. You could argue whether this is a good idea or not, but the fact that they used it does not mean that you should too.

Maybe there are some extreme cases where you would want to use killProcess(), but usually the OS does this for you, according to current load and usage. Not sure what kind of an answer you are looking for -- you are aware that using killProcess() might break things, unless you can justify its usage, don't use it.


Who said calling Process.killProcess(Process.myPid()) is a bad idea?

Yes, letting the OS manage its own memory is the best practice for both you and the user using your application (faster to open again, less chances for force closes, etc...).

However, assuming you know for sure that you're not interrupting threads or other background operations and you use this call in onDestroy() - I see no reason why you shouldn't use it. Especially when it's an API call and not a workaround, and Google didn't mention it's better not to use it in the API documentation.