0

I am maintenancing a WPF application. I added a UWP nedia player on my project. But, memory usage is too high. I realized that UWP media player did it, so I created a reproducible code.

while (true)
{
    var mp = new MediaPlayer()
    {
        Source = MediaSource.CreateFromUri(new Uri("Test.mp4"))
    };
    Thread.Sleep(1000);
    mp.Play();
    Thread.Sleep(1000);
    mp.Dispose();
}

This code occurs a memory leak. I created MediaPlayer and Disposed it! But, its memory usage grows up infinitely.

How can I catch memory leak on this code?

This is .NET Core 3.0 project. (XAML islands with WPF) I didn't test that if it occurs in pure UWP project, yet.

Someone says that it is natural because it is a loop. But, below code doesn't make any memory leak because GC works. (Of course, some (but limitative) references will be not collected.)

while (true)
{
    new SomeClass();
}
Stefan Wick MSFT
  • 13,600
  • 1
  • 32
  • 51
P. ful
  • 113
  • 9

2 Answers2

2

It is absolutely a bug of Windows 10 19H1. Because built-in app (Movie and TV) has same memory leak issue. To reproduce this, just repeat that open video file and close it.

P. ful
  • 113
  • 9
  • I could confirm this! I've ran a video in a loop for a couple of minutes and memory did rise from around 300mb to 1037mb and constantly rising. – C0dR Dec 02 '19 at 16:15
1

The way your code is written memory will bloat and grow until you run out of memory. I verified also in pure UWP. If you make the following two changes you will find that the memory will remain stable and the system will reclaim all memory after each loop:

  1. Dispose also of the MediaSource object you create and assign to the Source property
  2. Don't run this in a tight loop, instead invoke yourself as a dispatcher action

Here is the code (tested in UWP) that doesn't show any leak. In WPF the Dispatcher call would look slightly different:

private async void PlayMedia()
{
    var ms = MediaSource.CreateFromUri(new Uri("ms-appx:///Media1.mp4"));
    var mp = new MediaPlayer()
    {
        Source = ms
    };
    Thread.Sleep(1000);
    mp.Play();
    Thread.Sleep(1000);
    mp.Dispose();
    ms.Dispose();

    await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(PlayMedia));
}

As a side note: the "SomeClass" comparison you mentioned isn't exactly an apples-to-apples to comparison if SomeClass is a pure managed code class, as the objects you are creating here are complex native Windows Runtime objects that only have a thin managed code wrapper around them.

Tested also now in WPF: I reproduced the original memory growth issue, then applied the suggested changes and verified that the memory no longer grows. Here is my test project for reference: https://1drv.ms/u/s!AovTwKUMywTNuLk9p3frvE-U37saSw

Also I ran your shared solution with the WPF app packaged as a Windows App Package and I am not seeing a leak on the latest released version of Windows 10 (17763.316). Below is a screenshot of the memory diagnostics after running your solution for quite a while. If this is specific to the insider build you are running, please log a bug via Feedback Hub. I think at this point we should close this question as answered.

enter image description here

Stefan Wick MSFT
  • 13,600
  • 1
  • 32
  • 51
  • I already tried it. But, it leaks. I think if it is not happened in UWP, it might be a bug of XamlIsland (in preview stage). – P. ful Feb 17 '19 at 10:57
  • Your repro code does not involve XamlIslands at all actually. I have just tested the scenario based on the code you provided in the question and confirmed that my suggested code changes also work in the WPF case. Here is my test project: https://1drv.ms/u/s!AovTwKUMywTNuLk9p3frvE-U37saSw - if this doesn't solve it for you, please share a complete repro project that demonstrates the issue. – Stefan Wick MSFT Feb 17 '19 at 15:58
  • Thank you. I realized that UWP media player can be used without XAML islands. By the way, I still cannot understand why it is happened. I think that there should be no memory leak even in a tight loop because I called Dispose(). And, I found that both your and my code didn't cause a memory leak. (Sorry) But, if I create WAP (Windows Application Packaging) project, it leaks. And there is a difference that pure WPF project didn't make any sound, but WAP project played audio in video file. Oh, I am using Windows Insider Preview 19H1 (Build 18334.1) with preview SDK. – P. ful Feb 17 '19 at 22:31
  • Here is a full example solution file. Thanks again. https://drive.google.com/open?id=1-FU9ci9E8DeD3IKXmcDM4OWfYxs6sKJn – P. ful Feb 17 '19 at 22:40
  • So with WAP it leaks even after applying the suggested changes? If so please add that information to the question - or close this one and open a new question for clarity. The question didn't mention anything about WAP or 19H1. – Stefan Wick MSFT Feb 18 '19 at 00:38
  • The solution that I uploaded is your solution with WAP project, and sample video file. And, I expected that it leaks because of not WAP project but UWP bug yesterday. 19H1 is just a hypothesis. – P. ful Feb 18 '19 at 01:21
  • No leak with your shared solution on the latest released build of Windows 10 (1809 update, build 17763.316). See my updated answer. – Stefan Wick MSFT Feb 18 '19 at 04:43
  • Thank you for your kind answers. I will post Insider feedback as soon as possible. – P. ful Feb 18 '19 at 10:00