Releasing memory from activity of foreground service

佐手、 提交于 2021-01-28 00:11:25

问题


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?


回答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




回答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.



来源:https://stackoverflow.com/questions/22538533/releasing-memory-from-activity-of-foreground-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!