为什么垃圾收集在Windows 10 UWP应用程序中运行得太频繁?

时间:2016-04-27 13:51:23

标签: c# windows garbage-collection win-universal-app windows-10-mobile

为什么垃圾收集会阻止Windows 10 UWP中的UI线程? (可能没有。但是性能诊断工具说垃圾收集器在UI线程中运行) blocking UI thread 如果垃圾收集正在运行,应用程序也没有响应。 在Windows Phone 8.1中,它工作正常,并不经常调用垃圾回收。 windows 10 UWP garbage collection

更新 诊断工具的更新结果: enter image description here

更新2 我需要从外部API获取大对象(过去2年每月1000-2000个对象)。 API以json格式返回数据。我正在使用Newtonsoft.Json来反序列化对象。因此,每个API请求获得1-2 MB的临时数据是理想的行为。我使用FieldMedic从设备分析了PerfView日志,发现集合的反序列化是最昂贵的操作。 Windows Phone 8.1的结果相同。仅在JITStats中,Windows 10中的方法数量为6,734,而WP 8.1中则为421 enter image description here

Windows Phone 8.1的相同应用程序不会阻止UI线程。 enter image description here 问题是“为什么在Windows 10 UWP中从垃圾收集中需要这么多资源?”。

更新3 演示项目展示了不同之处。在WP8.1中,json序列化/反序列化无法阻止UI线程。在Windows 10中,线程被GC阻止。

private void StartButton_OnClick(object sender, RoutedEventArgs e)
    {        
        Task.Factory.StartNew(async () =>
        {
            var storageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///trips.json"));
            var content = await FileIO.ReadTextAsync(storageFile);
            for (int i = 0; i < 100; i++)
            {
                var trips = JsonConvert.DeserializeObject<IEnumerable<Trip>>(content);                    
            }
        });
    }

链接到演示项目: gc_test

0 个答案:

没有答案
相关问题