android.view.ViewRoot$RunQueue$HandlerAction 阻止 Dialog 被垃圾收集

发布于 2024-12-09 05:28:57 字数 542 浏览 0 评论 0原文

我目前正在构建一个仅在平板电脑上运行的 Android 应用程序。 一项功能是在(自定义创建的)对话框中显示事件列表。

在某些情况下,当用户确认这些事件时,对话框不会被垃圾收集,并且在多次打开对话框后,保存在内存中的对话框实例的数量不断增加。

由于该应用程序要运行很长时间,我想这会导致内存问题。

因此,我进行了内存转储并启动了 Eclipse 内存分析器 (MAT)。 我以前从未遇到过此类问题,而且我对 MAT 不太熟悉,但我的假设是:

我可以看到我的 Dialog 的多个实例和一些“内部类”(MyDialog$1) 仍然存在。对于所有其他“内部类”(MyDialog$2、MyDialog$3,...),计数为 0。

使用“合并到 GC 根的最短路径”和“包含所有引用”选项会导致我 android.view.ViewRoot$RunQueue$HandlerAction,所以我的假设是,以某种方式保留了我的听众之一的引用?

我希望你能告诉我我的假设(以及我的分析方式)是否正确。 希望您能给我一个解决方案或提示如何解决这个问题。

提前致谢 斯文

I am currently building an android application that will be run only on tablets.
One feature is displaying a list of events in a (custom created) dialog.

In some cases, when the user acknowledges these events, the dialog is not beeing garbage collected, and after opening the dialog several times, the number of instances of the dialog that are kept in memory is increasing and increasing.

Since the app is to be run for a long time I guess this will lead to memory issues.

So I took a memory dump and fired up the Eclipse Memory Analyzer (MAT).
I never had this kind of problem before and I am not too familiar with MAT but here is what I assume:

I can see that there are several instances of my Dialog and some "inner class" (MyDialog$1) still there. For all other "inner classes" (MyDialog$2, MyDialog$3, ...) the count is 0.

Using "Merge shortest paths to GC Roots" with the option "with all references" leads me to
android.view.ViewRoot$RunQueue$HandlerAction, so my assumption is that somehow there is a reference of one of my listeners kept?

I hope you can tell me if my assumption (and my way of analyzing) is correct.
And hopefully you can give me a solution or hint on how to solve this.

Thanks in advance
Sven

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两仪 2024-12-16 05:28:57

我认为你的假设是正确的。作为匿名类的侦听器将具有对封闭类的引用。当您希望销毁对话框时,您应该取消注册侦听器。

或者,您可以考虑使用 API 来处理对话框的生命周期。已弃用的 Activity.showDialog< /a> 和 Activity.removeDialog 应该在删除对话框时删除对对话框的所有引用。

在 Android 3.0 及更高版本中,您可以使用 DialogFragment 而不是 Dialog,并且 FragmentManager 应该为您处理它的生命周期。请参阅Fragments 开发人员指南。

I think that you are correct in your assumption. A listener that is an anonymous class will have a reference to the enclosing class. You should unregister the listeners when you are want the dialog destroyed.

Alternatively, you might consider using an API to handle the lifecycle of the Dialog. The deprecated Activity.showDialog and Activity.removeDialog should remove all references to the dialog when it is removed.

In Android 3.0 and above you can use the DialogFragment instead of Dialog, and the FragmentManager should handle it's lifecycle for you. See the Fragments developer guide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文