gpt4 book ai didi

android - 为什么在使用主页按钮离开时我的 Activity 会导致内存泄漏?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:31:55 24 4
gpt4 key购买 nike

每当我使用 Android 手机上的主页按钮离开我的应用程序并使用启动器图标再次进入该应用程序时,我的应用程序的初始 Activity 就会重复。如果我重复该例程五次,则我的初始 Activity 有五个实例。

我通过 Eclipse 中的堆转储对此进行了监控,确保在每次转储之前进行垃圾收集。这是我在直方图 View 中请求 Merge Shortes Path to GC Root with all references 时得到的 View :

Class Name                                                                     | Ref. Objects | Shallow Heap | Ref. Shallow Heap | Retained Heap
-------------------------------------------------------------------------------------------------------------------------------------------------
| | | |
android.view.ViewRootImpl$WindowInputEventReceiver @ 0x41408a80 Native Stack | 1 | 32 | 224 | 184
class android.view.inputmethod.InputMethodManager @ 0x40d93e78 System Class | 1 | 88 | 224 | 616
'- mInstance android.view.inputmethod.InputMethodManager @ 0x413422e0 | 1 | 112 | 224 | 296
'- mServedView, mNextServedView android.widget.ListView @ 0x41331100 | 1 | 1 088 | 224 | 124 096
'- mContext nilsnett.chinese.activities.GameOverviewActivity @ 0x44eeecd8| 1 | 224 | 224 | 1 000
android.view.ViewRootImpl$WindowInputEventReceiver @ 0x41af4a60 Native Stack | 1 | 32 | 224 | 184
android.view.ViewRootImpl$WindowInputEventReceiver @ 0x412cf928 Native Stack | 1 | 32 | 224 | 184
android.view.ViewRootImpl$WindowInputEventReceiver @ 0x44cd3090 Native Stack | 1 | 32 | 224 | 184
'- this$0 android.view.ViewRootImpl @ 0x44cd1f20 | 1 | 464 | 224 | 2 168
'- mView com.android.internal.policy.impl.PhoneWindow$DecorView @ 0x44cbef00| 1 | 536 | 224 | 1 336
'- mContext nilsnett.chinese.activities.GameOverviewActivity @ 0x44cbdee8| 1 | 224 | 224 | 1 000
Total: 5 entries | 5 | 216 | 1 120 |
-------------------------------------------------------------------------------------------------------------------------------------------------

所以似乎有一个属性(或类?)WindowInputEventReceiver 保持对我的 Activity GameOverviewActivity 的引用。为什么,我该如何阻止它?

最佳答案

我发现原因是 WindowInputEventReceiver 实现了 finalize 方法,它导致引用。我在onDestroy中调用System.gc来解决这个问题。

关于android - 为什么在使用主页按钮离开时我的 Activity 会导致内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16959754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com