How can I prevent garbage collection from being ca

2020-02-15 04:34发布

问题:

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

回答1:

There is also a solution that would completely disable the bitmap image related memory pressure and subsequent garbage collection. It is more of a hack, but you can read about similar issue here.

typeof(BitmapImage).Assembly.GetType("MS.Internal.MemoryPressure").GetField("_totalMemory", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, Int64.MinValue / 2); 

This way you avoid searching all over your code to find and modify WPF icon initialization. On top of that some controls like System.Windows.Forms.Integration.ElementHost will implicitly add bitmap related memory pressure regardless of how you initialize.



回答2:

Have you check other stackoverflow questions related to the topic? There might be some hints you can use:

How to avoid garbage collection in real time .NET application?



回答3:

This is a bug/feature in WPF:

https://connect.microsoft.com/VisualStudio/feedback/details/687605/gc-is-forced-when-working-with-small-writeablebitmap

http://referencesource.microsoft.com/#PresentationCore/Core/CSharp/MS/Internal/MemoryPressure.cs

EDIT: Since .NET 4.6.2 the MemoryPressure class has been removed.



回答4:

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!