本文介绍了如何防止垃圾收集从XAML窗口上调用ShowDialog的时候被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用大量内存的应用程序,但现在我无法改变这个事实。我的问题是,我有我想执行,并提供一个进度对话框的操作,但似乎显示XAML进度窗口引起 GC.Collect的被称为10倍!任何想法如何,我可以优化我打开进度窗口?



据我的蚂蚁探查领导到GC.Collect的调用是

  System.Window.ShowDialog() -  GT; 
..
..
System.Windows.Media.Imaging.BitmapSource.CreateCachedBitmap - >
SafeMILHandle.UpdateEstimatedSize - >
SafeMILHandleMemoryPressure.ctor - >
MemoryPressure.Add - >
MemoryPressure.ProcessAdd - >
GC.Collect的


解决方案

垃圾收集是引起的WPF图标的初始化。当我删除从XAML的图标属性:

 <窗​​口...图标=/ CommonUI;组件/通用/ ProgressReporting / MyIcon.ico> 

和,而不是初始化它在构造函数中:

 公共ProgressWindow()
{
的InitializeComponent();
图标= Properties.Resources.MyIcon.ToImageSource();
}

问题就走开了。



System.Window.UpdateIcon差()不再在的ShowDialog称为()。在 UpdateIcon()电话是创建一个 CachedBitmap 在图标文件中的每个图像大小而这又打电话 MemoryPressure.Add 键,由于应用程序的内存占用率过高打电话 GC.Collect的为创建的每个新的位图。



这个小变化减少了15秒我的进度对话框的加载时间,当一个大型项目中的应用程序加载!


I have a application that's using a lot of memory, but for now I cannot change this fact. My problem is that I have an operation I'd like to perform and provide a progress dialog but it appears that displaying the xaml progress window is causing GC.Collect to be called 10 times! Any ideas how I can optimize opening my progress window?

According to my Ants Profiler the calls leading up to GC.Collect are

System.Window.ShowDialog() ->
..
..
System.Windows.Media.Imaging.BitmapSource.CreateCachedBitmap ->
SafeMILHandle.UpdateEstimatedSize ->
SafeMILHandleMemoryPressure.ctor ->
MemoryPressure.Add ->
MemoryPressure.ProcessAdd ->
GC.Collect
解决方案

The garbage collection was caused by the initialization of the WPF icon. When I removed the icon property from the xaml:

<Window ... Icon="/CommonUI;component/Common/ProgressReporting/MyIcon.ico">

and instead initialized it in the constructor:

public ProgressWindow()
        {
            InitializeComponent();
            Icon = Properties.Resources.MyIcon.ToImageSource();
        }

the problem went away.

The difference that System.Window.UpdateIcon() is no longer called during ShowDialog(). The UpdateIcon() call was creating a CachedBitmap for each image size in the icon file which in turn was calling MemoryPressure.Add and due to the high memory usage of the application was calling GC.Collect for each new bitmap created.

This small change has reduced the loading time of my progress dialog by 15s when a large project is loaded in the application!

这篇关于如何防止垃圾收集从XAML窗口上调用ShowDialog的时候被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 02:12