UWP App WebView 内存泄漏,图像不清晰 [英] UWP App WebView Leaks Memory, doesn't clear images

查看:24
本文介绍了UWP App WebView 内存泄漏,图像不清晰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

WebView 在加载图像后不释放内存.

如果所有 WebView 实例都被销毁几秒钟,内存似乎确实会被释放.我们从 XAML 树中删除并清除了代码中的所有引用.(我们在调试器中检查到所有实例都在那时被释放.)

The memory does seem to get released if all WebView instances are destroyed for few seconds. We removed from XAML tree and all references in code cleared. (We checked in debugger that all instances were released at that point.)

这个解决方案是有问题的,因为 Web 视图必须死一段时间才能启动内存清除,这对于我们的用例来说是不可接受的.

This solution is problematic since the web view must be dead for a while for the memory clearing to kick in and is unacceptable for our use case.

如何复制:

制作 UWP C# 应用或 C++ UWP 应用 -> 添加 WebView -> 加载带有随机 URL 的大图像 -> 内存不断增长.

Make either a UWP C# app or a C++ UWP app -> add a WebView -> load large images with randomized URLs -> Memory keeps growing.

我们只有一个活动的 WebView,我们一个接一个地在其中多次加载大图像.(我们随机化部分图片网址以模拟不同的广告加载.)

We have only one active WebView and we load a large image in it multiple times one after another. (We randomise part of the image url to simulate different ad loads.)

内存不断增长,就好像图像永远不会被释放一样.我们尝试了什么:

The memory keeps growing as if the images never get released. What we tried:

  • 使用 WebView.ClearTemporaryWebDataAsync() 清除缓存,但它没有做任何事情.
  • 手动触发 GC.

注意事项:

  • 我们使用这个WebView(WebViewExecutionMode.SeparateThread)"初始化webview.(其他执行模式似乎没有帮助).
  • 我们不使用 WebViewBrush.

推荐答案

UWP 应用 WebView 内存泄漏,无法清除图像

UWP App WebView Leaks Memory, doesn't clear images

WebView 是复杂元素.并且它有自己的垃圾收集规则,为了保持渲染性能,它会缓存大量数据,导致内存不断增长,gc 进程很慢.我们不能两全其美.

WebView is complex element. And it has own garbage collection rules, For keep render performance, it will cache a lot of data that cause memory keeps growing and gc process is slow. we can't have it both ways.

根据我的经验,您可以设置 WebView 重复源为about:blank",可以立即释放大部分数据.

For my experience, you could set the WebView Source as "about:blank" repeatedly that could release most data immediately.

private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
    int count = 0;
    var timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) };
    timer.Start();
    timer.Tick += (s, p) =>
    {
        TestWebView.Source = new Uri("about:blank");
        count++;
        if (count == 20)
        {
            timer.Stop();
        }
    };      
}

这篇关于UWP App WebView 内存泄漏,图像不清晰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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