从前台服务活动中释放内存 [英] Releasing memory from activity of foreground service

查看:74
本文介绍了从前台服务活动中释放内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有前台服务和一项活动的应用程序.该服务可以在启动时自行启动,也可以从活动中启动.我已经注意到,当服务在启动时自行启动时,内存使用量约为3mB.打开活动后,内存使用量将跃升至约9mB.一旦活动被打开(然后通过后退"或主页"按钮关闭),即使打开多个小时和其他应用程序,内存也永远不会降到3mB.

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?

推荐答案

事实证明,直到重新启动整个过程,Android才会从内存中释放活动.由于它是前台服务,因此它的进程几乎永远不会被杀死,因此这不会发生.

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"

在AndroidManifest.xml中

in AndroidManifest.xml

这篇关于从前台服务活动中释放内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆