mrmaffen / vlc-android-sdk

VLC Android SDK pushed to Maven Central. Primarily used in project tomahawk-android.
792 stars 244 forks source link

memoryLeak in version 2.0.6 #67

Open kazemihabib opened 7 years ago

kazemihabib commented 7 years ago

I think in new version 2.0.6 there is a memory leak. Is any one else had this problem?

SaundersB commented 7 years ago

I'm experiencing this same problem. Working on a solution where I instantiate and set those libVLC Android objects null. Let me know if you find any solutions.

vdegrandpre commented 6 years ago

I think there is a leak in 2.1.12 as well and this is how I reproduce.

Looping the same video forever produces an observable memory leak. In 35 minutes, memory has grown up 20 MB. Let the player go and it will crash in the future.

Here is a createPlayer function inside a VideoActivity :

if(libvlc == null)
    libvlc = new LibVLC(this, options);           

PlayerListener mPlayerListener = new PlayerListener(this,mediaPath);
mMediaPlayer = new MediaPlayer(libvlc);
mMediaPlayer.setEventListener(mPlayerListener);

// Set up video output
final IVLCVout vout = mMediaPlayer.getVLCVout();
vout.setVideoView(mSurface);    
vout.addCallback(this);
vout.attachViews();

Media m = new Media(libvlc, media);
mMediaPlayer.setAspectRatio("16:9");
mMediaPlayer.setMedia(m);
mMediaPlayer.play();`

And here a playerListener object :

  private static class PlayerListener implements MediaPlayer.EventListener {
    private WeakReference<VideoActivity> mOwner;
    private String mFilePath;

    public PlayerListener(VideoActivity owner, String mFilePath) {
        mOwner = new WeakReference<VideoActivity>(owner);
        this.mFilePath = mFilePath;
    }

    public void onEvent(MediaPlayer.Event event) {
        VideoActivity player = mOwner.get();

        switch(event.type) {
            case MediaPlayer.Event.EndReached:
                Log.d(TAG, "MediaPlayerEndReached");
                player.mMediaPlayer.stop();
                player.mMediaPlayer.setPosition(0);
                player.mMediaPlayer.play();
                break;
            case MediaPlayer.Event.Playing:
            case MediaPlayer.Event.Paused:
            case MediaPlayer.Event.Stopped:
            default:
                break;
        }
    }
}

` Note : These parts of code are inspired from edwardcw, which is reffered to in VideoLAN wiki page LibVLC on Android

vdegrandpre commented 6 years ago

Apparently the VLC native objects cannot be re-used without causing memory leaks. By destroying them at each media end, the memory gets more stable. I'm currently testing this approach and am confident that this solves the problem.

anonym24 commented 6 years ago

@vdegrandpre what exactly are you destroying?

vdegrandpre commented 6 years ago

Hey @anonym24 basically there are 2 things here.

  1. There IS a native memory leak that is observable and repeatable;
  2. I destroyed everything, except the application itself (thus, the owner process), meaning : releasing media, LibVLC and everything in-between

Even the simpliest use case (one video looping to infinity) causes a native memory growth that would eventually overflow.

Destroying/releasing everything helps but does not resolve.

Best, V.

anonym24 commented 6 years ago

@vdegrandpre isn't media released when you call MediaPlayer.stop() method? I don't think we should manually release it

also I don't think that official VLC Android app releases MediaPlayer and LibVLC every time video track is changed

they do something else and there are no memory issues

anonym24 commented 6 years ago

@vdegrandpre I made a test class from LibVLC sample to analyze native memory leaks and releasing MediaPlayeror LibVLCdoesn't help:

https://code.videolan.org/videolan/vlc-android/issues/580

so I have no idea how to solve this issue

vdegrandpre commented 6 years ago

My guess is that, depending on your device, it may also be an OMX resources that is not freed correctly or the like. Because OMX is used in many-many technologies nowadays, that may be the issue. I'll follow the ticket you opened on videolan.org.

Best, V.