BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Three Hotfixes for WPF Memory Leaks

| by Jonathan Allen Follow 66 Followers on Jun 17, 2010. Estimated reading time: 1 minute |

Windows Presentation Foundation is quickly becoming well known for the ease in which memory leaks are introduced. For example, it heavily relies on weak references for its data-binding technology. Like concurrent multi-threading, weak references seem easy enough but are in fact surprisingly difficult to use correctly. Even when the actual objects are being released in a timely manner, WPF’s arrays of weak references hang around and under stress become the memory leak.

This is especially apparent when using a large number of short-lived elements. KB981107 addresses this in three areas. First, WPF can leak application-level resources for styles and templates if the control that references the resource never actually uses it. Jossef Goldberg elaborates,

An example:  App resources define   <sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">20</sys:Double>     This creates a resource that's used by any control that can display a horizontal scrollbar.  Creating a large number of such controls that never actually display their scrollbar will encounter the leak.

Another situation covered by this hotfix is when lots of short-lived elements have properties that are data-bound to a dependency property on a long-lived object. The weak reference from the long-lived object to the short-lived object is kept long after the short-lived object has been destroyed.

Unfortunately the KB981107 hotfix isn’t available for .NET 4.0 yet and Microsoft hasn’t even decided if it intends to release one. Two other hotfixes, KB967634 and KB967328 were shipped as part of .NET 4.0. so anyone on that platform doesn’t need to worry about them.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT