<?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, Silverlight, Windows Phone 7, Windows 8</description>
	<lastBuildDate>Fri, 27 Jan 2012 07:57:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Track memory usage of your Windows Phone 7.1 app in real time</title>
		<link>http://www.japf.fr/2011/11/track-memory-usage-of-your-windows-phone-7-1-app-in-real-time/</link>
		<comments>http://www.japf.fr/2011/11/track-memory-usage-of-your-windows-phone-7-1-app-in-real-time/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 11:51:38 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Windows Phone]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[windowsphone]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=1001</guid>
		<description><![CDATA[Update January 17th: I just found out that Peter Torr released more than a year ago a similar helper class which is very nice. You can check out his solution here. Memory usage is an important aspect, especially on mobile device. If you want to publish an app on the Windows Phone marketplace you must [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update January 17th:</strong> I just found out that <a href="http://blogs.msdn.com/b/ptorr">Peter Torr</a> released more than a year ago a similar helper class which is very nice. You can check out his solution <a href="http://blogs.msdn.com/b/ptorr/archive/2010/10/30/that-memory-thing-i-promised-you.aspx">here</a>.</p>
<p>Memory usage is an important aspect, especially on mobile device. If you want to publish an app on the Windows Phone marketplace you must satisfy the <em>Technical Certification Requirements: &#8220;5.2.5 Memory Consumption: An application must not exceed 90 MB of RAM usage, except on devices that have more than 256 MB of memory.&#8221; </em></p>
<p>In this post, I&#8217;m sharing <strong>a technique to track the memory usage of a WP7 app in real tile in every single page of the app</strong>. By adding only one line of code in your existing app, you’ll be able to display memory usage in all your pages (without any changes):</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2011/11/memorywatcher.png" rel="lightbox[1001]"><img class="alignnone size-full wp-image-1014" title="memorywatcher" src="http://www.japf.fr/wp-content/uploads/2011/11/memorywatcher.png" alt="" width="525" height="474" /></a></p>
<p><a href="http://www.japf.fr/wp-content/uploads/2011/11/MemoryWatcherDemo.zip">Download source code and example</a></p>
<p><span style="text-decoration: underline;">How to integrate the component in your existing app?</span></p>
<ol>
<li>import the <em>MemoryWatcher</em> class in your existing project</li>
<li>in the InitializePhoneApplication method, add a new line after the creation of the RootFrame:</li>
</ol>

<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('p1001code3'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10013"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p1001code3"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Create the frame but don't set it as RootVisual yet; this allows the splash</span>
<span style="color: #008080; font-style: italic;">// screen to remain active until the application is ready to render.</span>
RootFrame <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> PhoneApplicationFrame<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// Add the following line !</span>
<span style="color: #008000;">new</span> MemoryWatcher<span style="color: #008000;">&#40;</span>RootFrame<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> IsDisplayed <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><span style="text-decoration: underline;">How it works?</span></p>
<p>During its initialization, the <em>MemoryWatcher</em> control will set an event handler to have a callback whenever the user navigates to a new page. When the new page is loaded, it checks if it can dynamically insert the <em>MemoryWatcher</em> control. This is done by checking the root UI element of the page and inserting the watcher control in it. Here is the full code of the <em>MemoryWatcher</em> 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('p1001code4'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p10014"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
</pre></td><td class="code" id="p1001code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Controls</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Media</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Navigation</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Windows.Threading</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Microsoft.Phone.Controls</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Microsoft.Phone.Info</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> MemoryWatcherDemo
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> MemoryWatcher <span style="color: #008000;">:</span> ContentControl
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> DispatcherTimer timer<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">readonly</span> PhoneApplicationFrame frame<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">float</span> ByteToMega <span style="color: #008000;">=</span> <span style="color: #FF0000;">1024</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">1024</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsDisplayed <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">public</span> MemoryWatcher<span style="color: #008000;">&#40;</span>PhoneApplicationFrame frame<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>frame <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> ArgumentNullException<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;frame&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">frame</span> <span style="color: #008000;">=</span> frame<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">frame</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Navigated</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> NavigatedEventHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnFrameNavigated</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">frame</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Navigating</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> NavigatingCancelEventHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnFrameNavigating</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">timer</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DispatcherTimer <span style="color: #008000;">&#123;</span> Interval <span style="color: #008000;">=</span> TimeSpan<span style="color: #008000;">.</span><span style="color: #0000FF;">FromMilliseconds</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">timer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Tick</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> EventHandler<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">OnTimerTick</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #008080; font-style: italic;">// setup some basic properties to ensure the content will be visible</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Foreground</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SolidColorBrush<span style="color: #008000;">&#40;</span>Colors<span style="color: #008000;">.</span><span style="color: #0000FF;">Red</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">VerticalContentAlignment</span> <span style="color: #008000;">=</span> VerticalAlignment<span style="color: #008000;">.</span><span style="color: #0000FF;">Center</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">HorizontalContentAlignment</span> <span style="color: #008000;">=</span> HorizontalAlignment<span style="color: #008000;">.</span><span style="color: #0000FF;">Center</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Margin</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Thickness<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">0</span>, <span style="color: #008000;">-</span><span style="color: #FF0000;">35</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnFrameNavigated<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, NavigationEventArgs e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsDisplayed</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">;</span>
&nbsp;
            var page <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">frame</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span> <span style="color: #0600FF; font-weight: bold;">as</span> PhoneApplicationPage<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>page <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                Panel host <span style="color: #008000;">=</span> page<span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span> <span style="color: #0600FF; font-weight: bold;">as</span> Panel<span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>host <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> <span style="color: #008000;">!</span>host<span style="color: #008000;">.</span><span style="color: #0000FF;">Children</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Any</span><span style="color: #008000;">&#40;</span>c <span style="color: #008000;">=&gt;</span> c <span style="color: #008000;">is</span> MemoryWatcher<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">timer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Start</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    host<span style="color: #008000;">.</span><span style="color: #0000FF;">Children</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnFrameNavigating<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, NavigatingCancelEventArgs navigatingCancelEventArgs<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            var page <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">frame</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span> <span style="color: #0600FF; font-weight: bold;">as</span> PhoneApplicationPage<span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>page <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                Panel host <span style="color: #008000;">=</span> page<span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span> <span style="color: #0600FF; font-weight: bold;">as</span> Panel<span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>host <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> host<span style="color: #008000;">.</span><span style="color: #0000FF;">Children</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">timer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Stop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    host<span style="color: #008000;">.</span><span style="color: #0000FF;">Children</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">void</span> OnTimerTick<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> sender, EventArgs e<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">try</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #6666cc; font-weight: bold;">string</span> currentMemory <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>DeviceStatus<span style="color: #008000;">.</span><span style="color: #0000FF;">ApplicationCurrentMemoryUsage</span> <span style="color: #008000;">/</span> ByteToMega<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;#.00&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #6666cc; font-weight: bold;">string</span> peakMemory <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>DeviceStatus<span style="color: #008000;">.</span><span style="color: #0000FF;">ApplicationPeakMemoryUsage</span> <span style="color: #008000;">/</span> ByteToMega<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;#.00&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
                <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span> <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Current: {0}MB Peak: {1}MB&quot;</span>, currentMemory, peakMemory<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">catch</span> <span style="color: #008000;">&#40;</span>Exception<span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">timer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Stop</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><span style="text-decoration: underline;">Note:</span></p>
<ul>
<li>The MemoryWatcher is looking for a Panel type in order to add itself to the list of children in the page. You might want to modify and improve this portion in order to better fit your needs.</li>
<li>The attached project targets Windows Phone 7.1, if you want to use the code in a 7.0 version, you should change the way the memory values are read (see <a href="http://msdn.microsoft.com/en-us/library/microsoft.phone.info.deviceextendedproperties%28v=vs.92%29.aspx">this article</a> for more details)</li>
</ul>
<p>Enjoy the code and start tracking memory leaks <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><a href="http://www.japf.fr/wp-content/uploads/2011/11/MemoryWatcherDemo.zip">Download source code and example</a></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2011/11/track-memory-usage-of-your-windows-phone-7-1-app-in-real-time/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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('p234code7'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2347"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p234code7"><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('p234code8'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2348"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p234code8"><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>6</slash:comments>
		</item>
	</channel>
</rss>

