Tech Tock

Time is of the essence.

Losing my Memory

After fixing the classic event handler reference memory leak, I uncovered a WPF memory leak in my application.  It seems that WPF itself has a memory issue in the way it tries to lazy load dictionary resources.

I had a lot of additional memory in snapshot diffs being taken up by DeferredAppResourceReference objects.  There seems to be a Hotfix that addresses the issue, but I found a decent code solution by Andrew S on MSDN.  This code will instantiate the dictionary objects on startup, avoiding the buggy MS lazy loading.

public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
WalkDictionary(this.Resources);

base.OnStartup(e);
}

private static void WalkDictionary(ResourceDictionary resources)
{
foreach (DictionaryEntry entry in resources)
{
}

foreach (ResourceDictionary rd in resources.MergedDictionaries)
WalkDictionary(rd);
}
}

 

 

Ironically, now one of the largest growth objects in my app is WeakReference.

January 31, 2011 Posted by | Uncategorized | , , | 1 Comment

   

Follow

Get every new post delivered to your Inbox.