1

According to my previous post, and some research online, I found out that the sytem might be playing the music even after the service's onDestroy was called because it was holding on to some resources like the MediaPlayer, so I decided to actually kill the process instead of calling the stopForeground or stopSelf, because they simply are not working. However, I synced all the application states and called System.exit(0) and it kills the foreground process as well as the activity as intended.

The only bit of confusion I had if System.exit(0) killed the process and also freed the app resources? That is, does it release every bit of resource held by the app? I am worried about it because I don't need to call MediaPlayer.release() and stopForeground() and other similar cleanup methods, just one System.exit() does the trick.

It's okay to call this for my app, because it triggers the behaviour I intend, but, I wonder if its okay for the android system, because lingering memories would be a bad thing. Thanks in advance, any insight would be helpful.

juztcode
  • 1,196
  • 2
  • 21
  • 46
  • Typically it should free up the resources but you should be on the lookout for unintended side-effects. Besides, what's wrong with calling `MediaPlayer.release()` in service's `onDestroy()` and using `stopSelf()`? It's generally considered a bad practice to fight the framework. – ashu Nov 02 '20 at 07:16
  • @ashu , stopSelf() doesn't release it's components, and manual work for MediaPlayer.release() is required, I am not sure on how that would be trustworthy, so I think we need to call System.exit(0) anyway just to be on the safe side, check my link in the question, I've described how the music keeps playing even though the service that held it is destroyed. – juztcode Nov 02 '20 at 07:35
  • 1
    That's the intended behaviour. Media Player resources need to be freed up manually when not in use. Stopping a service doesn't stop any threads that it might've started during its execution. Manual clean up is always required and it's the safest approach since a programmer will generally be aware of how their resources needs to handle cleanup. https://stackoverflow.com/a/37792887 – ashu Nov 02 '20 at 07:41
  • @ashu , then, can we somehow set the `MediaPlayer` to be a `daemon`? – juztcode Nov 02 '20 at 07:44
  • AFAIK a Service + MediaPlayer implementation is equivalent to a daemon. – ashu Nov 02 '20 at 07:48
  • I don't see anything in the documentation, but why not just use all those class? Or even use https://developer.android.com/reference/java/lang/System#runFinalization() Which seems to also be useful in your case: *Calling this method suggests that the Java Virtual Machine expend effort toward running the finalize methods of objects that have been found to be discarded but whose finalize methods have not yet been run. When control returns from the method call, the Java Virtual Machine has made a best effort to complete all outstanding finalizations.* – Dan Baruch Nov 02 '20 at 07:50
  • @ashu, "AFAIK a Service + MediaPlayer implementation is equivalent to a daemon. " , but if it were a daemon then it should have stopped when the service had ended, i.e. after the onDestroy() method get called – juztcode Nov 02 '20 at 08:30
  • 1
    @juztcode I am not sure what you mean when you say daemon. I don't think daemon has anything to do with issue at hand. https://en.m.wikipedia.org/wiki/Daemon_(computing) – ashu Nov 02 '20 at 09:21
  • in code lingo, a daemon thread would be a thread that exits automatically when the parent thread ends – juztcode Nov 02 '20 at 11:14

1 Answers1

1

does System.exit(int) kill the process and clear up all the resources?

Yes.

if System.exit(0) killed the process and also freed the app resources?

No. You should release resources gracefully when the app is closed.

Consider this situation:

System camera > System camera service <(server side|system level)---(app level|client side)> APP camera interface < APP uses camera resources.

System.exit() only release client side resource, but camera isn't release, it's not properly.

尹劍平
  • 184
  • 1
  • 1
  • 5
  • oh, I see, I get the point, yet it's hard to understand what you did up with the angle brackets, do you mind translating them please? Also, do you know of a similar function/routine in android/java that would release any(system or client) side resources taken by the app? – juztcode Nov 02 '20 at 08:47