<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JAPF &#187; memory</title>
	<atom:link href="http://www.japf.fr/tag/memory/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.japf.fr</link>
	<description>Jeremy Alles Presentation Foundation: WPF, .Net and modern software development</description>
	<lastBuildDate>Thu, 29 Jul 2010 07:29:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Learn how .Net manages memory in 15min</title>
		<link>http://www.japf.fr/2009/08/learn-how-net-manages-memory-in-15min/</link>
		<comments>http://www.japf.fr/2009/08/learn-how-net-manages-memory-in-15min/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 13:20:24 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[memory]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=248</guid>
		<description><![CDATA[A couple of days a ago I colleague sent me a cool link that I wanted to share here. If you want to learn how .Net manages memory in details, understand how the garbage collector works,  what is the next object pointer, how finalizers are managed (and much more !), you should check it out [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of days a ago I colleague sent me a cool link that I wanted to share here.</p>
<p>If you want to learn how .Net <em><strong>manages memory</strong></em> in details, understand how the <strong><em>garbage collector</em></strong> works,  what is the <strong><em>next object pointer</em></strong>, how <strong><em>finalizers</em> </strong>are managed (and much more !), you should check it out this <a href="http://www.red-gate.com/products/ants_memory_profiler/DOTNET_Memory_Management/Index.html">cool video</a> made by the guys of <a href="http://www.red-gate.com/products/ants_performance_profiler/index.htm">ANTS Profiler</a>:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2009/08/memory-management.png" rel="lightbox[248]"><img class="alignnone size-full wp-image-249" title="memory-management" src="http://www.japf.fr/wp-content/uploads/2009/08/memory-management.png" alt="memory-management" width="381" height="210" /></a></p>
<p><a href="http://www.red-gate.com/products/ants_memory_profiler/DOTNET_Memory_Management/Index.html">source video</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/08/learn-how-net-manages-memory-in-15min/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WPF possible memory leak with EventManager.RegisterClassHandler</title>
		<link>http://www.japf.fr/2009/08/wpf-memory-leak-with-eventmanager-registerclasshandler/</link>
		<comments>http://www.japf.fr/2009/08/wpf-memory-leak-with-eventmanager-registerclasshandler/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 06:43:17 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[leak]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[routedevent]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=234</guid>
		<description><![CDATA[In the project I’m currently working on, I&#8217;m using the DataGrid from the WPF toolkit (that will be part of .Net 4.0 btw). Because I needed to tweak its behavior for copy/paste operations, I created a new class, PropertyDataGrid that inherits the DataGrid of the toolkit (you’ll notice that this time I used inheritance and [...]]]></description>
			<content:encoded><![CDATA[<p>In the project I’m currently working on, I&#8217;m using the DataGrid from the <a href="http://wpf.codeplex.com/">WPF toolkit</a> (that will be part of .Net 4.0 btw). Because I needed to tweak its behavior for copy/paste operations, I created a new class, PropertyDataGrid that inherits the DataGrid of the toolkit (you’ll notice that this time I used inheritance and not simply an attached behavior :p). In my DataGrid, I needed to register an event handler to be notified when a key is pressed in a DataGridCell. They are several ways to accomplish that:</p>
<ul>
<li>If you’re tied to a XAML file (with a UserControl for instance) you can use an <a href="http://msdn.microsoft.com/en-us/library/system.windows.eventsetter.aspx">EventSetter</a></li>
<li>If you’re using a class that derives from <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx">ItemsControl</a> you can override the GetContainerForItemOverride and IsItemItsOwnContainerOverride method to make the control uses your sub class (for example a CustomListBox that uses CustomListBoxItem) and do the job in the sub class (CustomListBoxItem)</li>
</ul>
<p>Because those 2 approaches weren’t good in our case, we used the EventManager class that is very useful for this kind of operations. In one line of code, you can register an EventHandler for any RoutedEvent of any class:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p234code3'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2343"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p234code3"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> PropertyDataGrid<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    EventManager<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterClassHandler</span><span style="color: #008000;">&#40;</span>
        <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>DataGridCell<span style="color: #008000;">&#41;</span>, 
        PreviewKeyDownEvent, 
        <span style="color: #008000;">new</span> KeyEventHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnCellPreviewKeyDown</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I wrote this code a couple of months ago. Then, a tester explained me that the memory used by our window was never reclaimed. Because the user can open several windows, the memory of the application was growing very quickly.</p>
<p>After spending a couple of hours tracking this memory leak, I found out that it was the call to RegisterClassHandler that was causing the leak. Using a non-static method (OnCellPreviewKeyDown) was causing the EventManager to keep a reference to the PropertyDataGrid control. This strong reference prevented the control for being garbage collected.</p>
<p>The solution was quick and easy; we just make the method static and initialize the handler in the static constructor. This way the delegate does not have a reference to the instance of the control, and can be kept by the EventManager without causing the leak:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p234code4'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2344"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p234code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">static</span> PropertyDataGrid<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    EventManager<span style="color: #008000;">.</span><span style="color: #0000FF;">RegisterClassHandler</span><span style="color: #008000;">&#40;</span>
        <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>DataGridCell<span style="color: #008000;">&#41;</span>, 
        PreviewKeyDownEvent, 
        <span style="color: #008000;">new</span> KeyEventHandler<span style="color: #008000;">&#40;</span>OnCellPreviewKeyDown<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And voila ! We move from a solution eating always more memory to something reasonable:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2009/08/before.png" rel="lightbox[234]"><img src="http://www.japf.fr/wp-content/uploads/2009/08/before.png" alt="before" title="before" width="137" height="129" class="alignnone size-full wp-image-238" /></a><br />
<a href="http://www.japf.fr/wp-content/uploads/2009/08/after.png" rel="lightbox[234]"><img src="http://www.japf.fr/wp-content/uploads/2009/08/after.png" alt="after" title="after" width="137" height="129" class="alignnone size-full wp-image-238" /></a></p>
<p>Even using tools such a <a href="http://memprofiler.com">Memory Profiler</a>, it was difficult to find the source of the leak, but at least, now, I’ll use carefully the EventManager class.</p>
<p>Note that of course you can have memory leak problems too if you don’t unregister your event handlers properly. If you want more information about that you can check out this <a href="http://www.codeproject.com/KB/cs/WeakEvents.aspx">article</a> on CodeProject.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/08/wpf-memory-leak-with-eventmanager-registerclasshandler/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
