Using Eclipse Memory Analyzer i discovered that if I go back and forth between 2 activity (e.g. A and B), although onDestroy method of activity B is called, GC never removes it from memory, so I have multiple instances of Activity B in memory. I found that the problem is in initialize method called on YouTubePlayerFragment instance. Here is the code:
YouTubePlayerFragment ytpf =(YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
ytpf.initialize(DEVELOPER_KEY,this);
this
is the the activity where fragment takes place, and implements YouTubePlayer.OnInitializedListener
.
How can I release the callback passed on initialize method?
I tried to call youtubePlayer.release()
but the problem remains.
Fixing The LeakClick on the memory tab. Here you have three options to take a memory snapshot: JS Heap Snapshot: Shows memory distribution of JS objects and DOM elements. Allocation Instrumentation on Timeline: Shows JS object memory allocation during a specific time interval.
In computer science, a memory leak is a leak of resources when computer software incorrectly manages memory allocation. A memory leak occurs when your web application assigns memory and keeps using it even though it is no longer needed.
Use reference objects to avoid memory leaks ref package, you can work with the garbage collector in your program. This allows you to avoid directly referencing objects and use special reference objects that the garbage collector easily clears. The special subclasses allow you to refer to objects indirectly.
Memory leak occurs when programmers create a memory in heap and forget to delete it. The consequences of memory leak is that it reduces the performance of the computer by reducing the amount of available memory.
I was hit by this leak this week. It seems that Google still hasn't fixed it, regardless of the ticket's status. I made some tests on my initial project, on which the leak is causing an OOM, and on the demo project by Google. My observations are :
YouTubePlayer.OnInitializedListener
that is causing the leak. When calling YouTubePlayerFragment.initialize()
, there are calls in YouTubePlayerFragment
getting a reference to the activity that is hosting the fragment. So no luck in trying to call initialize()
with an empty and static listener when the fragment is being stopped, hoping that the reference would be cleaned up.YouTubePlayerFragment
& YouTubePlayerView
, same leaks.I don't see any workaround this. I guess I will have to use another implementation, based on a webview like this one : https://github.com/PierfrancescoSoffritti/Android-YouTube-Player
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With