Android-youtube-player: Memory not being released on destroying activity

Created on 22 Oct 2019  Β·  7Comments  Β·  Source: PierfrancescoSoffritti/android-youtube-player

I have observed that when activity is destroyed then all the instances of the library code are not finished and thus the memory taken by library code isn't released.

Could you please look into it.

bug

Most helpful comment

I'm also seeing the same issue reported by Logcat on Android Studio. A workaround that seems to work is to override onStop in my fragment as follows

override fun onStop() {
        super.onStop()
        youtubePlayerView.release()
    }

I understand this is not the best option as I'm doing more than needed but at least it unregisters the networkListener

Proposed fix is to simply call context.unregisterReceiver(networkListener) in the onStop method of LegacyYoutubePlayerView ...

@PierfrancescoSoffritti I have created a PR for a proposed fix... this is my 1st PR ever to an open-source project so I hope I'm doing things right.

Regards,
Adam

All 7 comments

Hey, how do you know this is happening?
Feel free to send a pull request.

I use your library as an item in my RecyclerView in an Activity. Maybe you need to check the internal webview.

Below is my LeakCanary info:

ApplicationLeak(className=com.abc.ArticleDetailActivity, leakTrace=
┬
β”œβ”€ org.chromium.device.power_save_blocker.PowerSaveBlocker
β”‚ Leaking: NO (a class is never leaking)
β”‚ GC Root: Global variable in native code
β”‚ ↓ static PowerSaveBlocker.b
β”‚ ~
β”œβ”€ java.util.HashMap
β”‚ Leaking: UNKNOWN
β”‚ ↓ HashMap.table
β”‚ ~
β”œβ”€ java.util.HashMap$Node[]
β”‚ Leaking: UNKNOWN
β”‚ ↓ array HashMap$Node[].[0]
β”‚ ~
β”œβ”€ java.util.HashMap$Node
β”‚ Leaking: UNKNOWN
β”‚ ↓ HashMap$Node.key
β”‚ ~

β”œβ”€ com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.WebViewYouTubePlayer
β”‚ Leaking: YES (View.mContext references a destroyed activity)
β”‚ mContext instance of com.abc.ArticleDetailActivity with mDestroyed = true
β”‚ View#mParent is null
β”‚ View#mAttachInfo is null (view detached)
β”‚ View.mWindowAttachCount = 2
β”‚ ↓ WebViewYouTubePlayer.mContext
β•°β†’ com.abc.ArticleDetailActivity
​ Leaking: YES (WebViewYouTubePlayer↑ is leaking and Activity#mDestroyed is true and ObjectWatcher was watching this)
​ key = 4ee2961d-1584-4a7a-8eab-d2c91687a56a
​ watchDurationMillis = 6575
​ retainedDurationMillis = 1567
, retainedHeapByteSize=295071)

Hello,
This problem can be reproduced in Sample App.
After adding debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-beta-3' into build.gradle of core-sample-app, launch "ayp API" and click "Fragment Example". Then press back key to go back to main page. You will see leakcanary reports the leak(refer to the attachment).
image

Thanks for the info. These days I don't have much time to work on the library, pull requests are welcome!

I use your library as an item in my RecyclerView in an Activity. Maybe you need to check the internal webview.

Below is my LeakCanary info:

ApplicationLeak(className=com.abc.ArticleDetailActivity, leakTrace=
┬
β”œβ”€ org.chromium.device.power_save_blocker.PowerSaveBlocker
β”‚ Leaking: NO (a class is never leaking)
β”‚ GC Root: Global variable in native code
β”‚ ↓ static PowerSaveBlocker.b
β”‚ ~
β”œβ”€ java.util.HashMap
β”‚ Leaking: UNKNOWN
β”‚ ↓ HashMap.table
β”‚ ~
β”œβ”€ java.util.HashMap$Node[]
β”‚ Leaking: UNKNOWN
β”‚ ↓ array HashMap$Node[].[0]
β”‚ ~
β”œβ”€ java.util.HashMap$Node
β”‚ Leaking: UNKNOWN
β”‚ ↓ HashMap$Node.key
β”‚ ~

β”œβ”€ com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.WebViewYouTubePlayer
β”‚ Leaking: YES (View.mContext references a destroyed activity)
β”‚ mContext instance of com.abc.ArticleDetailActivity with mDestroyed = true
β”‚ View#mParent is null
β”‚ View#mAttachInfo is null (view detached)
β”‚ View.mWindowAttachCount = 2
β”‚ ↓ WebViewYouTubePlayer.mContext
β•°β†’ com.abc.ArticleDetailActivity
​ Leaking: YES (WebViewYouTubePlayer↑ is leaking and Activity#mDestroyed is true and ObjectWatcher was watching this)
​ key = 4ee2961d-1584-4a7a-8eab-d2c91687a56a
​ watchDurationMillis = 6575
​ retainedDurationMillis = 1567
, retainedHeapByteSize=295071)

same problem with me

Screenshot_2020-03-24-00-36-08-99
how to resolve it?

I'm also seeing the same issue reported by Logcat on Android Studio. A workaround that seems to work is to override onStop in my fragment as follows

override fun onStop() {
        super.onStop()
        youtubePlayerView.release()
    }

I understand this is not the best option as I'm doing more than needed but at least it unregisters the networkListener

Proposed fix is to simply call context.unregisterReceiver(networkListener) in the onStop method of LegacyYoutubePlayerView ...

@PierfrancescoSoffritti I have created a PR for a proposed fix... this is my 1st PR ever to an open-source project so I hope I'm doing things right.

Regards,
Adam

Was this page helpful?
0 / 5 - 0 ratings

Related issues

theforec picture theforec  Β·  8Comments

cnbcyln picture cnbcyln  Β·  8Comments

mankidal19 picture mankidal19  Β·  8Comments

uzaysan picture uzaysan  Β·  7Comments

raghavagg01 picture raghavagg01  Β·  5Comments