Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memory leak caused by initialize method of YouTubePlayer.Provider

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.

like image 559
andios Avatar asked Nov 25 '14 13:11

andios


People also ask

How to fix JavaScript memory leaks?

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.

When cause memory leak in web application?

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.

Which of the following can be done to reduce memory leakage?

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.

What is memory leak What are different types and how do you avoid them?

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.


1 Answers

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 :

  • It's not the listener 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.
  • While there is a leak for sure, it seems that at some point, there is a cleanup being done. I would go to 18 instances of the same activity, and then the count would drop down to 8 instances. So I guess that for the majority of the cases the leak could go unnoticed.
  • 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

like image 188
hulius Avatar answered Oct 12 '22 22:10

hulius