Skip to content Skip to sidebar Skip to footer

Releasing Memory From Activity Of Foreground Service

I have an app with a foreground service and one activity. The service can either start on its own on boot, or be started from within the activity. I have noticed when the service s

Solution 1:

It turns out Android will not release the activity from memory until the entire process is restarted. Since it is a foreground service, its process is almost never killed, so this doesn't happen.

The solution is to run the service and activity in their own processes by specifying:

android:process="name"

in AndroidManifest.xml

Solution 2:

You can terminate the process using System.exit(0). It will be restarted with only the foreground running and hence using less memory. However, your foreground service will cease to run for a short amount of time before it is restarted by the system.

However, just because this can be done does not mean that it should be done. When android needs memory, it will automatically do the same thing. So you should not worry about the increased memory consumption. Memory will be reclaimed when needed.

Post a Comment for "Releasing Memory From Activity Of Foreground Service"