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.

Advertisements

January 31, 2011 - Posted by | Uncategorized | , ,

1 Comment »

  1. nice. thanks.

    Comment by softwaretrading | February 13, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: