Paolo
Paolo

Reputation: 2472

Android service in same process and memory release

I have a Service, which is foreground. It is locally bound to one (or more) activity, so the activity and the service runs in the same process. There might be some time during which the activity is not displayed (typical music player use case).

If service and activity are in the same process, the memory for the activity can't be repossessed by operating system, unless the service too is terminated? The answer seems yes to me, according to this article

If the last is true, should I create two separate process:

Or, it is not a big deal to have the activity sticking around together with the service?

Upvotes: 2

Views: 1231

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007658

I have a Service, which is foreground. It is locally bound to one (or more) activity, so the activity and the service runs in the same process. There might be some time during which the activity is not displayed (typical music player use case).

A "typical music player use case" would not use bindService() IMHO. A foreground service would not use bindService() IMHO. At minimum, in addition to bindService() for activity->service communication, you would need startService(), so that after you unbindService() (e.g., user presses BACK), the service can stay running.

If service and activity are in the same process, the memory for the activity can't be repossessed by operating system, unless the service too is terminated?

The memory for the activity is never "repossessed by the operating system", except by terminating your entire process. Unfortunately, this is not entirely clear from the documentation.

If the user pressed BACK to exit the activity, or you otherwise call finish() on the activity, the activity should be garbage-collected, assuming nothing is causing it to hang around (e.g., referred to by a static data member).

should I create two separate process

Absolutely not.

Upvotes: 3

Related Questions