media-kit / media-kit

A cross-platform video player & audio player for Flutter & Dart.
https://github.com/media-kit/media-kit
MIT License
893 stars 126 forks source link

memory leaks? #776

Open liuchuancong opened 1 month ago

liuchuancong commented 1 month ago

After playing the video for an hour in Windows, the content soared from 300MB to 1G. The non-caching attribute has been set. Is this a memory leak in the player?

Here is my simple code :

player = Player();
      if (player.platform is NativePlayer) {
        await (player.platform as dynamic).setProperty('cache', 'no');
      }
      mediaPlayerController = media_kit_video.VideoController(player);
      setDataSource(datasource);
      mediaPlayerController.player.stream.playing.listen((bool playing) {
        if (playing) {
          isPlaying.value = true;
        } else {
          isPlaying.value = false;
        }
      });
      mediaPlayerController.player.stream.error.listen((event) {
        if (event.toString().contains('Failed to open')) {
          hasError.value = true;
          isPlaying.value = false;
        }
      });
      mediaPlayerControllerInitialized.value = true;
justbendev commented 3 weeks ago

I I'm running 16 RTSP Streams and ram usage go up pretty quick to around 2900 MB in only 15 minutes. Note : 16 Player instances alone without any media is already 700-800MB of memory

The cache should be default be limited to 32MB but i don't think this is actually being applied. After seeing you use setProperty i investigated the function and read all available mpv options and applied theses options.

Player ply = Player();
      (ply.platform as dynamic).setProperty('cache', 'no');                   // --cache=<yes|no|auto>
      (ply.platform as dynamic).setProperty('cache-secs', '0');               // --cache-secs=<seconds> with cache but why not.
      (ply.platform as dynamic).setProperty('demuxer-seekable-cache', 'no');  // --demuxer-seekable-cache=<yes|no|auto> Redundant with cache but why not.
      (ply.platform as dynamic).setProperty('demuxer-max-back-bytes', '0');   // --demuxer-max-back-bytes=<bytesize>
      (ply.platform as dynamic).setProperty('demuxer-donate-buffer', 'no');   // --demuxer-donate-buffer==<yes|no>

Now it's stable and won't go over 1,688 MB after hours of running it

All my memory usage numbers are in Debug mode

liuchuancong commented 2 weeks ago

@justbendev Thanks for the recovery, I'll try to see if the problem recurs, you're my hero!

liuchuancong commented 2 weeks ago

This problem still exists,increase 30M every 10 minutes when playing m3u8 media files

sherlockvn commented 2 weeks ago

I run the profile, its actually leaks, I just open and close video again 8 times, and see some component inVideoTexture class is not disposed.

image
bdlukaa commented 3 days ago

It looks like these two properties are not being disposed at the dispose() method.

https://github.com/media-kit/media-kit/blob/13f6b3f0ef9ccf8a711e396bc49cb05af9b3e497/media_kit_video/lib/src/video/video_texture.dart#L139-L156

https://github.com/media-kit/media-kit/blob/13f6b3f0ef9ccf8a711e396bc49cb05af9b3e497/media_kit_video/lib/src/video/video_texture.dart#L292-L300