質問

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 starts itself on boot, the memory usage is ~3mB. Once I open the activity, memory usage jumps up to about ~9mB. Once the activity has been opened (and then closed either by back or home button), the memory never drops back down to 3mB, even after many hours and other apps opening.

My question is, should the memory from the activity be released or is this expected with a foreground service?

役に立ちましたか?

解決

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

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top