UWP MediaPlayer (Windows.Media.Playback.MediaPlayer) 上的内存泄漏 [英] Memory Leak on UWP MediaPlayer (Windows.Media.Playback.MediaPlayer)

查看:26
本文介绍了UWP MediaPlayer (Windows.Media.Playback.MediaPlayer) 上的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在维护一个 WPF 应用程序.我在我的项目中添加了一个 UWP nedia 播放器.但是,内存使用率太高了.我意识到 UWP 媒体播放器做到了,所以我创建了一个可重现的代码.

while (true){var mp = 新媒体播放器(){Source = MediaSource.CreateFromUri(new Uri("Test.mp4"))};线程睡眠(1000);mp.播放();线程睡眠(1000);mp.Dispose();}

这段代码发生了内存泄漏.我创建了 MediaPlayer 并处理了它!但是,它的内存使用量无限增长.

如何捕获这段代码的内存泄漏?

这是 .NET Core 3.0 项目.(带有 WPF 的 XAML 岛)我还没有测试它是否出现在纯 UWP 项目中.

有人说这是自然的,因为它是一个循环.但是,下面的代码不会造成任何内存泄漏,因为 GC 有效.(当然,一些(但有限的)参考资料不会被收集.)

while (true){新的 SomeClass();}

解决方案

编写代码的方式内存会膨胀并增长,直到内存耗尽.我也在纯 UWP 中进行了验证.如果进行以下两个更改,您会发现内存会保持稳定,并且每次循环后系统都会回收所有内存:

  1. 同时处理您创建并分配给 Source 属性的 MediaSource 对象
  2. 不要在一个紧凑的循环中运行它,而是将自己作为调度程序操作调用

这是没有显示任何泄漏的代码(在 UWP 中测试).在 WPF 中 Dispatcher 调用看起来略有不同:

private async void PlayMedia(){var ms = MediaSource.CreateFromUri(new Uri("ms-appx:///Media1.mp4"));var mp = 新媒体播放器(){源 = 毫秒};线程睡眠(1000);mp.播放();线程睡眠(1000);mp.Dispose();女士处置();等待 Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(PlayMedia));}

附带说明:如果 SomeClass 是纯托管代码类,那么您提到的SomeClass"比较并不完全是一个苹果对苹果的比较,因为您在此处创建的对象是复杂的本机 Windows 运行时对象它们周围只有一个瘦的托管代码包装器.

现在也在 WPF 中测试:我重现了最初的内存增长问题,然后应用了建议的更改并验证内存不再增长.这是我的测试项目供参考:

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();
}

解决方案

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.

这篇关于UWP MediaPlayer (Windows.Media.Playback.MediaPlayer) 上的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆