<?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; performance</title>
	<atom:link href="http://www.japf.fr/tag/performance/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>Windows Phone performance analysis &amp; optimization during TechDays</title>
		<link>http://www.japf.fr/2012/01/windows-phone-performance-analysis-optimization-during-techdays/</link>
		<comments>http://www.japf.fr/2012/01/windows-phone-performance-analysis-optimization-during-techdays/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 07:57:31 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Windows Phone]]></category>
		<category><![CDATA[analysis]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[techdays]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=1065</guid>
		<description><![CDATA[In about 2 weeks now, I&#8217;ll have the chance to be part of the French TechDays in Paris as a speaker. This year, I&#8217;ll own a session called &#8220;Windows Phone performance analysis &#38; optimisation&#8221; with my colleague Charlotte. The agenda looks like the following: why performance analysis ? device vs emulator leveraging WP7 threads using [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.japf.fr/wp-content/uploads/2012/01/LogoTechDays.png" rel="lightbox[1065]"><img class="alignnone size-full wp-image-1067" title="LogoTechDays" src="http://www.japf.fr/wp-content/uploads/2012/01/LogoTechDays.png" alt="" width="223" height="79" /></a></p>
<p>In about 2 weeks now, I&#8217;ll have the chance to be part of the French TechDays in Paris as a speaker. This year, I&#8217;ll own a session called &#8220;Windows Phone performance analysis &amp; optimisation&#8221; with my colleague Charlotte.</p>
<p>The agenda looks like the following:</p>
<ul>
<li>why performance analysis ?</li>
<li>device vs emulator</li>
<li>leveraging WP7 threads</li>
<li>using the VS profiler for WP7</li>
<li>tips and tricks</li>
</ul>
<p>During the session we will use a &#8220;real&#8221; app we&#8217;re working on for a few months now (I&#8217;ll share more details after the session). We have some cool tips that haven&#8217;t been shared anywhere before, so if performance is a topic of interest for you, stat tuned !</p>
<p>Click on the following image for a link to the TechDays website:</p>
<p><a href="http://www.microsoft.com/france/mstechdays/programmes/parcours.aspx#DomID=9e0aee6e-9896-4c59-ad3f-d4cd48109eaa&amp;SessionID=db46a2a8-566e-4954-8903-e133f2217857"><img class="alignnone  wp-image-1066" title="SessionTechDays" src="http://www.japf.fr/wp-content/uploads/2012/01/SessionTechDays.png" alt="" width="553" height="281" /></a></p>
<p>I&#8217;m planning to share the most of the content of this session on my blog soon after the event.</p>
<p>Don&#8217;t hesitate to stop by and say hi&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2012/01/windows-phone-performance-analysis-optimization-during-techdays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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>How to measure rendering time in a WPF application ?</title>
		<link>http://www.japf.fr/2009/10/measure-rendering-time-in-a-wpf-application/</link>
		<comments>http://www.japf.fr/2009/10/measure-rendering-time-in-a-wpf-application/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 10:47:20 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[WPF]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[practises]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=325</guid>
		<description><![CDATA[Last week, a colleague of mine asked me an interesting question: &#8220;I&#8217;m filling a control with content and I&#8217;d like to measure the time needed to render my control. How can I do that ?&#8221; The first approach is to measure the elapsed time needed to instantiate and populate the control from C# code. We [...]]]></description>
			<content:encoded><![CDATA[<p>Last week, a colleague of mine asked me an interesting question: &#8220;<em>I&#8217;m filling a control with content and I&#8217;d like to measure the time needed to render my control. How can I do that ?&#8221;</em></p>
<p>The first approach is to measure the elapsed time needed to instantiate and populate the control from C# code. We can use the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx">StopWatch</a> class to have a precise and easy to use measuring tool.</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('p325code7'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3257"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code" id="p325code7"><pre class="csharp" style="font-family:monospace;">Stopwatch sw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Stopwatch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
sw<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>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">5000</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// here is the operation that fills the control</span>
  <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">canvas</span><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: #008000;">new</span> Rectangle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
sw<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>
MessageBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Took &quot;</span> <span style="color: #008000;">+</span> sw<span style="color: #008000;">.</span><span style="color: #0000FF;">ElapsedMilliseconds</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; ms&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>However this approach will not give good results because we&#8217;re not taking into account the time needed to render elements in the visual tree. This is because elements are not rendered when you call the Add methods (for example in a Canvas) but when the visual tree is fully loaded.</p>
<p>A much better approach is to use the Dispatcher and request it to process an action at a priority right bellow the Render priority which is the Loaded priority:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2009/10/dispatcherpriority.png" rel="lightbox[325]"><img class="alignnone size-full wp-image-326" title="dispatcherpriority" src="http://www.japf.fr/wp-content/uploads/2009/10/dispatcherpriority.png" alt="dispatcherpriority" width="705" height="188" /></a></p>
<p>Using this trick, we ensure that all rendering actions have been completed. We can use the following code to do that:</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('p325code8'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p3258"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p325code8"><pre class="csharp" style="font-family:monospace;">Stopwatch sw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Stopwatch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
sw<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>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> <span style="color: #FF0000;">5000</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// here is the operation that fills the control</span>
  <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">canvas</span><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: #008000;">new</span> Rectangle<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</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;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Dispatcher</span><span style="color: #008000;">.</span><span style="color: #0000FF;">BeginInvoke</span><span style="color: #008000;">&#40;</span>
  DispatcherPriority<span style="color: #008000;">.</span><span style="color: #0000FF;">Loaded</span>,
  <span style="color: #008000;">new</span> Action<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
  <span style="color: #008000;">&#123;</span>
    sw<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>
    MessageBox<span style="color: #008000;">.</span><span style="color: #0000FF;">Show</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Took &quot;</span> <span style="color: #008000;">+</span> sw<span style="color: #008000;">.</span><span style="color: #0000FF;">ElapsedMilliseconds</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; ms&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Hope this helps !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/10/measure-rendering-time-in-a-wpf-application/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The cost of building Visual Trees</title>
		<link>http://www.japf.fr/2009/10/the-cost-of-building-visual-trees/</link>
		<comments>http://www.japf.fr/2009/10/the-cost-of-building-visual-trees/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 07:36:25 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=268</guid>
		<description><![CDATA[In the current project I&#8217;m working on, I&#8217;m designing a diagramming component. Of course because it&#8217;s for work I can&#8217;t release the source code (however if you want a good starting point you can check out this article). Designing such a component is a challenging task and there are a number of things I learned [...]]]></description>
			<content:encoded><![CDATA[<p>In the current project I&#8217;m working on, I&#8217;m designing a diagramming component. Of course because it&#8217;s for work I can&#8217;t release the source code (however if you want a good starting point you can check out <a href="http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part4.aspx">this article</a>). Designing such a component is a challenging task and there are a number of things I learned during the last few months that I&#8217;m glad to share here.</p>
<p>As you problably know the WPF engine manages 2 trees: the logical tree and the visual tree. For each logical item (that you declare in the XAML), the item is expanded with its visual tree (for example, a scrollbar is made of RepeatButtons and a Thumb). To visualize the visual tree you can use a tool like <a href="http://blois.us/Snoop/">snoop</a>.</p>
<p>In a diagramming tool, you might want to draw lines betweeen entities, like the following:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2009/10/connection.png" rel="lightbox[268]"><img class="alignnone size-medium wp-image-270" title="connection" src="http://www.japf.fr/wp-content/uploads/2009/10/connection-300x71.png" alt="connection" width="300" height="71" /></a></p>
<p>In this article, we&#8217;re going to talk about the underlying control that we can use to do that. If you need to create that kind of control, you basically have 2 options:</p>
<ul>
<li>inherits from Control and override the OnRender method to do the basic graphic operations (such as drawing a line or an ellipse)</li>
<li>inherits from Control and create a ControlTemplate to fill the visual tree of your control</li>
</ul>
<p>Of course the first option has better performance, but it&#8217;s also less evolutive. Moreover, in the OnRender method, you can only do low level graphic operations.</p>
<p>Using the second option is generally fine and easy:</p>
<ol>
<li>create the class that inherit from Control</li>
<li>create a style that targets your new control</li>
<li>add a Setter to set the ControlTemplate property</li>
<li>fill the ControlTemplate&#8217;s content</li>
</ol>
<p>Here is a sample ControlTemplate for a basic connection (assuming that the Connection class has 4 double dependency properties (X1,Y1) (X2,Y2) that defines that start and the end point):</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('p268code10'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p26810"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p268code10"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type VisualTreePerf:Connection}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Template&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type VisualTreePerf:Connection}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Line</span> <span style="color: #000066;">X1</span>=<span style="color: #ff0000;">&quot;{TemplateBinding X1}&quot;</span> <span style="color: #000066;">Y1</span>=<span style="color: #ff0000;">&quot;{TemplateBinding Y1}&quot;</span></span>
<span style="color: #009900;">                  <span style="color: #000066;">X2</span>=<span style="color: #ff0000;">&quot;{TemplateBinding X2}&quot;</span> <span style="color: #000066;">Y2</span>=<span style="color: #ff0000;">&quot;{TemplateBinding Y2}&quot;</span></span>
<span style="color: #009900;">                  <span style="color: #000066;">Stroke</span>=<span style="color: #ff0000;">&quot;Black&quot;</span> <span style="color: #000066;">StrokeThickness</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>                                
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Line<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Of course this is a very minimal template, you&#8217;re probable going to improve it by adding new features:</p>
<ul>
<li> 2 rectangles to show the extremities of the connection</li>
<li>2 thumb to be able to drag&#8217;n'drop the extremities</li>
<li>1 context menu to access connection&#8217;s operations</li>
</ul>
<p>You can do that by adding more controls to the template. Now, take a second and think about the performances. Time is needed to built and render the visual tree and the most complex it is, the longer it takes. I did a quick benchmark on my machine and here are the results for 500 connections added in a Canvas:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2009/10/performance.png" rel="lightbox[268]"><img class="alignnone size-medium wp-image-269" title="performance" src="http://www.japf.fr/wp-content/uploads/2009/10/performance-300x83.png" alt="performance" width="300" height="83" /></a></p>
<p>What we can see is that adding only 5 controls in the template, we multiply the rendering time by 4 ! That means we must be careful when we&#8217;re designing ControlTemplate that are going to be instantiated many time. Before creating the control we should think about whether the control is going to by displayed 1, 10, 100 or 1000 times in our application. More complex ControlTemplates mean more time to render them. Several techniques can be used to improved performance:</p>
<ul>
<li>use a panel that supports UI virtualization (such a VirtualizingStackPanel)</li>
<li>use adorners to give feedback while an item is selected to improve creation performance (create the Adorner only when the item is selected)</li>
</ul>
<p>To explore other performance related techniques:</p>
<ul>
<li> for ItemsControl, make sure to check out Charles Petzold excellent <a href="http://msdn.microsoft.com/en-us/magazine/dd483292.aspx">article on MSDN Magazine</a></li>
<li>for a virtualized canvas, check out <a href="http://blogs.msdn.com/jgoldb/archive/2008/03/08/performant-virtualized-wpf-canvas.aspx">this blog post</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/10/the-cost-of-building-visual-trees/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

