In the main activity of my app, I show a number of cards to user. when he clicks on any of them, a fragment is opened which contains a ViewPager2
. The fragments inside the viewPager2 each contains an ExoPlayer
and plays a related video when resumed.
When the user clicks on back button, the fragment must be closed and release all its children (including viewPager2 and its fragments, and exoPlayers). In order to release exoPlayers, I use the following code in OnDestroyView()
of the viewPager2 fragments:
@Override
public void onDestroyView() {
super.onDestroyView();
try {
if (player != null && listener != null) {
player.removeListener(listener);
player.release();
player = null;
}
if (playerView != null)
playerView.setPlayer(null);
dashMediaSource = null;
progressiveMediaSource = null;
playerView = null;
listener = null;
} catch (Exception ignored) {
}
}
after that, I ran Android Studio profiler to check app heap and I noticed some objects related to exoPlayer are still in memory:
Now the question is are I making a mistake in releasing the memory and specifically exoPlayers? If not why these objects are still in RAM and how to get rid of them?