<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Vijay's Weblog</title>
	<atom:link href="http://vijaymp.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://vijaymp.wordpress.com</link>
	<description>Vijay's Weblog</description>
	<lastBuildDate>Mon, 26 Sep 2011 16:14:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='vijaymp.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Vijay's Weblog</title>
		<link>http://vijaymp.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://vijaymp.wordpress.com/osd.xml" title="Vijay&#039;s Weblog" />
	<atom:link rel='hub' href='http://vijaymp.wordpress.com/?pushpress=hub'/>
		<item>
		<title>What is a Memory Leak in .net World?</title>
		<link>http://vijaymp.wordpress.com/2009/01/16/detectingmanagedleak/</link>
		<comments>http://vijaymp.wordpress.com/2009/01/16/detectingmanagedleak/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 18:46:26 +0000</pubDate>
		<dc:creator>vijaymp</dc:creator>
				<category><![CDATA[Managed leak]]></category>
		<category><![CDATA[Memory leak]]></category>
		<category><![CDATA[Windbg]]></category>

		<guid isPermaLink="false">http://vijaymp.wordpress.com/?p=35</guid>
		<description><![CDATA[What is a Memory Leak? In Computer Science, “Memory Leak” is a particular type of unintentional memory consumption by a computer program which occurs when the program fails to release memory that is no longer needed. Usually, this condition arises due to a bug in the program which prevents it from freeing up memory that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=35&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="post-body entry-content"><span style="font-size:85%;"><span style="font-size:100%;"><span style="font-weight:bold;font-family:verdana;">What is a Memory Leak?</span><br />
<span style="font-family:verdana;">In Computer Science, “Memory Leak” is a particular type of unintentional memory consumption by a computer program which occurs when the program fails to release memory that is no longer needed. Usually, this condition arises due to a bug in the program which prevents it from freeing up memory that it no longer needs.</span></p>
<p><span style="font-weight:bold;font-family:verdana;">Does traditional memory leak exist in .Net?</span><br />
<span style="font-family:verdana;">.Net CLR does an excellent job of reclaiming the memory that it has assigned to a program on the request. So, in theory if a memory has been assigned by .Net CLR, it can reclaim this memory by cleaning up our live references. However, we can have a native leak in the following cases:</span><br />
<span style="font-family:verdana;">•    Improper use of Interops</span><br />
<span style="font-family:verdana;">•    Unsafe code </span><br />
<span style="font-family:verdana;">•    GCHandles</span><br />
<span style="font-family:verdana;">•    Failure to dispose the objects that internally use native resources </span></p>
<p><span style="font-family:verdana;">There are many more reasons, but those are not so common. </span></p>
<p><span style="font-weight:bold;font-family:verdana;">Memory Management by .Net CLR </span><br />
<span style="font-family:verdana;">Though .Net CLR manages the memory in an excellent way, it does not take into account any unintentional leak done by a programmer. The “unintentional leak” here means that the programmer fails to remove object references for an object instance that should not be alive anymore.</span></p>
<p><span style="font-weight:bold;font-family:verdana;">Code Snippet</span><br />
<span style="font-family:verdana;">Let us write a simple application where supposedly the programmer wanted to put some data in a hashtable data structure and remove the item from hashtable when the task for that item was over. Apparently, the programmer forgot to write the code to remove items from one of the code flow paths.</span></p>
<p><span style="font-family:courier new;">public partial class Form1 : Form</span><br />
<span style="font-family:courier new;"> {</span><br />
<span style="font-family:courier new;"> private Hashtable hshDataHolder = new Hashtable();</span><br />
<span style="font-family:courier new;"> public Form1()</span><br />
<span style="font-family:courier new;"> {</span><br />
<span style="font-family:courier new;"> InitializeComponent();</span><br />
<span style="font-family:courier new;"> }</span></p>
<p><span style="font-family:courier new;"> private void btnLeak_Click(object sender, EventArgs e)</span><br />
<span style="font-family:courier new;"> {</span><br />
<span style="font-family:courier new;"> for (int i=0; i&lt;50;i++)</span><br />
<span style="font-family:courier new;"> {</span><br />
<span style="font-family:courier new;"> byte[] _data = new byte[1024*1024];</span><br />
<span style="font-family:courier new;"> hshDataHolder.Add(DateTime.Now.Ticks + i,_data);</span><br />
<span style="font-family:courier new;"> Thread.Sleep(1);</span><br />
<span style="font-family:courier new;"> }</span><br />
<span style="font-family:courier new;"> }</span><br />
<span style="font-family:courier new;"> }</span></p>
<p><span style="font-size:100%;"><span style="font-family:verdana;">In this program, things will work fine for a while, as our programmer friend wrote correct business logic to process the data stored in hashtable. However, over a period of time, the memory (specifically, the Virtual memory) consumption by the application shoots up as the data items that should have been removed start piling up.</span></span></p>
<p><span style="font-weight:bold;font-family:verdana;">Detecting a Managed Memory Leak</span><br />
<span style="font-weight:bold;font-style:italic;font-family:verdana;">Code Review</span><br />
<span style="font-family:verdana;">One of the best practices in programming is to have a thorough code review. To an experienced eye, detecting a memory leak is easy if the code footprint is small.</span></p>
<p><span style="font-weight:bold;font-style:italic;font-family:verdana;">WinDbg tool</span><br />
<span style="font-family:verdana;">However, in the case where lines of code run into tens of thousands, it is next to impossible to detect memory leak just by manual review process. This is especially the case if you are not the author of the program </span></p>
<p><span style="font-family:verdana;">Using WinDbg, a code debugging tool, we are better equipped to find a memory leak.</span></p>
<p><span style="font-family:verdana;">WinDbg can be downloaded from Microsoft web site at: <a href="http://www.microsoft.com/whdc/devtools/debugging/installx86.Mspx">http://www.microsoft.com/whdc/devtools/debugging/installx86.Mspx</a>. </span></p>
<p><span style="font-family:verdana;">The tool needs symbols files for the operating system, which can be downloaded at: <a href="http://www.microsoft.com/whdc/DevTools/Debugging/symbolpkg.mspx">http://www.microsoft.com/whdc/DevTools/Debugging/symbolpkg.mspx</a></span></p>
<p><span style="font-family:verdana;">Here are the steps to use WinDbg:</span><br />
<span style="font-family:verdana;">1.    Run the application to be debugged for memory leak.</span></p>
<p><span style="font-family:verdana;">2.    Make sure the application has reached a stage where it has consumed more memory than normally expected (hence, suffering from a ‘memory leak’).</span></p>
<p><span style="font-family:verdana;">3.    Open WinDbg and attach the debugger to the application.</span></p>
<p><span style="font-family:verdana;">4.    In the command box, type </span><br />
<span style="font-style:italic;font-family:verdana;">.loadby sos mscorwks </span><span style="font-family:verdana;">and press Enter. This loads the managed debugging extension SOS (Son Of Strike). This extension provides useful statistics about the managed code.</span></p>
<p><span style="font-family:verdana;">5.    Run the command</span><br />
<span style="font-style:italic;font-family:verdana;">!DumpHeap –stat</span><br />
<span style="font-family:verdana;">This command provides memory usage of each class type in the application. The output reads something like this:</span></p>
<p><span style="font-family:courier new;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span><br />
<span style="font-size:85%;"><span style="font-family:courier new;">MT            Count      Size  Type</span></span><span style="font-size:85%;"><br />
<span style="font-family:courier new;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span><br />
<span style="font-family:courier new;">0b223718       18          720 System.Windows.Forms.Internal.IntNativeMethods+LOGFONT</span><br />
<span style="font-family:courier new;">7ae3c59c       17          748 System.Drawing.BufferedGraphics</span><br />
<span style="font-family:courier new;">79332f40       27         1512 System.Collections.Hashtable</span><br />
<span style="font-family:courier new;">7932b16c       68         1632 System.Collections.Stack</span><br />
<span style="font-family:courier new;">79331a6c       84         1680 System.RuntimeType</span><br />
<span style="font-family:courier new;">7933151c       17         1700 System.Char[]</span><br />
<span style="font-family:courier new;">79330508      143         1716 System.Object</span><br />
<span style="font-family:courier new;">79332a88       20         2032 System.Int32[]</span><br />
<span style="font-family:courier new;">79332178      150         2400 System.Int64</span><br />
<span style="font-family:courier new;">7b22394c       68         4896 System.Windows.Forms.Internal.DeviceContext</span><br />
<span style="font-family:courier new;">648c8c4c       95         5320 System.Configuration.FactoryRecord</span><br />
<span style="font-family:courier new;">7933303c       27        10200 System.Collections.Hashtable+bucket[]</span><br />
<span style="font-family:courier new;">793040bc      120        37832 System.Object[]</span><br />
<span style="font-family:courier new;">793308ec      832        69552 System.String</span><br />
<span style="font-family:courier new;">0015c5c8      675       181748      Free</span><br />
<span style="font-family:courier new;">7933335c      153    157299228 System.Byte[]</span><br />
<span style="font-family:courier new;">Total 3217 objects</span></span></p>
<p><span style="font-family:verdana;font-size:100%;">6.    We see that about 153 MB of memory is taken up by System.Byte[]! That’s a good starting point for our memory leak detection process. Next, let us inspect exactly at which point this memory is consumed by the application.</span></p>
<p>7.    Run<br />
<span style="font-style:italic;">!DumpHeap -type System.Byte[]</span><br />
This command displays all the instances of System.Byte[] created by the application, along with their memory pointers and size of the objects.</p>
<p><span style="font-family:courier new;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span><br />
<span style="font-family:courier new;">Address       MT     Size</span><br />
<span style="font-family:courier new;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span><br />
<span style="font-family:courier new;">0b391110 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0b491130 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0b591150 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0b691170 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0b791190 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0b8911b0 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0ba91000 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0bb91020 7933335c  1048592 </span><br />
<span style="font-family:courier new;"><span style="color:#cc0000;">0bc91040 </span>7933335c  1048592 </span><br />
<span style="font-family:courier new;">0bd91060 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0be91080 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0bf910a0 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0c0910c0 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0c1910e0 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0c291100 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0c391120 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0c491140 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0c591160 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0c691180 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0c7911a0 7933335c  1048592 </span><br />
<span style="font-family:courier new;">0c8911c0 7933335c  1048592 </span><br />
<span style="font-family:courier new;">total 153 objects</span><br />
<span style="font-family:courier new;">Statistics:</span><br />
<span style="font-family:courier new;"> MT    Count    TotalSize Class Name</span><br />
<span style="font-family:courier new;">7933335c      153    157299228 System.Byte[]</span><br />
<span style="font-family:courier new;">Total 153 objects</span></p>
<p><span style="font-size:100%;"><span style="font-family:verdana;">8.    We observe that many byte[] of size about 1 MB are stored somewhere by the application. Let us check if they are live (referenced) instances, and if they are, where they are referenced.</span></span></p>
<p><span style="font-family:verdana;">9.    Run </span><br />
<span style="font-style:italic;font-family:verdana;">!GCRoot 0bc91040 </span><br />
<span style="font-family:verdana;">Here 0bc91040 is the memory address of any one of the byte[] instance that we found in the previous step. Display shows an output similar to the following:</span></p>
<p><span style="font-family:courier new;">0:004&gt; !GCRoot 0bc91040 </span><br />
<span style="font-family:courier new;">Note: Roots found on stacks may be false positives. Run &#8220;!help gcroot&#8221; for</span><br />
<span style="font-family:courier new;">more info.</span><br />
<span style="font-family:courier new;">eax:Root:<span style="color:#cc0000;">0128a4ec</span>(WindowsApplication2.Form1)-&gt;</span><br />
<span style="font-family:courier new;"><span style="color:#cc0000;">0128a638</span>(System.Collections.Hashtable)-&gt;</span><br />
<span style="font-family:courier new;">012c1bd4(System.Collections.Hashtable+bucket[])</span><br />
<span style="font-family:courier new;">Scan Thread 0 OSTHread c6c</span><br />
<span style="font-family:courier new;">Scan Thread 2 OSTHread 8fc</span></p>
<p><span style="font-size:100%;"><span style="font-family:verdana;">10.    Here we can see that the byte[] is referenced (live) and it is stored in a hashtable. (Think this could have been found without looking into the code?). The hashtable is created in Form1 class. This limits our scope further, as now we know that we have to look for a hashtable which is declared in Form1 class which stores a byte[].</span></span></p>
<p><span style="font-family:verdana;">11.    Let us dump the object Form1 (that stores the hashtable in question) by the command</span><br />
<span style="font-style:italic;font-family:verdana;">!do 0128a4ec</span><br />
<span style="font-family:verdana;"> Here 0128a4ec is the address of Form1</span></p>
<p><span style="font-family:verdana;">Display shows output similar to the following:</span></p>
<p><span style="font-family:courier new;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span><br />
<span style="font-family:courier new;"> MT    Field   Offset                 Type VT     Attr    Value Name </span><br />
<span style="font-family:courier new;">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</span><br />
<span style="font-family:courier new;">79332b38  4001e82      fe0         System.Int32  1   static       58 PropMainMenuStrip</span><br />
<span style="font-family:courier new;">79332b38  4001e83      fe4         System.Int32  1   static       59 PropMdiWindowListStrip</span><br />
<span style="font-family:courier new;">79332b38  4001e84      fe8         System.Int32  1   static       60 PropMdiControlStrip</span><br />
<span style="font-family:courier new;">79332b38  4001e85      fec         System.Int32  1   static       61 PropSecurityTip</span><br />
<span style="font-family:courier new;">79332b38  4001e86      ff0         System.Int32  1   static       62 PropOpacity</span><br />
<span style="font-family:courier new;">79332b38  4001e87      ff4         System.Int32  1   static       63 PropTransparencyKey</span><br />
<span style="font-family:courier new;">79330508  4001e9a      bc0        System.Object  0   static 0128aa04 EVENT_MAXIMIZEDBOUNDSCHANGED</span><br />
<span style="font-family:courier new;">00000000  4000004      13c                       0 instance 00000000 components</span><br />
<span style="font-family:courier new;">7b21a924  4000005      140 &#8230;dows.Forms.Button  0 instance 012a8f14 btnLeak</span><br />
<span style="font-family:courier new;">79332f40  4000006      144 &#8230;ections.Hashtable  0 instance <span style="color:#cc0000;">0128a638 </span>hshDataHolder</span></p>
<p><span style="font-size:100%;"><span style="font-family:verdana;">12.    Studying the above output, we ascertain the name of the hashtable as hshDataHolder. </span></span></p>
<p><span style="font-family:verdana;">This definitely makes it easier to find the related code! Next, it is up to the programmer to refine and fine-tune the lines of code in order to get rid of the memory leak.</span></p>
<p></span></span></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vijaymp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vijaymp.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vijaymp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vijaymp.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vijaymp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vijaymp.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vijaymp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vijaymp.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vijaymp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vijaymp.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vijaymp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vijaymp.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vijaymp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vijaymp.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=35&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vijaymp.wordpress.com/2009/01/16/detectingmanagedleak/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ebf6feed3efc092ee4582667fdac0659?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vijaymp</media:title>
		</media:content>
	</item>
		<item>
		<title>Writing Fast Code the easy way Part IV</title>
		<link>http://vijaymp.wordpress.com/2008/10/21/writing-fast-code-the-easy-way-part-iv-2/</link>
		<comments>http://vijaymp.wordpress.com/2008/10/21/writing-fast-code-the-easy-way-part-iv-2/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 09:29:46 +0000</pubDate>
		<dc:creator>vijaymp</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[Initializing]]></category>
		<category><![CDATA[LOH (Large Object Heap)]]></category>
		<category><![CDATA[Scope]]></category>
		<category><![CDATA[Using Variables]]></category>

		<guid isPermaLink="false">http://vijaymp.wordpress.com/?p=32</guid>
		<description><![CDATA[Using Variables Initializing We all have a habit of initializing variables with their default values as soon as we declare them. We also have a tendency to assign the object variables to null when we think we don’t need the variable any more. Well I thought the above procedure to be correct and a standard [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=32&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><span style="font-size:100%;"><strong><span style="font-family:Verdana;"><span style="font-weight:bold;font-family:Verdana;">Using Variables</span></span></strong></span></p>
<p class="MsoNormal"><span style="font-size:100%;"><strong><span style="font-family:Verdana;"><span style="font-weight:bold;font-family:Verdana;">Initializing </span></span></strong></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">We all have a habit of initializing variables with their default values as soon as we declare them. We also have a tendency to assign the object variables to null when we think we don’t need the variable any more.</span></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">Well I thought the above procedure to be correct and a standard way, and I myself had advocated the way for quite some time in past until I found the following.</span></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:100%;"><em><span style="font-family:Verdana;"><span style="font-style:italic;font-family:Verdana;">Once space for the object is allocated, it remains to initialize it (construct it). The CLR guarantees that all object references are pre-initialized to null, and all primitive scalar types are initialized to 0, 0.0, false, etc. (Therefore it is unnecessary to redundantly do so in your user-defined constructors. Feel free, of course. But be aware that the JIT compiler currently does not necessarily optimize away your redundant stores.)</span></span></em></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">What will we benefit if we don’t redundantly initialize the variable? Well it will be the CPU cycles that go into loading the variable in memory and assigning a value. Redundant Initializing is nothing but variable assignment! Same is true when we assign null to object references when they are supposed to go out of scope. GC will automatically collect those variables that have gone out of scope. If the object reference implements IDisposable, call the dispose() for sure, if its has close(), clear(), Stop(), and you don&#8217;t need the object reference, make sure to call that method which is supposed to release the resources. Not doing above and setting it to null is leaking memory and in addition wasting CPU cycles. </span></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:100%;"><strong><span style="font-family:Verdana;"><span style="font-weight:bold;font-family:Verdana;">Scope</span></span></strong></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">When we write a function block we normally declare variables on top of the function, some people nicely declaring the primitive types in a section and object variable in a separate, which make the code look beautiful (I agree on that)</span></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">However when we want the GC to work efficiently for us and memory usage/leak is a concern, it’s always better to declare variables very near to the scope the variable will have. </span></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">Benefit, </span></span></p>
<ol style="margin-top:0;" type="1">
<li class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">The      memory allocation will take place only when required.</span></span></li>
<li class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">The memory de-allocation will take sooner as GC will known that variable is no longer required as soon the variable will exit his scope.</span></span></li>
</ol>
<p class="MsoNormal"><span style="font-size:100%;"><strong><span style="font-family:Verdana;"><span style="font-weight:bold;font-family:Verdana;"> </span></span></strong></span></p>
<p class="MsoNormal"><span style="font-size:100%;"><strong><span style="font-family:Verdana;"><span style="font-weight:bold;font-family:Verdana;">Caching</span></span></strong></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">Caching values is one of the best optimization techniques that I have found so far (of course writing efficient code has no match and I believe caching is part of that).</span></span></p>
<p class="MsoNormal"><span style="font-size:100%;"><strong><span style="font-family:Verdana;"><span style="font-weight:bold;font-family:Verdana;"> </span></span></strong></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">Always cache a property value if it’s used more more than once, and make a habit to cache a value returned from a function even more as generally the function has to do some data churning, may be DB access to get you the desired output. You can save those CPU cycles and memory allocations/de-allocations if you cache those values.</span></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;"><strong></strong></span></span></p>
<p class="MsoNormal"><span style="font-size:100%;"><strong><span style="font-family:Verdana;"><span style="font-weight:bold;font-family:Verdana;">LOH (Large Object Heap)</span></span></strong></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">I read on MSDN that LOH is not compacted like other generations. On another site I read that heavy allocation and de-allocation of large objects (size more than ~85K) will fragment LOH heavily and sooner or later a time will come that even though we will have sufficient free memory available, but all fragmented with no contiguous block. Requesting large object memory from GC at this juncture will result in OutOfMemory exception.</span></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">To address this problem currently I can think of the following:</span></span></p>
<ol style="margin-top:0;" type="1">
<li class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">Don’t      create objects which can possibly land up in LOH. Break them into smaller      units.</span></span></li>
<li class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">Don’t allocate and de-allocate large objects frequently, especially for server end applications as they are supposed to run 24&#215;7 and they are sure candidate to fall in this trap. </span></span></li>
<li class="MsoNormal"><span style="font-size:100%;font-family:Verdana;"><span style="font-family:Verdana;">I don’t know for sure if reusing large object variable variables (without de-allocating and re-allocating) will help in this aspect, I will have to test this to be sure. You are welcome to post your comments if you have tried this before.</span></span></li>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vijaymp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vijaymp.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vijaymp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vijaymp.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vijaymp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vijaymp.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vijaymp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vijaymp.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vijaymp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vijaymp.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vijaymp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vijaymp.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vijaymp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vijaymp.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=32&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vijaymp.wordpress.com/2008/10/21/writing-fast-code-the-easy-way-part-iv-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ebf6feed3efc092ee4582667fdac0659?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vijaymp</media:title>
		</media:content>
	</item>
		<item>
		<title>Writing Fast Code the easy way Part III</title>
		<link>http://vijaymp.wordpress.com/2008/10/21/writing-fast-code-the-easy-way-part-iii/</link>
		<comments>http://vijaymp.wordpress.com/2008/10/21/writing-fast-code-the-easy-way-part-iii/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 09:25:27 +0000</pubDate>
		<dc:creator>vijaymp</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://vijaymp.wordpress.com/?p=28</guid>
		<description><![CDATA[Switch Vs If Use of Switch instead of If whenever possible can be quite helpful, especially if its inside a loop or any code block which is executed frequently. It can give a performance boost up to 4 times ! Check out the sample skeleton code below and the performance readings for the same. public [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=28&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><span style="font-size:100%;"><strong><span style="font-family:Verdana;"><span style="font-weight:bold;font-family:Verdana;">Switch Vs If</span></span></strong></span></p>
<p class="MsoNormal"><span style="font-size:100%;"><strong><span style="font-family:Verdana;"><span style="font-weight:bold;font-family:Verdana;"> </span></span></strong></span></p>
<p class="MsoNormal"><span style="font-family:Verdana;font-size:100%;"><span style="font-family:Verdana;">Use of Switch instead of If whenever possible can be quite helpful, especially if its inside a loop or any code block which is executed frequently. It can give a performance boost up to 4 times !</span></span></p>
<p class="MsoNormal"><span style="font-family:Verdana;font-size:100%;"><span style="font-family:Verdana;"> </span></span></p>
<p class="MsoNormal"><span style="font-family:Verdana;font-size:100%;"><span style="font-family:Verdana;">Check out the sample skeleton code below and the performance readings for the same.</span></span></p>
<p class="MsoNormal"><span style="font-family:Verdana;font-size:100%;"><span style="font-family:Verdana;"> </span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">public</span></span> <span style="color:blue;"><span style="color:blue;">void</span></span> SwitchTest()</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> {</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">int</span></span> i = 0;</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">while</span></span> (LOOPCOUNT &gt;= i++)</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> {</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">switch</span></span> (i)</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> {</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">case</span></span> 10:</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">break</span></span>;</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">case</span></span> 20:</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">break</span></span>;</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">case</span></span> 30:</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">break</span></span>;</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">case</span></span> 40:</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">break</span></span>;</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">case</span></span> 50:</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">break</span></span>;</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">case</span></span> 60:</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">break</span></span>;</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">case</span></span> 70:</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">break</span></span>;</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> }</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> </span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> }</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> }</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">public</span></span> <span style="color:blue;"><span style="color:blue;">void</span></span> IfTest()</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> {</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">int</span></span> i = 0;</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">while</span></span> (LOOPCOUNT &gt;= i++)</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> {</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">if</span></span> (i == 10)</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> {</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> }</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">else</span></span> <span style="color:blue;"><span style="color:blue;">if</span></span> (i == 20)</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> {</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> }</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">else</span></span> <span style="color:blue;"><span style="color:blue;">if</span></span> (i == 30)</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> {</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> }</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">else</span></span> <span style="color:blue;"><span style="color:blue;">if</span></span> (i == 40)</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> {</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> }</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">else</span></span> <span style="color:blue;"><span style="color:blue;">if</span></span> (i == 50)</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> {</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> }</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">else</span></span> <span style="color:blue;"><span style="color:blue;">if</span></span> (i == 60)</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> {</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> }</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> <span style="color:blue;"><span style="color:blue;">else</span></span> <span style="color:blue;"><span style="color:blue;">if</span></span> (i == 70)</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> {</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> }</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> </span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> }</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"> }</span></span></p>
<p class="MsoNormal"><span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"><br />
</span></span></p>
<p class="MsoNormal"><a href="http://2.bp.blogspot.com/_ZNxZF4XoqOA/SMOKrR03avI/AAAAAAAABQA/9Ru4GXcyv5k/s1600-h/Switch.PNG"><img style="display:block;text-align:center;cursor:pointer;margin:0 auto 10px;" src="http://2.bp.blogspot.com/_ZNxZF4XoqOA/SMOKrR03avI/AAAAAAAABQA/9Ru4GXcyv5k/s400/Switch.PNG" border="0" alt="" /></a><br />
<span style="font-family:Courier New;font-size:100%;"><span style="font-family:&quot;"></span></span></p>
<p class="MsoNormal"><span style="font-size:100%;"><strong><span style="font-family:Verdana;"><span style="font-weight:bold;font-family:Verdana;"> </span></span></strong></span></p>
<p><span style="font-family:Verdana;font-size:100%;"><span style="font-family:Verdana;"><span style="font-style:italic;">The above readings were generated with exact same code listed above and measured using DevPartner Performance expert. Its highly recommended that before applying any performance tips in your project, make sure to measure the performance yourself.</span></span></span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vijaymp.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vijaymp.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vijaymp.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vijaymp.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vijaymp.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vijaymp.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vijaymp.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vijaymp.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vijaymp.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vijaymp.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vijaymp.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vijaymp.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vijaymp.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vijaymp.wordpress.com/28/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=28&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vijaymp.wordpress.com/2008/10/21/writing-fast-code-the-easy-way-part-iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ebf6feed3efc092ee4582667fdac0659?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vijaymp</media:title>
		</media:content>

		<media:content url="http://2.bp.blogspot.com/_ZNxZF4XoqOA/SMOKrR03avI/AAAAAAAABQA/9Ru4GXcyv5k/s400/Switch.PNG" medium="image" />
	</item>
		<item>
		<title>Writing Fast Code the easy way Part II</title>
		<link>http://vijaymp.wordpress.com/2008/10/21/writing-fast-code-the-easy-way-part-ii/</link>
		<comments>http://vijaymp.wordpress.com/2008/10/21/writing-fast-code-the-easy-way-part-ii/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 09:20:10 +0000</pubDate>
		<dc:creator>vijaymp</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Compuware DevPartner]]></category>
		<category><![CDATA[Fast code]]></category>
		<category><![CDATA[If conditions]]></category>

		<guid isPermaLink="false">http://vijaymp.wordpress.com/?p=22</guid>
		<description><![CDATA[Writing If conditions The way we write If conditions can dramatically affect the performance, especially if its in a loop churning out lot of data. Check out following two sample code (the If conditions are meaningless and are just put to simulate some conditional processing) public void IfNested() { int i = 0; while (LOOPCOUNT [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=22&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="post-body entry-content"><!--[if gte mso 9]&gt;  Normal 0   false false false        MicrosoftInternetExplorer4  &lt;![endif]--><!--[if gte mso 9]&gt;   &lt;![endif]--></p>
<p class="MsoNormal"><strong><span style="font-size:11pt;font-family:Verdana;">Writing If conditions</span></strong></p>
<p class="MsoNormal"><strong><span style="font-size:11pt;font-family:Verdana;"> </span></strong></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;">The way we write <strong>If</strong> conditions can dramatically affect the performance, especially if its in a loop churning out lot of data.</span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;"> </span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;">Check out following two sample code (the If conditions are meaningless and are just put to simulate some conditional processing)</span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">public</span> <span style="color:blue;">void</span> IfNested()</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">int</span> i = 0;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">while</span> (LOOPCOUNT &gt;= i++)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">if</span> (i &gt; 20)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">if</span> (i &gt; 40)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">if</span> (i &gt; 60)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">public</span> <span style="color:blue;">void</span> IfCombined()</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">int</span> i = 0;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">while</span> (LOOPCOUNT &gt;= i++)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">if</span> (i &gt; 20 &amp;&amp; i &gt; 40 &amp;&amp; i &gt; 60)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;">Out of the above two, combined condition is a winner as it benefits from conditional short circuiting optimization in .net. It’s also always a good move to put such conditions first in order which can help the runtime to skip executing unnecessary code blocks in advance. </span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;"> </span></p>
<p><span style="font-size:11pt;font-family:Verdana;">If( CondA &amp;&amp; CondB &amp;&amp; CondC) is a good one if we know that CondA can be false most of the times. If CondC is the one which can be false most of the times, the if block should be written as If( CondC &amp;&amp; CondB &amp;&amp; CondA).</span></p>
<p><a href="http://3.bp.blogspot.com/_ZNxZF4XoqOA/SMKnTt58rbI/AAAAAAAABP4/l78GDQd4v4s/s1600-h/IF.PNG"><img style="display:block;text-align:center;cursor:pointer;margin:0 auto 10px;" src="http://3.bp.blogspot.com/_ZNxZF4XoqOA/SMKnTt58rbI/AAAAAAAABP4/l78GDQd4v4s/s400/IF.PNG" border="0" alt="" /></a><!--[if gte mso 9]&gt;  Normal 0   false false false        MicrosoftInternetExplorer4  &lt;![endif]--><!--[if gte mso 9]&gt;   &lt;![endif]--> <!--[if gte mso 10]&gt;   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:"Table Normal"; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-parent:""; 	mso-padding-alt:0in 5.4pt 0in 5.4pt; 	mso-para-margin:0in; 	mso-para-margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:10.0pt; 	font-family:"Times New Roman"; 	mso-ansi-language:#0400; 	mso-fareast-language:#0400; 	mso-bidi-language:#0400;}  &lt;![endif]--></p>
<p class="MsoNormal" style="font-style:italic;"><span style="font-size:11pt;font-family:Verdana;"><br />
</span></p>
<p class="MsoNormal" style="font-style:italic;"><span style="font-size:11pt;font-family:Verdana;">The above readings were generated with exact same code listed above and measured using DevPartner Performance expert. Its highly recommended that before applying any performance tips in your project, make sure to measure the performance yourself.</span></p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vijaymp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vijaymp.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vijaymp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vijaymp.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vijaymp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vijaymp.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vijaymp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vijaymp.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vijaymp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vijaymp.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vijaymp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vijaymp.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vijaymp.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vijaymp.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=22&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vijaymp.wordpress.com/2008/10/21/writing-fast-code-the-easy-way-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ebf6feed3efc092ee4582667fdac0659?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vijaymp</media:title>
		</media:content>

		<media:content url="http://3.bp.blogspot.com/_ZNxZF4XoqOA/SMKnTt58rbI/AAAAAAAABP4/l78GDQd4v4s/s400/IF.PNG" medium="image" />
	</item>
		<item>
		<title>Writing Fast Code the easy way Part I</title>
		<link>http://vijaymp.wordpress.com/2008/10/21/writing-fast-code-the-easy-way-part-i/</link>
		<comments>http://vijaymp.wordpress.com/2008/10/21/writing-fast-code-the-easy-way-part-i/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 09:14:49 +0000</pubDate>
		<dc:creator>vijaymp</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Compuware DevPartner]]></category>
		<category><![CDATA[efficient code]]></category>
		<category><![CDATA[Fast code]]></category>
		<category><![CDATA[stringbuilder vs String]]></category>

		<guid isPermaLink="false">http://vijaymp.wordpress.com/?p=19</guid>
		<description><![CDATA[String or StringBuilder? Which is better string or StringBuilder? We all know that StringBuilder is the better choice. Really? If yes, then by what ratio. Let’s check it out. We have following code blocks which perform exactly same, but are written in different ways private const int LOOPCOUNT = 1000; private void StringBuilderAppend() { StringBuilder [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=19&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div class="post-body entry-content"><!--[if !mso]&gt;  v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);}  &lt;![endif]--><!--[if gte mso 9]&gt;  Normal 0   false false false        MicrosoftInternetExplorer4  &lt;![endif]--><!--[if gte mso 9]&gt;   &lt;![endif]--></p>
<p class="MsoNormal"><strong><span style="font-size:100%;font-family:Verdana;">String or StringBuilder?<br />
</span></strong></p>
<p><!--[if gte mso 9]&gt;  Normal 0   false false false        MicrosoftInternetExplorer4  &lt;![endif]--><!--[if gte mso 9]&gt;   &lt;![endif]--> <!--[if gte mso 10]&gt;   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:"Table Normal"; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-parent:""; 	mso-padding-alt:0in 5.4pt 0in 5.4pt; 	mso-para-margin:0in; 	mso-para-margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:10.0pt; 	font-family:"Times New Roman"; 	mso-ansi-language:#0400; 	mso-fareast-language:#0400; 	mso-bidi-language:#0400;}  &lt;![endif]--></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;">Which is better string or StringBuilder? We all know that StringBuilder is the better choice. Really? If yes, then by what ratio.</span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;"> </span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;">Let’s check it out.</span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;"> </span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;">We have following code blocks which perform exactly same, but are written in different ways</span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">private</span> <span style="color:blue;">const</span> <span style="color:blue;">int</span> LOOPCOUNT = 1000;</span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">private</span> <span style="color:blue;">void</span> StringBuilderAppend()</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:#2b91af;">StringBuilder</span> _subElementValue = <span style="color:blue;">new</span> <span style="color:#2b91af;">StringBuilder</span>();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">int</span> i = 0;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">while</span> (i++ &lt;= LOOPCOUNT)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>_subElementValue.Append(<span style="color:#a31515;">&#8220;_xmlStart&#8221;</span>);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>_subElementValue.Append(<span style="color:#a31515;">&#8220;nav3Name&#8221;</span>);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>_subElementValue.Append(<span style="color:#a31515;">&#8220;_xmlEnd&#8221;</span>);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>_subElementValue.Append(<span style="color:#a31515;">&#8220;nav3.Value&#8221;</span>);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>_subElementValue.Append(<span style="color:#a31515;">&#8220;_xmlEndStart&#8221;</span>);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>_subElementValue.Append(<span style="color:#a31515;">&#8220;nav3Name&#8221;</span>);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>_subElementValue.Append(<span style="color:#a31515;">&#8220;_xmlEnd&#8221;</span>);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">private</span> <span style="color:blue;">void</span> StringConcatenate()</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">int</span> i = 0;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">string</span> sTemp = <span style="color:#a31515;">&#8220;&#8221;</span>;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span> </span><span style="color:blue;">while</span> (i++ &lt;= LOOPCOUNT)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>sTemp += <span style="color:#a31515;">&#8220;_xmlStart&#8221;</span>;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>sTemp += <span style="color:#a31515;">&#8220;nav3Name&#8221;</span>;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>sTemp += <span style="color:#a31515;">&#8220;_xmlEnd&#8221;</span>;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>sTemp += <span style="color:#a31515;">&#8220;nav3.Value&#8221;</span>;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>sTemp += <span style="color:#a31515;">&#8220;_xmlEndStart&#8221;</span>;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>sTemp += <span style="color:#a31515;">&#8220;nav3Name&#8221;</span>;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>sTemp += <span style="color:#a31515;">&#8220;_xmlEnd&#8221;</span>;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;">As expected code block using StringBuilder.Append is quite fast compared to the string += operations. It’s 347 times faster than the string += operations! It’s a clear choice when concatenating multiple strings. What if we need the fastest code to be a little faster without killing the code readability and jumping into unsafe codes?</span><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;">The following code is much faster than the <strong>StringBuilderAppend</strong> method. It’s faster by a factor of 2.4 times. What we simply did was used multiple string + operations (note there is no +=) and appended it in a StringBuilder.</span></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">private</span> <span style="color:blue;">void</span> StringBuilderAndStringConcatenate()</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:#2b91af;">StringBuilder</span> _subElementValue = <span style="color:blue;">new</span> <span style="color:#2b91af;">StringBuilder</span>();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">int</span> i = 0;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:blue;">while</span> (i++ &lt;= LOOPCOUNT)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>{</span></p>
<p class="MsoNormal" style="margin-left:1.5in;"><span style="font-size:10pt;font-family:&quot;">_subElementValue.Append(<span style="color:#a31515;">&#8220;_xmlStart&#8221;</span> + <span style="color:#a31515;">&#8220;nav3Name&#8221;</span> + <span style="color:#a31515;">&#8220;_xmlEnd&#8221;</span> + <span style="color:#a31515;">&#8220;nav3.Value&#8221;</span> + <span style="color:#a31515;">&#8220;_xmlEndStart&#8221;</span> +</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span><span style="color:#a31515;">&#8220;nav3Name&#8221;</span> + <span style="color:#a31515;">&#8220;_xmlEnd&#8221;</span>);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p><span style="font-size:10pt;font-family:&quot;"><span> </span>}</span></p>
<p class="MsoNormal">
<p class="MsoNormal"><span style="font-family:&quot;"> </span></p>
<p class="MsoNormal"><span style="font-family:&quot;"><!--[if gte vml 1]&gt;                    &lt;![endif]--><!--[if !vml]--></span></p>
<p class="MsoNormal"><a href="http://1.bp.blogspot.com/_ZNxZF4XoqOA/SMJ6TG8gWmI/AAAAAAAABPw/oPlS1xOiOxs/s1600-h/StringvsStringBuilder.PNG"><img style="display:block;text-align:center;cursor:pointer;margin:0 auto 10px;" src="http://1.bp.blogspot.com/_ZNxZF4XoqOA/SMJ6TG8gWmI/AAAAAAAABPw/oPlS1xOiOxs/s400/StringvsStringBuilder.PNG" border="0" alt="" /></a><br />
<span style="font-family:&quot;"><!--[endif]--></span></p>
<p class="MsoNormal"><span style="font-family:&quot;"> </span></p>
<p class="MsoNormal"><!--[if gte mso 9]&gt;  Normal 0   false false false        MicrosoftInternetExplorer4  &lt;![endif]--><!--[if gte mso 9]&gt;   &lt;![endif]--> <!--[if gte mso 10]&gt;   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:"Table Normal"; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-parent:""; 	mso-padding-alt:0in 5.4pt 0in 5.4pt; 	mso-para-margin:0in; 	mso-para-margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:10.0pt; 	font-family:"Times New Roman"; 	mso-ansi-language:#0400; 	mso-fareast-language:#0400; 	mso-bidi-language:#0400;}  &lt;![endif]--></p>
<p class="MsoNormal"><span style="font-size:11pt;font-family:Verdana;"><span style="font-style:italic;">The above readings were generated with exact same code listed above and measured using DevPartner Performance expert. Its highly recommended that before applying any performance tips in your project, make sure to measure the performance yourself.</span></span></p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vijaymp.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vijaymp.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vijaymp.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vijaymp.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vijaymp.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vijaymp.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vijaymp.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vijaymp.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vijaymp.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vijaymp.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vijaymp.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vijaymp.wordpress.com/19/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vijaymp.wordpress.com/19/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vijaymp.wordpress.com/19/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=19&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vijaymp.wordpress.com/2008/10/21/writing-fast-code-the-easy-way-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ebf6feed3efc092ee4582667fdac0659?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vijaymp</media:title>
		</media:content>

		<media:content url="http://1.bp.blogspot.com/_ZNxZF4XoqOA/SMJ6TG8gWmI/AAAAAAAABPw/oPlS1xOiOxs/s400/StringvsStringBuilder.PNG" medium="image" />
	</item>
		<item>
		<title>How to use DevPartner for Memory profiling of .net Windows Services</title>
		<link>http://vijaymp.wordpress.com/2008/04/15/how-to-use-devpartner-for-memory-profiling-of-net-windows-services/</link>
		<comments>http://vijaymp.wordpress.com/2008/04/15/how-to-use-devpartner-for-memory-profiling-of-net-windows-services/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 13:22:51 +0000</pubDate>
		<dc:creator>vijaymp</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[Compuware DevPartner]]></category>
		<category><![CDATA[Memory Profile]]></category>
		<category><![CDATA[Windows Services]]></category>

		<guid isPermaLink="false">http://vijaymp.wordpress.com/?p=16</guid>
		<description><![CDATA[I have using the Compuware Devpartner studio for quite some time for profiling our application for memory and performance, and I found it quite useful. Recently we had a memory leak issue in one of our windows service. I tried using the DevPartner profiling for memory on it, but alas I could not profile a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=16&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">I have using the Compuware Devpartner studio for quite some time for profiling our application for memory and performance, and I found it quite useful. Recently we had a memory leak issue in one of our windows service. I tried using the DevPartner profiling for memory on it, but alas I could not profile a service!!</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">This was a setback and now I was in a fix. Either I have to explore new profiling tools to get my job done or dive into the vast code and find the issue. I decided to spend some time on finding if there is some other way to profile the windows services using DevPartner. I Googled a lot and could not find anything <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </span></span><span style="font-size:x-small;font-family:Wingdings;color:#888888;"></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">Later out of frustration I looked into the installation directory, and found lots of exe’s and out of curiosity I opened them one by one. To my pleasant surprise I found one exe named<strong><span style="font-weight:bold;"> </span></strong><strong><span style="font-weight:bold;">DPAnalysis.exe </span></strong>which was what I wanted. It was a command line tool for doing all sorts of Analysis</span></span></p>
<ul style="margin-top:0;" type="disc">
<li class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">Performance</span></span></li>
<li class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">Coverage</span></span></li>
<li class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">Memory </span></span></li>
<li class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">Performance expert</span></span></li>
</ul>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> Following is the help output from that exe, which explains all the different command line arguments.</span></span></p>
<p class="MsoNormal">
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">Usage:</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">1) DPAnalysis [a] [b] [c] [d] {e} target [target args]</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">2) DPAnalysis /config config.xml</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">a) AnalysisType:<span> </span>Set the run-time analysis type. Performance is default.</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/PERF[ORMANCE]<span> </span>Set analysis type to DevPartner Performance Analysis</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/COV[ERAGE]<span> </span>Set analysis type to DevPartner Coverage Analysis</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/MEM[ORY]<span> </span>Set analysis type to DevPartner Memory Analysis</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/EXP[ERT]<span> </span>Set analysis type to DevPartner Performance Expert</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">b) DataCollection:<span> </span>Enable/Disable data collection for a given target.<span> </span>DOES NOT LAUNCH the target.</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/E[NABLE]<span> </span>Enable data collection for the specified process or service</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/D[ISABLE]<span> </span>Disable data collection for the specified process or service</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">c) OtherOptions:</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/O[UTPUT]<span> </span>Specify the session file output directory and/or name with<span> </span>optional extension (.dpprf, .dpcov, .dpmem, or .dppxp)</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/W[ORKINGDIR]<span> </span>Specify the process&#8217; working directory</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/H[OST]<span> </span>Specify target&#8217;s host machine</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/NOWAIT<span> </span>Don&#8217;t wait for process to exit, just wait for it to start</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/N[EWCONSOLE]<span> </span>Run the process in its own command window</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">d) AnalysisOptions:</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/NO_MACH5<span> </span>Disables excluding time spent on other threads</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/NM_METHOD_GRANULARITY<span> </span>Set data collection granularity to method-level<span> </span>(line-level is default)</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/EXCLUDE_SYSTEM_DLLS<span> </span>Exclude data collection for system dlls (Perf only)</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/NM_ALLOW_INLINING<span> </span>Enable run-time instrumentation of inline methods</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/NO_OLEHOOKS<span> </span>Disable collection of COM</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/NM_TRACK_SYSTEM_OBJECTS Track system object allocation (Memory only)</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">e) TargetType: Identify target process or service.<span> </span>MUST BE LAST OPTION.</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">All arguments after the target name/path are passed directly to the target.</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/P[ROCESS]<span> </span>Target is an exe filename (followed by arguments to the process)</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">/S[ERVICE]<span> </span>Target is a service name<span> </span>(followed by arguments to the service)</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">2)/C[ONFIG]<span> </span>Path to configuration file that includes all startup information<span> </span>Note: no other options can be used with /Config</span></span></p>
<p class="MsoNormal">
<p class="MsoNormal">
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">For profiling the windows service I used the following command”</span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"><span> </span><strong>DPAnalysis /MEM /E /S “my service name”</strong></span></span></p>
<p class="MsoNormal"><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;"> </span></span></p>
<p><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">After running the above using a command prompt, I started the service from service controller, and let the service do its job. I monitored the memory usage and VM Size of service using task manager, and when I was convinced that the memory leak condition is reached, I stopped the service. I was prompted for saving the memory analysis file </span></span><span style="font-size:x-small;font-family:Wingdings;color:#888888;"><span style="font-size:10pt;font-family:Wingdings;"><span>J</span></span></span><span style="font-size:x-small;font-family:Trebuchet MS;color:#888888;"><span style="font-size:10pt;">, which could be easily opened up in VS2005 (of course because DevPartner was installed)</span></p>
<p></span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vijaymp.wordpress.com/16/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vijaymp.wordpress.com/16/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vijaymp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vijaymp.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vijaymp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vijaymp.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vijaymp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vijaymp.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vijaymp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vijaymp.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vijaymp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vijaymp.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vijaymp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vijaymp.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vijaymp.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vijaymp.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=16&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vijaymp.wordpress.com/2008/04/15/how-to-use-devpartner-for-memory-profiling-of-net-windows-services/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ebf6feed3efc092ee4582667fdac0659?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vijaymp</media:title>
		</media:content>
	</item>
		<item>
		<title>Use of EventWaitHandle</title>
		<link>http://vijaymp.wordpress.com/2008/03/01/use-of-eventwaithandle/</link>
		<comments>http://vijaymp.wordpress.com/2008/03/01/use-of-eventwaithandle/#comments</comments>
		<pubDate>Sat, 01 Mar 2008 05:29:26 +0000</pubDate>
		<dc:creator>vijaymp</dc:creator>
				<category><![CDATA[EventWaitHandle]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://vijaymp.wordpress.com/2008/03/01/use-of-eventwaithandle/</guid>
		<description><![CDATA[Ever faced a problem where the application does not close even after the main form is closed? If yes, one reason could be the threads you are using. If you are using some threads that run a infinite loop which perform some operation at some intervals with a interval of some minutes or more, and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=15&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><span>Ever faced a problem where the application does not close even after the main form is closed? If yes, one reason could be the threads you are using. If you are using some threads that run a infinite loop which perform some operation at some intervals with a interval of some minutes or more, and you are using Thread.Sleep(), read further.</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span>Consider the following class code snippet.</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>    </span><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">ThreadTest</span></span></p>
<p class="MsoNormal"><span><span>    </span>{</span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:#2b91af;">Thread</span> thSettingsMonitor = <span style="color:blue;">null</span>;</span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">bool</span> _stopTimeOutProcessing = <span style="color:blue;">false</span>;</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">public</span> <span style="color:blue;">override</span> <span style="color:blue;">string</span> ToString()</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">return</span> <span style="color:#a31515;">&#8220;Thread running state is : &#8220;</span> + <span style="color:blue;">this</span>._stopTimeOutProcessing.ToString();</span></p>
<p class="MsoNormal"><span><span>        </span>} </span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">public</span> <span style="color:blue;">void</span> Start()</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span>thSettingsMonitor = <span style="color:blue;">new</span> <span style="color:#2b91af;">Thread</span>(<span style="color:blue;">new</span> <span style="color:#2b91af;">ThreadStart</span>(ReloadSettings));</span></p>
<p class="MsoNormal"><span><span>            </span>thSettingsMonitor.Start();</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:#2b91af;">Debug</span>.WriteLine(<span style="color:#a31515;">&#8220;~~~ Starting the thread. Hash code: &#8220;</span> + <span style="color:blue;">this</span>.GetHashCode().ToString());</span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">public</span> <span style="color:blue;">void</span> Stop()</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">try</span></span></p>
<p class="MsoNormal"><span><span>            </span>{</span></p>
<p class="MsoNormal"><span><span>                </span><span style="color:green;">//set the variable so that the loop breaks and thread terminates</span></span></p>
<p class="MsoNormal"><span><span>                </span>_stopTimeOutProcessing = <span style="color:blue;">true</span>;</span></p>
<p class="MsoNormal"><span><span>            </span>}</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">catch</span> { }</span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">private</span> <span style="color:blue;">void</span> ReloadSettings()</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">while</span> (!_stopTimeOutProcessing)</span></p>
<p class="MsoNormal"><span><span>            </span>{</span></p>
<p class="MsoNormal"><span><span>                </span><span style="color:blue;">try</span></span></p>
<p class="MsoNormal"><span><span>                </span>{</span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:green;">//do some operation in the thread</span></span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:green;">//Mimic by a sleep of 2 seconds</span></span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:#2b91af;">Thread</span>.Sleep(2000);</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:green;">//wait for 5 minutes</span></span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:#2b91af;">Debug</span>.WriteLine(<span style="color:#a31515;">&#8220;~~~ reload thread waiting on sleep for 5 min. Hash code: &#8220;</span> + GetHashCode().ToString());</span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:#2b91af;">Thread</span>.Sleep(30000);</span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:#2b91af;">Debug</span>.WriteLine(<span style="color:#a31515;">&#8220;~~~ Sleep period completed&#8221;</span>);</span></p>
<p class="MsoNormal"><span><span>                </span>}</span></p>
<p class="MsoNormal"><span><span>                </span><span style="color:blue;">catch</span> (<span style="color:#2b91af;">Exception</span> ex)</span></p>
<p class="MsoNormal"><span><span>                </span>{</span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:#2b91af;">Debug</span>.WriteLine(ex);</span></p>
<p class="MsoNormal"><span><span>                </span>}</span></p>
<p class="MsoNormal"><span><span>            </span>}</span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>    </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span>Now in a windows application form write the following code</span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">private</span> <span style="color:#2b91af;">ThreadTest</span> othTest = <span style="color:blue;">new</span> <span style="color:#2b91af;">ThreadTest</span>();</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">private</span> <span style="color:blue;">void</span> button1_Click(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">EventArgs</span> e)</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:green;">//Start the reloadsettings thread</span></span></p>
<p class="MsoNormal"><span><span>            </span>othTest.Start();</span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">private</span> <span style="color:blue;">void</span> FrmTest_FormClosing(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">FormClosingEventArgs</span> e)</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span>othTest.Stop();</span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span>Run the code, click on the button so that the thread starts running. Try closing the form. Note that the form closes but the .net IDE does not breaks until some time. This means that some code is still executing. Click on the pause button of .net IDE to see which code is running. You will notice that the thread code</span><span><span>                    </span><span style="color:#2b91af;">Thread</span>.Sleep(30000); </span><span>is still executing.</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span>Now try the updated code.</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>    </span><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">ThreadTest</span></span></p>
<p class="MsoNormal"><span><span>    </span>{</span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:#2b91af;">Thread</span> thSettingsMonitor = <span style="color:blue;">null</span>;</span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">bool</span> _stopTimeOutProcessing = <span style="color:blue;">false</span>;</span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">private</span> <span style="color:#2b91af;">EventWaitHandle</span> _exitEvent = <span style="color:blue;">new</span> <span style="color:#2b91af;">EventWaitHandle</span>(<span style="color:blue;">false</span>, <span style="color:#2b91af;">EventResetMode</span>.ManualReset);</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">public</span> <span style="color:blue;">override</span> <span style="color:blue;">string</span> ToString()</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">return</span> <span style="color:#a31515;">&#8220;Thread running state is : &#8220;</span> + <span style="color:blue;">this</span>._stopTimeOutProcessing.ToString();</span></p>
<p class="MsoNormal"><span><span>        </span>} </span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">public</span> <span style="color:blue;">void</span> Start()</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span>thSettingsMonitor = <span style="color:blue;">new</span> <span style="color:#2b91af;">Thread</span>(<span style="color:blue;">new</span> <span style="color:#2b91af;">ThreadStart</span>(ReloadSettings));</span></p>
<p class="MsoNormal"><span><span>            </span>thSettingsMonitor.Start();</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:#2b91af;">Debug</span>.WriteLine(<span style="color:#a31515;">&#8220;~~~ Starting the thread. Hash code: &#8220;</span> + <span style="color:blue;">this</span>.GetHashCode().ToString());</span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">public</span> <span style="color:blue;">void</span> Stop()</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">try</span></span></p>
<p class="MsoNormal"><span><span>            </span>{</span></p>
<p class="MsoNormal"><span><span>                </span><span style="color:green;">//set the variable so that the loop breaks and thread terminates</span></span></p>
<p class="MsoNormal"><span><span>                </span>_stopTimeOutProcessing = <span style="color:blue;">true</span>;</span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:blue;">if</span> (_exitEvent != <span style="color:blue;">null</span>)</span></p>
<p class="MsoNormal"><span><span>                    </span>{</span></p>
<p class="MsoNormal"><span><span>                        </span><span style="color:#2b91af;">Debug</span>.WriteLine(<span style="color:#a31515;">&#8220;~~~ Signalling the timeout thread. Hash code: &#8220;</span> + <span style="color:blue;">this</span>.GetHashCode().ToString());</span></p>
<p class="MsoNormal"><span><span>                        </span>_exitEvent.Set();</span></p>
<p class="MsoNormal"><span><span>                        </span><span style="color:#2b91af;">Debug</span>.WriteLine(<span style="color:#a31515;">&#8220;~~~ Signalling the timeout thread done. Hash code: &#8220;</span> + <span style="color:blue;">this</span>.GetHashCode().ToString());</span></p>
<p class="MsoNormal"><span><span>                    </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>  </span><span>          </span>}</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">catch</span> { }</span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">private</span> <span style="color:blue;">void</span> ReloadSettings()</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">while</span> (!_stopTimeOutProcessing)</span></p>
<p class="MsoNormal"><span><span>            </span>{</span></p>
<p class="MsoNormal"><span><span>                </span><span style="color:blue;">try</span></span></p>
<p class="MsoNormal"><span><span>                </span>{</span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:green;">//do some operation in the thread</span></span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:green;">//Mimic by a sleep of 2 seconds</span></span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:#2b91af;">Thread</span>.Sleep(2000);</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:green;">//wait for 5 minutes</span></span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:#2b91af;">Debug</span>.WriteLine(<span style="color:#a31515;">&#8220;~~~ reload thread waiting on eventwait object for 5 min. Hash code: &#8220;</span> + GetHashCode().ToString());</span></p>
<p class="MsoNormal"><span><span>                    </span>_exitEvent.WaitOne(300000, <span style="color:blue;">true</span>);</span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:#2b91af;">Debug</span>.WriteLine(<span style="color:#a31515;">&#8220;~~~ done waiting or the eventwait object was signaled&#8221;</span>);</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>                </span>}</span></p>
<p class="MsoNormal"><span><span>                </span><span style="color:blue;">catch</span> (<span style="color:#2b91af;">Exception</span> ex)</span></p>
<p class="MsoNormal"><span><span>                </span>{</span></p>
<p class="MsoNormal"><span><span>                    </span><span style="color:#2b91af;">Debug</span>.WriteLine(ex);</span></p>
<p class="MsoNormal"><span><span>                </span>}</span></p>
<p class="MsoNormal"><span><span>            </span>}</span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span><span>    </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span>Run the code and note the difference. This time the application closes immediately. </span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span>Let’s understand what was different.</span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">private</span> <span style="color:#2b91af;">EventWaitHandle</span> _exitEvent = <span style="color:blue;">new</span> <span style="color:#2b91af;">EventWaitHandle</span>(<span style="color:blue;">false</span>, <span style="color:#2b91af;">EventResetMode</span>.ManualReset);</span></p>
<p class="MsoNormal"><span>Here we created a thread synchronization object with initial state as non signaled and mode as manual.</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span>Instead of </span><span>Thread</span><span>.Sleep(2000); </span><span>we have now used</span><span> _exitEvent.WaitOne(300000, <span style="color:blue;">true</span>); </span><span>this instructs the thread to wait for 5 minutes (30000 msec) and exit the wait before the time is elapsed if the event object is signaled.</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span>During form close we have used</span><span> _exitEvent.Set();</span><span>. This code signals the event object. When the event object is signaled the thread comes out of wait immediately even before the wait time is completed. The EventWaitHandle is a versatile object when it comes to thread synchronization. Explore it if you are writing multi threaded applications.</span></p>
<p class="MsoNormal"><span>PS: The above problem can be easily worked out in normal scenario by setting the thread as a background thread. The only problem it will have is it will be aborted immediately during application close.</span></p>
<p class="MsoNormal"><span><br />
</span></p>
<p><span>Happy coding!</span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vijaymp.wordpress.com/15/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vijaymp.wordpress.com/15/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vijaymp.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vijaymp.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vijaymp.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vijaymp.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vijaymp.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vijaymp.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vijaymp.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vijaymp.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vijaymp.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vijaymp.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vijaymp.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vijaymp.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vijaymp.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vijaymp.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=15&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vijaymp.wordpress.com/2008/03/01/use-of-eventwaithandle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ebf6feed3efc092ee4582667fdac0659?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vijaymp</media:title>
		</media:content>
	</item>
		<item>
		<title>Override ToString()</title>
		<link>http://vijaymp.wordpress.com/2008/03/01/override-tostring/</link>
		<comments>http://vijaymp.wordpress.com/2008/03/01/override-tostring/#comments</comments>
		<pubDate>Sat, 01 Mar 2008 05:27:36 +0000</pubDate>
		<dc:creator>vijaymp</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Override]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://vijaymp.wordpress.com/2008/03/01/override-tostring/</guid>
		<description><![CDATA[.Net IDE provides excellent support for debugging. One of the supporting tools is the Watch window. It does a very good job when it comes to displaying of data, but it doesn&#8217;t always display the data in the best form, especially for the classes that we write. But that is not the fault of the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=14&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span>.Net IDE provides excellent support for debugging. One of the supporting tools is the Watch window. It does a very good job when it comes to displaying of data, but it doesn&#8217;t always display the data in the best form, especially for the classes that we write. But that is not the fault of the Watch window, its we the developers who are not providing correct implementation of ToString() to our classes.</span></p>
<p class="MsoNormal"><span>The .net debugger calls the ToString() automatically on the instance which is to be monitored in the watch window. If we override the ToString() implementation in our class and provide the key fields as part of implementation, that will make our job a lot easier when it comes to debugging.</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span>See the following code listing. Run it with the ToString() method commented and add a instance of the clsExample class nin watch window and notice the data. Compare it with the original verison.</span></p>
<p class="MsoNormal"><span><br />
</span></p>
<p class="MsoNormal"><span>using</span><span> System.Diagnostics;</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span>namespace</span><span> DebuggererAttribute</span></p>
<p class="MsoNormal"><span>{</span></p>
<p class="MsoNormal"><span><span>    </span><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">clsExample</span></span></p>
<p class="MsoNormal"><span><span>    </span>{</span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">private</span> <span style="color:blue;">int</span> _FieldOne;</span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">private</span> <span style="color:blue;">double</span> _FieldTwo;</span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">private</span> <span style="color:blue;">string</span> _FieldThree;</span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">private</span> <span style="color:blue;">bool</span> _FieldFour;</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">public</span> <span style="color:blue;">int</span> FieldOne</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">get</span></span></p>
<p class="MsoNormal"><span><span>            </span>{</span></p>
<p class="MsoNormal"><span><span>                </span><span style="color:blue;">return</span> _FieldOne;</span></p>
<p class="MsoNormal"><span><span>            </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">set</span></span></p>
<p class="MsoNormal"><span><span>            </span>{</span></p>
<p class="MsoNormal"><span><span>                </span>_FieldOne = <span style="color:blue;">value</span>;</span></p>
<p class="MsoNormal"><span><span>            </span>}</span></p>
<p class="MsoNormal"><span><span>            </span></span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">public</span> <span style="color:blue;">double</span> FieldTwo</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">get</span></span></p>
<p class="MsoNormal"><span><span>            </span>{</span></p>
<p class="MsoNormal"><span><span>                </span><span style="color:blue;">return</span> _FieldTwo;</span></p>
<p class="MsoNormal"><span><span>            </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">set</span></span></p>
<p class="MsoNormal"><span><span>            </span>{</span></p>
<p class="MsoNormal"><span><span>                </span>_FieldTwo = <span style="color:blue;">value</span>;</span></p>
<p class="MsoNormal"><span><span>            </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>    </span><span>    </span><span style="color:blue;">public</span> <span style="color:blue;">string</span> FieldThree</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">get</span></span></p>
<p class="MsoNormal"><span><span>            </span>{</span></p>
<p class="MsoNormal"><span><span>                </span><span style="color:blue;">return</span> _FieldThree;</span></p>
<p class="MsoNormal"><span><span>            </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">set</span></span></p>
<p class="MsoNormal"><span><span>            </span>{</span></p>
<p class="MsoNormal"><span><span>                </span>_FieldThree = <span style="color:blue;">value</span>;</span></p>
<p class="MsoNormal"><span><span>            </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">public</span> <span style="color:blue;">bool</span> FieldFour</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">get</span></span></p>
<p class="MsoNormal"><span><span>            </span>{</span></p>
<p class="MsoNormal"><span><span>                </span><span style="color:blue;">return</span> _FieldFour;</span></p>
<p class="MsoNormal"><span><span>            </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">set</span></span></p>
<p class="MsoNormal"><span><span>            </span>{</span></p>
<p class="MsoNormal"><span><span>                </span>_FieldFour = <span style="color:blue;">value</span>;</span></p>
<p class="MsoNormal"><span><span>            </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">public</span> <span style="color:blue;">override</span> <span style="color:blue;">string</span> ToString()</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span><span style="color:blue;">return</span> <span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&#8220;FieldOne = {0}, FieldTwo = {1}, FieldThree = {2}, FieldFour = {3}&#8221;</span>, _FieldOne, _FieldTwo, _FieldThree, _FieldFour);</span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span><span>        </span><span style="color:blue;">public</span> clsExample()</span></p>
<p class="MsoNormal"><span><span>        </span>{</span></p>
<p class="MsoNormal"><span><span>            </span>_FieldOne = 2;</span></p>
<p class="MsoNormal"><span><span>            </span>_FieldTwo = 3.66;</span></p>
<p class="MsoNormal"><span><span>            </span>_FieldThree = <span style="color:#a31515;">&#8220;This is some text&#8221;</span>;</span></p>
<p class="MsoNormal"><span><span>            </span>_FieldFour = <span style="color:blue;">true</span>;</span></p>
<p class="MsoNormal"><span><span>        </span>}</span></p>
<p class="MsoNormal"><span><span>    </span>}</span></p>
<p><span>}</span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vijaymp.wordpress.com/14/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vijaymp.wordpress.com/14/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vijaymp.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vijaymp.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vijaymp.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vijaymp.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vijaymp.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vijaymp.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vijaymp.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vijaymp.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vijaymp.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vijaymp.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vijaymp.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vijaymp.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vijaymp.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vijaymp.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=14&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vijaymp.wordpress.com/2008/03/01/override-tostring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ebf6feed3efc092ee4582667fdac0659?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vijaymp</media:title>
		</media:content>
	</item>
		<item>
		<title>AutoResetEvent Object and Hanged Application</title>
		<link>http://vijaymp.wordpress.com/2008/03/01/autoresetevent-object-and-hanged-application/</link>
		<comments>http://vijaymp.wordpress.com/2008/03/01/autoresetevent-object-and-hanged-application/#comments</comments>
		<pubDate>Sat, 01 Mar 2008 05:25:35 +0000</pubDate>
		<dc:creator>vijaymp</dc:creator>
				<category><![CDATA[AutoResetEvent]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Server Busy]]></category>

		<guid isPermaLink="false">http://vijaymp.wordpress.com/?p=13</guid>
		<description><![CDATA[This is something related to a bug which was not so reproduce able , but was seen once a week at least on one of the many test machines. We used to get a &#8220;Server busy&#8221; dialog box and Switching or Retrying did not do anything. We have to kill the application. I was accustom [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=13&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is something related to a bug which was not so reproduce able , but was seen once a week at least on one of the many test machines. We used to get a &#8220;Server busy&#8221; dialog box and Switching or Retrying did not do anything. We have to kill the application.</p>
<p>I was accustom to see this dialog when a COM server is busy or is waiting for some resource and usually after some time, Switching or Retrying worked.</p>
<p>Fortunately i got this error on my development machine once and more fortunately i was running a debug build. I paused the program and watched the call stack. The program was waiting on a thread which was in a &#8220;Wait or Sleep/Join state&#8221;. Drilling down more on the thread, it was found that it was one of the our own application thread.</p>
<p>This information gave me a good deal of information on where to look for. I checked the module which created the thread and found that it was created in a user control and the thread was using a AutoResetEvent object. The wait state of the thread as i discovered earlier from call stack was from the Dispose call of the user control. I checked the code to see the places where the AutoResetEvent was closed or signaled. Well the object was not cleaned up/closed in the Dispose call &#8220;which was the problem&#8221;.</p>
<p>The thread was waiting on the AutoResetEvent object and was not allowing the user control to Dispose and hence the application to close.</p>
<p>Learning:<br />
1. Clean up all variables manually, don&#8217;t rely on GC. GC does a good job, but we screw up our selves thinking GC will take care.<br />
2. Use of thread and windows kernel objects like Mutex, Semaphores etc (thread synch objects) are a double edged sword. Always use them with caution.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vijaymp.wordpress.com/13/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vijaymp.wordpress.com/13/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vijaymp.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vijaymp.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vijaymp.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vijaymp.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vijaymp.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vijaymp.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vijaymp.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vijaymp.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vijaymp.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vijaymp.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vijaymp.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vijaymp.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vijaymp.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vijaymp.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=13&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vijaymp.wordpress.com/2008/03/01/autoresetevent-object-and-hanged-application/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ebf6feed3efc092ee4582667fdac0659?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vijaymp</media:title>
		</media:content>
	</item>
		<item>
		<title>How much a Thread cost you?</title>
		<link>http://vijaymp.wordpress.com/2008/01/08/how-much-a-thread-cost-you/</link>
		<comments>http://vijaymp.wordpress.com/2008/01/08/how-much-a-thread-cost-you/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 12:08:45 +0000</pubDate>
		<dc:creator>vijaymp</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Threads]]></category>
		<category><![CDATA[Virtual Memory]]></category>

		<guid isPermaLink="false">http://vijaymp.wordpress.com/2008/01/08/how-much-a-thread-cost-you/</guid>
		<description><![CDATA[I was recently wondering if creating n number of threads will affect the memory usage of an application. I created a sample application which created a thread on a button click to observe the memory usage on each thread creation. The thread proc as such did nothing; I had put a sleep statement in it [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=12&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:85%;"><span style="font-family:trebuchet ms;">I was recently wondering if creating n number of threads will affect the memory usage of an application. I created a sample application which created a thread on a button click to observe the memory usage on each thread creation. The thread proc as such did nothing; I had put a sleep statement in it so that the thread is alive and is using the memory for some time while I am creating some more threads.</span></span></p>
<p style="font-family:trebuchet ms;" class="MsoNormal"><span style="font-size:85%;">I was relaxed to see that the memory usage in task manager rose by just 20K -30 K on each thread creation with an exception that the first thread creation increased the memory by around 1 MB. </span></p>
<p style="font-family:trebuchet ms;" class="MsoNormal"><span style="font-size:85%;"><br />
Later I started checking Virtual memory usage as well and was shocked to see 1 MB of memory usage on each thread creation! A little goggling revealed that by default each thread is assigned a 1 MB stack! Check out the sample code which prints out the Virtual Memory usage of the test application in debug window on each thread creation.</span></p>
<p style="font-family:trebuchet ms;" class="MsoNormal"><span style="font-size:85%;"> </span></p>
<p><span style="font-size:85%;"><span style="color:#009900;">//code</span><br />
private static void Somethread()<br />
{<br />
Debug.WriteLine(string.Format(&#8220;Started thread with thread id {0}&#8221;, Thread.CurrentThread.ManagedThreadId));<br />
//Print task managers VM Size column<br />
Debug.WriteLine(string.Format(&#8220;Current VM usage is {0} MB&#8221;, (Process.GetCurrentProcess().PagedMemorySize64 / (1024 * 1024))));</p>
<p>Thread.Sleep(1000 * 60 * 1); // let the thread live for some time while we create some more threads<br />
Debug.WriteLine(string.Format(&#8220;&#8212;-Ended thread with thread id {0}&#8221;, Thread.CurrentThread.ManagedThreadId));</p>
<p>//Print task managers VM Size column<br />
Debug.WriteLine(string.Format(&#8220;Current VM usage is {0} MB&#8221;, (Process.GetCurrentProcess().PagedMemorySize64 / (1024 * 1024))));</p>
<p>}</p>
<p>private void btnCreateThread_Click(object sender, EventArgs e)<br />
{<br />
//Each created thread will be allocated 1 MB of Virtual memory<br />
Thread samplethread = new Thread(Somethread);<br />
samplethread.IsBackground = true;<br />
samplethread.Start();<br />
}</p>
<p>Conclusion: Want to keep a small footprint of the application, think twice before creating a new thread.</span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vijaymp.wordpress.com/12/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vijaymp.wordpress.com/12/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vijaymp.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vijaymp.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vijaymp.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vijaymp.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vijaymp.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vijaymp.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vijaymp.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vijaymp.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vijaymp.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vijaymp.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vijaymp.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vijaymp.wordpress.com/12/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vijaymp.wordpress.com/12/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vijaymp.wordpress.com/12/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vijaymp.wordpress.com&amp;blog=2196718&amp;post=12&amp;subd=vijaymp&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vijaymp.wordpress.com/2008/01/08/how-much-a-thread-cost-you/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ebf6feed3efc092ee4582667fdac0659?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">vijaymp</media:title>
		</media:content>
	</item>
	</channel>
</rss>
