hudec117 / Mpv.NET-lib-

.NET embeddable video/media player based on mpv for WinForms and WPF
MIT License
138 stars 36 forks source link

Memory leak while using hwdec #27

Open OneB1t opened 3 years ago

OneB1t commented 3 years ago

Mpv.NET-lib version: 1.1.1

Describe the bug When i use mpv hwdec native memory is not correctly freed up. mpv-1.dll looks to be disposed correctly but some other libraries loaded by this dll are still hanging in memory.

To Reproduce Steps to reproduce the behavior:

  1. Use this code to enable hw decoding: player.API.SetOptionString("vo", "gpu"); // hwdecoder enabled player.API.SetOptionString("gpu-api", "d3d11"); player.API.SetOptionString("hwdec", "dxva2"); // hwdecoder enabled or (this makes even worse memory leak on AMD) player.API.SetOptionString("vo", "gpu"); // hwdecoder enabled player.API.SetOptionString("gpu-api", "d3d11"); player.API.SetOptionString("hwdec", "d3d11va"); // hwdecoder enabled

  2. After few mpv-1.dll loads and unloads program will crash/Freeze with out of memory exception

dxva2 memory usage: image

hwdec off: image

This does not happen when hwdec is off (everything is then disposed correctly).

Also i think that speed with which it is eating memory is somehow affected by graphic card drivers as on my radeon r290X leak speed is much higher then on intel hd 630. When i check handles inside process hacker it looks like with every load of mpv-1.dll it is not unloading this memory section inside handles

image is there anything i can do to force unload mpv-1.dll and all its children modules?

SebastianBecker2 commented 3 years ago

I had a similar issue when reading the duration from a video file. (Though I'm not even 100% sure I did that correctly... regardless)

I used the libmpv version 0.33 but when I tried the 0.29 version from this repo, the memory leak was gone. It turns out, at least for my use-case, the issue started with 0.32, while 0.31 works perfectly fine.

So maybe you could try different versions of libmpv.

SebastianBecker2 commented 3 years ago

Update on my issue, in case it's related to yours.

It turned out that it wasn't really a memory leak. The libmpv 0.33 and 0.32 allocated up to ~2 GB of RAM pretty quickly but didn't exceed that limit. I guess it allocated until it reached that limit and then reused it.

I tried compiling libmpv myself using the following flags: --enable-libmpv-shared --disable-cplayer --enable-lgpl This had the same result. But when I add the following flags: --disable-debug-build --disable-lua (I tried to shrink the binary a bit) this behavior was gone. The memory allocation didn't succeed even 200 MB anymore. I assume it's the debug symbols causing this.

Though you might just have a very different issue.

GiantappMan commented 3 years ago

I encountered the same issue. if I call player.stop() before playing video, the memory will increase hugely.

image

I remove that code, the memory increase will slowly. but it still seems to have a little memory leak.

image image

demo here: https://github.com/DaZiYuan/mpv_memory_leak_test