Android的语境内存泄漏的ListView由于AudioManager语境、内存、Android、ListView

2023-09-05 05:28:48 作者:别让我们变成回忆

我有一个的ListView ,我希望它从内存中清除,当活动结束。然而,似乎它正在泄漏。当我检查了内存转储,并获得 pathToGC 的ListView 我得到以下,

I have a ListView and I would expect it to be cleared from memory when the activity finishes. However, it appears that it is leaking. When I check the Memory Dump, and get the pathToGC for the ListView I get the following,

Class Name                                                          | Shallow Heap | Retained Heap 
android.widget.ExpandableListView @ 0x4063e560                      |          768 |        39,904 
|- list, mList com.hitpost.TeamChooser @ 0x405f92e8                 |          176 |         1,648 
|  '- mOuterContext android.app.ContextImpl @ 0x40657368            |          160 |           304 
|     '- mContext android.media.AudioManager @ 0x40662600           |           40 |           168 
|        '- this$0 android.media.AudioManager$1 @ 0x406626b0 Unknown|           24 |            24 

我看到了很多的同样的情况下泄露我的 ListView的。诀窍在于,我没有使用 AudioManager 的任何地方我的应用程序在所有的,从应用程序没有声音未来可言。请帮帮忙,它的驾驶我疯了。显然,试图弄清楚为什么会发生,什么可能是根的问题?

I see this same context leaking on a lot of of my ListView's. The trick is that, I am not using AudioManager anywhere in my app at all, no sound coming from the app at all. Please help, it's driving me crazy. Obviously trying to figure out why this is happening and what could be the root issue?

推荐答案

不涉及到OP的泄漏,但对于谁的人来,因为AudioManager在这里造成泄漏:

Not related to OP's leak, but for people who come in here because of AudioManager causing leak:

如果您看到此泄漏,因为使用的是VideoView,很可能就是因为这个错误的:https://$c$c.google.com/p/android/issues/detail?id=152173

If you see this leak because you are using VideoView, probably is because of this bug: https://code.google.com/p/android/issues/detail?id=152173

VideoView从未发布AudioManager如果被加载的视频。

VideoView never release AudioManager if video being loaded.

该修复程序,中提到的链接,创建VideoView手动使用ApplicationContext的。

the fix is, as mentioned in the link, create VideoView manually using ApplicationContext.

编辑:此解决将工作,直到......如果视频去codeR表示,视频有一个编码的问题。 VideoView尝试使用应用程序上下文弹出一个AlertDialog。比起崩溃发生。

this work around will work, until... if the video decoder says the video has an encoding problem. VideoView tries to pop up a AlertDialog using application context. Than a crash happens.

唯一的变通方法我能想到的就是继续使用活动的情况下创建的视频来看,在activity.onDestroy,使用反射设置AudioManager的mContext到应用程序上下文。

The only work around I can think is to keep creating video view using activity context, and in activity.onDestroy, set AudioManager's mContext to application context using reflection.

请注意:必须让使用activity.getSystemService(Context.AUDIO_SERVICE)的AudioManager,而不是activity.getApplicationContext.getSystemService(Context.AUDIO_SERVICE),因为AudioManager是上下文的成员变量(你会得到,如果你错了AudioManager实例从应用程序上下文得到它)。

Note: have to get the AudioManager using activity.getSystemService(Context.AUDIO_SERVICE) rather than activity.getApplicationContext.getSystemService(Context.AUDIO_SERVICE), since AudioManager is an member variable of Context (you will get wrong instance of AudioManager if you get it from application context).

最后,你可能不知道,为什么一个成员变量(AudioManager)是preventing类(活动),以被垃圾收集。从内存分析器,它显示AudioManager是由本地堆栈所拥有。所以AudioManager有点不干净本身正常。

At last, you may wonder why a member variable (AudioManager) is preventing the class (Activity) to being garbage collected. From the memory analyzer, it shows AudioManager is owned by native stack. So AudioManager somehow did not clean itself properly.