1

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
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Kim
  • 1,068
  • 13
  • 25
  • Can you tell me how to overclock my CPU? Probably not, since you don't even know what kind of hardware I have. Likewise, it's hard to optimize code that you can't see. Post your code. – Kendall Frey Jan 29 '13 at 21:29
  • The call stack that leads to the garbage collection is all .NET assemblies so I don't know what part of my code would be worthwhile posting. – Kim Jan 29 '13 at 21:35

4 Answers4

2

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.

Community
  • 1
  • 1
  • 3
    Warning! This does not work with the latest .Net 4.6.2 (release 394802 / 394806). MemoryPressure class no longer exists and AddPressure and RemovePressure have been moved into GC. – kjbartel Aug 09 '16 at 03:16
  • 1
    I am facing the same problem. Please let know on how to get over this problem in .Net 4.6.2 – Rahul Sundar Nov 20 '17 at 11:32
1

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?

Community
  • 1
  • 1
mybrave
  • 1,662
  • 3
  • 20
  • 37
  • I tried adjusting the latency mode, but it didn't help in this situation. http://msdn.microsoft.com/en-us/library/bb384202.aspx – Kim Jan 30 '13 at 13:41
1

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.

WebDancer
  • 333
  • 3
  • 8
0

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!

Kim
  • 1,068
  • 13
  • 25