前台服务保留内存对象 [英] Foreground Service retains memory objects

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

问题描述

我的Android应用程序本质上是一个前台服务.该应用程序的用户界面方面是用于此前台服务的设置和配置.

My Android application is essentially a foreground service. The User Interface aspect of the application is for settings and configuration of this foreground service.

通过应用程序UI启动该服务时,它开始显示23mb的内存使用情况.

When the service is started via the application UI, it begins showing memory usage of 23mb.

当Android启动服务(例如在启动时)时,它将开始显示7mb的内存使用情况.如果打开应用程序用户界面,则内存使用量将增加到23mb.

When Android starts the service (at boot for example) it begins showing memory usage of 7mb. If the application UI is opened, the memory usage increases to 23mb.

在UI的MainActivity中调用onDestroy()时,关联的可绘制对象和资源保留在附加到前台服务的内存中,并且不会收集垃圾.

When onDestroy() is called in the MainActivity of the UI, the associated drawables and resources remain in the memory attached to the foreground service and do not get garbage collected.

我可以通过在MainActivity的onDestroy()中调用System.exit(0)来解决"该问题.然后,Android在7mb处重新启动前台服务.

I can 'resolve' the problem by calling System.exit(0) in onDestroy() of the MainActivity. Android then restarts the foreground service at 7mb.

此外,我可以在清单中使用android:process标记来分隔UI和服务元素.

Additionally, I can use the android:process tag in the Manifest to separate the UI and the service elements.

以上两种解决方案"均未解决实际问题,希望您能提供帮助!

Neither of the above 'solutions' is addressing the actual issue though, which I hope to do with your help!

通过Eclipse使用MAT工具,我可以看到内存中保留了各种对象,只有两个重要的对象是:

Using the MAT tool via Eclipse, I can see that various objects are retained in the memory, the only two of significant size being:

Class Name | Shallow Heap | Retained Heap Percentage

class android.content.res.Resources @ 0x40ef7910 System Class|           40 |     8,475,664 |     68.71%
android.graphics.drawable.BitmapDrawable$BitmapState @ 0x411beb20|       40 |     1,127,720 |     9.14%

检查gc根目录的路径并不能提供线索,为什么它们仍然保留在内存中,或者是什么导致对它们的引用.

Examining the path to gc roots gives no clues as to why these are remaining in the memory, or what is holding a reference to them.

我已经尝试过使用MainActivity.thisgetApplicationContext()的上下文,并删除了对静态变量的引用,但我只是无法摆脱这些内存对象.

I've experimented with the contexts I'm using MainActivity.this and getApplicationContext() and removed references to static variables, but I just cannot get rid of these memory objects.

任何帮助将不胜感激.

gc根的路径,不包括弱引用:

Class Name | Shallow Heap | Retained Heap

class android.content.res.Resources @ 0x40ef7910 System Class|     40 |     8,475,664

Class Name      | Shallow Heap Retained Heap

android.graphics.drawable.BitmapDrawable$BitmapState @ 0x411beb20|  40 | 1,127,720

Class Name                              | Shallow Heap | Retained Heap**

[224] java.lang.Object[510] @ 0x41843738|        2,056 |     8,466,904

mValues android.util.LongSparseArray @ 0x40f46b28|  24 |     8,471,024

sPreloadedDrawables class android.content.res.Resources @ 0x40ef7910 SystemClass|          40 |     8,475,664

Found 1 path. No more paths left - for both entries.

推荐答案

我的Android应用程序本质上是一个前台服务.该应用程序的用户界面方面是用于此前台服务的设置和配置.

My Android application is essentially a foreground service. The User Interface aspect of the application is for settings and configuration of this foreground service.

这适用于数量很少的应用程序.

This is appropriate for a vanishingly small number of apps.

检查gc根目录的路径并不能提供线索,为什么它们仍然保留在内存中,或者是什么导致对它们的引用.

Examining the path to gc roots gives no clues as to why these are remaining in the memory, or what is holding a reference to them.

在每个Dalvik进程中,至少有一个Resources,并且不能删除. BitmapDrawable$BitmapState很可能与Resources绑定.

The Resources one, at minimum, is in every Dalvik process and cannot be removed. Most likely the BitmapDrawable$BitmapState is tied to the Resources.

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

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