Android Context Memory Leak ListView due to AudioManager

后端 未结 8 1859
走了就别回头了
走了就别回头了 2020-12-15 05:24

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 Du

8条回答
  •  难免孤独
    2020-12-15 05:38

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

    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 never release AudioManager if video being loaded.

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

    Edit: 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.

    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.

    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).

    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.

提交回复
热议问题