<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>ASP.NET Debugging : Debugging</title><link>http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx</link><description>Tags: Debugging</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Debugging .NET apps for .NET 4.0</title><link>http://blogs.msdn.com/tom/archive/2009/09/02/debugging-net-apps-for-net-4-0.aspx</link><pubDate>Wed, 02 Sep 2009 19:45:35 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9890471</guid><dc:creator>Tom</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/tom/comments/9890471.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=9890471</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=9890471</wfw:comment><description>&lt;p&gt;With Visual Studio 2010 and .NET 4.0 getting ready to be shipped sometime in the next year, I wanted to see what were peoples ideas for how to debug applications that were written with them.&amp;#160; Are you happy in production with what you are currently doing?&amp;#160; Do you use SOS.dll to troubleshoot problems?&lt;/p&gt;  &lt;h3&gt;SOS&lt;/h3&gt;  &lt;p&gt;The main place I am really interested in is production debugging.&amp;#160; Is SOS a sufficient tool to get the data that you need?&amp;#160; If you had another version of SOS for .NET 4.0, would you be happy with the features it has or would you want something more?&lt;/p&gt;  &lt;h3&gt;Debugger&lt;/h3&gt;  &lt;p&gt;I understand that using a typical debugger can be very difficult for people.&amp;#160; If you don’t know what you are looking at, it can be very difficult to understand what is a problem and what is normal.&amp;#160; It is also very difficult to see typical problems.&amp;#160; For example, if you know your program is crashing due to running out of memory, what do you do to see what is taking up memory?&amp;#160; How do you tell if it is fragmentation?&amp;#160; What types of fragmentation are there that you would need to look for?&lt;/p&gt;  &lt;p&gt;I wanted to see what the interest is in a different way of looking at the data, something more along the lines of &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=28bd5941-c458-46f1-b24d-f60151d875a3&amp;amp;displaylang=en"&gt;DebugDiag&lt;/a&gt;.&lt;/p&gt;  &lt;h3&gt;Opinions&lt;/h3&gt;  &lt;p&gt;I’d love to hear your thoughts.&amp;#160; Let me know!&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="margin:0px; padding:0px 0px 0px 0px;"&gt;&lt;br /&gt;&lt;br /&gt;&lt;script type="text/javascript"&gt;
&lt;/script&gt;
&lt;script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"&gt;&lt;/script&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9890471" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/SOS/default.aspx">SOS</category></item><item><title>Logging modules for ASP.NET (MVC) and also for Windows Azure</title><link>http://blogs.msdn.com/tom/archive/2009/04/24/logging-modules-for-asp-net-mvc-and-also-for-windows-azure.aspx</link><pubDate>Fri, 24 Apr 2009 17:07:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9566755</guid><dc:creator>Tom</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/tom/comments/9566755.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=9566755</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=9566755</wfw:comment><description>&lt;p&gt;I was just reading through Scott Hanselman’s &lt;a href="http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx"&gt;post&lt;/a&gt; about ELMAH and this sounds like a great idea.&amp;#160; Getting a easy to consume report of all of your exceptions is a wonderful thing, especially when you add in that you can get it as an RSS feed, an email or a web site.&amp;#160; You can check out &lt;a title="ELMAH" href="http://code.google.com/p/elmah/"&gt;ELMAH&lt;/a&gt; or read through his post to get a lot of details on it.&lt;/p&gt;  &lt;p&gt;The web site looks like this:&lt;/p&gt;  &lt;p&gt;&lt;img src="http://elmah.googlecode.com/svn/wiki/homeshot.png" /&gt;&lt;/p&gt;  &lt;p&gt;This also got me thinking about Windows Azure and some of the logging that has been done already for that.&amp;#160; So I wanted to highlight one of the projects here and talk about what it does.&amp;#160; It is called &lt;a href="http://azuremonitor.codeplex.com/"&gt;Azure Application Monitor&lt;/a&gt;.&amp;#160; &lt;/p&gt;  &lt;p&gt;This allows you to see how much time and memory your instance is using.&amp;#160; After you integrate this into your application, you will get a report like:&lt;/p&gt;  &lt;p&gt;&lt;img alt="AzureMonitor.jpg" src="http://i3.codeplex.com/Project/Download/FileDownload.aspx?ProjectName=AzureMonitor&amp;amp;DownloadId=63081" /&gt;&lt;/p&gt;  &lt;p&gt;It is easy to add this to your application.&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;&lt;a href="http://azuremonitor.codeplex.com/Release/ProjectReleases.aspx"&gt;Download&lt;/a&gt; the application &lt;/li&gt;    &lt;li&gt;Add the following line to your code.&amp;#160; If you are adding it to a WebRole, add it in the Page_PreRender() function.&amp;#160; Or in the Start() of a WorkerRole, changing it to WorkerRole below:      &lt;pre class="csharpcode"&gt;Neudesic.Azure.AzureMonitor.Start(&lt;span class="str"&gt;&amp;quot;AppName&amp;quot;&lt;/span&gt;, &lt;span class="str"&gt;&amp;quot;WebRole&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
    &lt;style type="text/css"&gt;

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }&lt;/style&gt;&lt;/li&gt;

  &lt;li&gt;Be sure you have the TableStorageEndpoint configuration setting configured.&lt;/li&gt;

  &lt;li&gt;Create a PerfCounterUpdateInterval configuration setting and set the value (the value is in ticks (1000 ticks = 1 second)&lt;/li&gt;

  &lt;li&gt;Test and deploy your application&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To view the data, you need to configure the AzureMonitor so that it points to your TableStorageEndpoint&lt;/p&gt;

&lt;p&gt;As for what all it tracks, here is the list:&lt;/p&gt;

&lt;li&gt;Application Name (you supply this in your code)&lt;/li&gt;

&lt;li&gt;Role Name (you supply this in your code)&lt;/li&gt;

&lt;li&gt;Machine Name&lt;/li&gt;

&lt;li&gt;Process ID&lt;/li&gt;

&lt;li&gt;Thread count&lt;/li&gt;

&lt;li&gt;Handle count&lt;/li&gt;

&lt;li&gt;Total processor time&lt;/li&gt;

&lt;li&gt;User processor time&lt;/li&gt;

&lt;li&gt;Private memory size&lt;/li&gt;

&lt;li&gt;Non-paged memory size&lt;/li&gt;

&lt;li&gt;Paged memory size&lt;/li&gt;

&lt;li&gt;Paged system memory size&lt;/li&gt;

&lt;li&gt;Peak paged memory size&lt;/li&gt;

&lt;li&gt;Peak virtual memory size&lt;/li&gt;

&lt;li&gt;Peak working set&lt;/li&gt;

&lt;li&gt;Page file usage&lt;/li&gt;

&lt;li&gt;Peak page file usage&lt;/li&gt;

&lt;li&gt;Non-paged Pool Usage&lt;/li&gt;

&lt;li&gt;Local machine time&lt;/li&gt;

&lt;li&gt;Process local start time&lt;/li&gt;

&lt;p&gt;It is rather easy to add more stuff to this as the code is available from the projects location.&lt;/p&gt;

&lt;p&gt;One a side-note, if you want to see what data you have in your Blobs, Queues and Tables, there is an updated version of &lt;a href="http://azurestorageexplorer.codeplex.com/"&gt;Azure Storage Explorer&lt;/a&gt; available now.&amp;#160; From that site:&lt;/p&gt;

&lt;p&gt;The most current release of Azure Storage Explorer, version 2.0, has several improvements over the original version:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The UI is WPF-based and has Outlook-style navigation.&lt;/li&gt;

  &lt;li&gt;Multiple storage accounts are supported, and you can change your storage account details directly in the tool.&lt;/li&gt;

  &lt;li&gt;In addition to text and byte views of items, blob items that are pictures can be viewed as images.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Azure Storage Explorer 2.0 does not presently allow you to act on storage items. This capability is being considered for a future update.&lt;/p&gt;

&lt;p&gt;Let me know what you think or if you use any other tools for ASP.NET or Windows Azure.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9566755" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Exceptions/default.aspx">Exceptions</category><category domain="http://blogs.msdn.com/tom/archive/tags/MVC/default.aspx">MVC</category><category domain="http://blogs.msdn.com/tom/archive/tags/Azure/default.aspx">Azure</category></item><item><title>Any ASP.NET Debugging requests?</title><link>http://blogs.msdn.com/tom/archive/2009/04/22/any-asp-net-debugging-requests.aspx</link><pubDate>Wed, 22 Apr 2009 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9561114</guid><dc:creator>Tom</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/tom/comments/9561114.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=9561114</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=9561114</wfw:comment><description>&lt;p&gt;With us starting to look forward to .NET 4.0, I started to think about the debugging story for ASP.NET and wondered if you had any requests for what you would like to see.&lt;/p&gt;  &lt;p&gt;Some of the things that are on my mind are ideas like:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Giving some kind of visual debugging experience to help people to troubleshoot ASP.NET problems more quickly.&lt;/li&gt;    &lt;li&gt;Coming up with a better story for x64 dumps&lt;/li&gt;    &lt;li&gt;Having some more automated commands to give information to users.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I am not saying that any of these will be coming, but they are all things that I am interested in seeing and will be investigating.&lt;/p&gt;  &lt;p&gt;So what would you like to see for the future of the debugging experience?&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9561114" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/.NET/default.aspx">.NET</category></item><item><title>ASP.NET Troubleshooting</title><link>http://blogs.msdn.com/tom/archive/2009/03/24/asp-net-troubleshooting.aspx</link><pubDate>Tue, 24 Mar 2009 19:31:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9504630</guid><dc:creator>Tom</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/tom/comments/9504630.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=9504630</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=9504630</wfw:comment><description>&lt;p&gt;So with how long ASP.NET has been out for now, I am really curious to know how people go about tracking down issues in their project.&lt;/p&gt;  &lt;p&gt;I’d like to know things like:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;How do you know there is a problem&lt;/li&gt;    &lt;li&gt;What tools do you use to confirm that the problem really is a problem&lt;/li&gt;    &lt;li&gt;How do you gather data about the problem&lt;/li&gt;    &lt;li&gt;How do you determine if the problem is your code or something else (like the network, hard drive failure, etc)&lt;/li&gt;    &lt;li&gt;If the problem is your code, how do you ensure your fix resolves the problem (how do you test the fix)&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;If there are any others, I’d also like to hear them.&lt;/p&gt;  &lt;p&gt;Also, if you do anything in the Cloud now, I’d love to hear how you troubleshoot that.&amp;#160; And it can be from any cloud service provider.&lt;/p&gt;  &lt;p&gt;And have any of these things changed with some of the new ways to program that have come out.&amp;#160; For example, AJAX, MVC, Silverlight, IE8, jQuery, etc.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9504630" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/AJAX/default.aspx">AJAX</category><category domain="http://blogs.msdn.com/tom/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/tom/archive/tags/IE8/default.aspx">IE8</category><category domain="http://blogs.msdn.com/tom/archive/tags/MVC/default.aspx">MVC</category><category domain="http://blogs.msdn.com/tom/archive/tags/ADO.NET/default.aspx">ADO.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Linq/default.aspx">Linq</category><category domain="http://blogs.msdn.com/tom/archive/tags/jQuery/default.aspx">jQuery</category></item><item><title>What do you need to troubleshoot Azure?</title><link>http://blogs.msdn.com/tom/archive/2008/11/19/what-do-you-need-to-troubleshoot-azure.aspx</link><pubDate>Wed, 19 Nov 2008 14:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9123534</guid><dc:creator>Tom</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/tom/comments/9123534.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=9123534</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=9123534</wfw:comment><description>&lt;p&gt;Looking to the future with cloud computing, it is going to become increasingly important to have good information about what is happening with your site in order to properly maintain it.&lt;/p&gt;  &lt;p&gt;Keeping development type of issues aside, what types of things do you think you will need in order to be able to properly troubleshoot a problem in your application once it is deployed to the cloud.&lt;/p&gt;  &lt;p&gt;For example, when you publish your application, it will be similar to uploading today to a hoster in that:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;No access to the server&lt;/li&gt;    &lt;li&gt;Cannot get dumps of any processes&lt;/li&gt;    &lt;li&gt;Cannot access perfmon logs&lt;/li&gt;    &lt;li&gt;Cannot access the Event log&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;So given that type of situation:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;What are the possible problem situations that may arise?&lt;/li&gt;    &lt;li&gt;What types of information do you think you will require to find out what is going wrong with your application?&lt;/li&gt;    &lt;li&gt;Would you try to pull your application onto your development box to try to repro some of the problems that way?&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Or are there any other concerns or thoughts you have around this?&amp;#160; I’d love to hear what you have to say.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9123534" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/IIS/default.aspx">IIS</category><category domain="http://blogs.msdn.com/tom/archive/tags/Azure/default.aspx">Azure</category></item><item><title>Strange callstacks</title><link>http://blogs.msdn.com/tom/archive/2008/10/29/strange-callstacks.aspx</link><pubDate>Wed, 29 Oct 2008 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9020880</guid><dc:creator>Tom</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/tom/comments/9020880.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=9020880</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=9020880</wfw:comment><description>&lt;p&gt;How many times have you been troubleshooting a dump or application, you look at the callstack and you see something that just doesn’t quite look right.&amp;#160; Chances are the problem is that you don’t have correct symbols.&lt;/p&gt;  &lt;p&gt;For example, if you see something like this:&lt;/p&gt;  &lt;pre class="code"&gt;Vswebdesign!DllCanUnloadNow+0xb02a0
Vswebdesign!DllCanUnloadNow+0xb8ef4
Vswebdesign!DllCanUnloadNow+0xb2203
Vswebdesign!DllCanUnloadNow+0xb248f
Vswebdesign!DllCanUnloadNow+0x2c406
Vswebdesign!DllCanUnloadNow+105a89
...&lt;/pre&gt;

&lt;p&gt;That usually is pointing to this problem.&amp;#160; You can see that the offsets for the functions are very large.&amp;#160; Also, the callstack is the same function repeated over and over.&amp;#160; And most applications probably don’t call DllCanUnloadNow.&lt;/p&gt;

&lt;p&gt;If you get this problem, you can try to get the correct symbols, or just understand what you are seeing here and know that you aren’t seeing the correct functions.&amp;#160; For most things that concern you, you should be able to get the correct symbols so that you can properly resolve your callstacks.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9020880" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/IIS/default.aspx">IIS</category><category domain="http://blogs.msdn.com/tom/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Videos are coming – suggested topics?</title><link>http://blogs.msdn.com/tom/archive/2008/10/13/videos-are-coming-suggested-topics.aspx</link><pubDate>Mon, 13 Oct 2008 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8994921</guid><dc:creator>Tom</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/tom/comments/8994921.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=8994921</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=8994921</wfw:comment><description>&lt;p&gt;So I have started to create some videos showing various things that you can do.&amp;#160; Are there specific things that everyone would like to see?&amp;#160; I’d like to build a list of the things you want and then I can start knocking them out.&amp;#160; I’m figuring most of them are going to be around debugging and probably around ASP.NET but feel free to suggest other things as well.&lt;/p&gt;  &lt;p&gt;For example:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;How to get sos to work when debugging on another machine.&lt;/li&gt;    &lt;li&gt;What is a good way to know if a memory problem is managed or native?&lt;/li&gt;    &lt;li&gt;How do I print out the column names of a DataTable?&lt;/li&gt;    &lt;li&gt;How can I find out what is stored in the ASP.NET Cache?&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;These are simply examples.&amp;#160; Please let me know what you would like and be as specific as possible.&lt;/p&gt;  &lt;p&gt;Also, keep in mind that we have a bunch of development videos already up on &lt;a href="http://www.asp.net/learn"&gt;http://www.asp.net/learn&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8994921" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Videos/default.aspx">Videos</category><category domain="http://blogs.msdn.com/tom/archive/tags/SOS/default.aspx">SOS</category></item><item><title>Tail calling in .NET</title><link>http://blogs.msdn.com/tom/archive/2008/10/02/tail-calling-in-net.aspx</link><pubDate>Thu, 02 Oct 2008 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8970666</guid><dc:creator>Tom</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/tom/comments/8970666.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=8970666</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=8970666</wfw:comment><description>&lt;p&gt;Thought I would give a little details on one type of optimization that it is possible to see and explain what it is and how it affects things so that if you come across it, you will understand what is happening.&lt;/p&gt;  &lt;p&gt;So there is this concept of tail calling which is where the compiler will optimize code to save execution of instructions as well as saving some reads and writes of stack memory.&amp;#160; This happens under certain circumstances when a function ends by calling another function (hence tail calling).&amp;#160; For example, if we have the following:&lt;/p&gt;  &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; background-color: #f4f4f4"&gt;   &lt;div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;     &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Main()&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;    Test();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;}&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Test()&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;    First();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;    Second();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;    Third();&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;}&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&amp;#160;&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Third()&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;{&lt;/pre&gt;

    &lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &amp;#39;Courier New&amp;#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"&gt;}&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;In this sample, it is possible for the compiler to make the call to Third() a tail call.&amp;#160; So to better explain this, under normal situations we would see something like this on a callstack while we are executing in the function Second():&lt;/p&gt;

&lt;pre class="code"&gt;Second()
Test()
Main()&lt;/pre&gt;

&lt;p&gt;When Third() runs, it is possible for it to be a little different.&amp;#160; Instead of creating a new allocation on the stack for Third, it can replace Test() with Third().&amp;#160; So then we would see:&lt;/p&gt;

&lt;pre class="code"&gt;Third()
Main()&lt;/pre&gt;

&lt;p&gt;When Third() returns, it will return directly to Main() and not have to go through Test() at all.&amp;#160; This saves time and memory for the application overall.&lt;/p&gt;

&lt;h3&gt;To tail or not to tail&lt;/h3&gt;

&lt;p&gt;So how does the compiler decide if it should use this optimization?&amp;#160; Well, first there is the discussion of which compiler we are referring to.&amp;#160; There are two compilers at play with .NET.&amp;#160; The first takes the source code and compiles it into IL.&amp;#160; Then the JIT compiler will take the IL and create native code.&amp;#160; The creation of IL can set up hints to try to help with the creation of tail calls, but it is ultimately the JIT compilers job to create them.&amp;#160; There are a lot of rules that determine if it can do this but a quick summary of the rules that will make us NOT use this option are:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Note: This is subject to change and you shouldn’t base anything on this behavior.&amp;#160; This is just for general knowledge.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
  &lt;li&gt;Caller doesn’t return immediately after the call&lt;/li&gt;

  &lt;li&gt;Stack arguments between caller and callee are incompatible in a way that would require shifting things around in the caller’s frame before the callee could execute&lt;/li&gt;

  &lt;li&gt;Caller and callee return different types&lt;/li&gt;

  &lt;li&gt;We inline the call instead (inlining is way better than tail calling, and opens the door to many more optimizations)&lt;/li&gt;

  &lt;li&gt;Security gets in the way&lt;/li&gt;

  &lt;li&gt;The debugger/profiler/config turned off JIT optimizations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you really want to see the full list, check this this &lt;a href="http://blogs.msdn.com/davbr/pages/tail-call-jit-conditions.aspx"&gt;post&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Who cares about tail calls, sounds like a trip to the zoo&lt;/h3&gt;

&lt;p&gt;Normally this kind of thing won’t matter to you unless you are trying to write a profiler, but there are times that you may see a callstack that doesn’t make any sense because one of the functions is missing.&amp;#160; As someone that has done a lot of debugging, I have come across this from time to time and thought others may find it useful to know about.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8970666" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Code/default.aspx">Code</category></item><item><title>ASP.NET Tip: How to avoid creating a GC Hole</title><link>http://blogs.msdn.com/tom/archive/2008/09/25/asp-net-tip-how-to-avoid-creating-a-gc-hole.aspx</link><pubDate>Thu, 25 Sep 2008 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8963719</guid><dc:creator>Tom</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/tom/comments/8963719.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=8963719</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=8963719</wfw:comment><description>&lt;p&gt;There are only a few things that can make a .NET process crash.&amp;#160; The most common one is an Unhandled Exception getting raised.&amp;#160; Another way that is can happen is by creating a GC Hole.&lt;/p&gt;  &lt;h3&gt;What is a GC Hole&lt;/h3&gt;  &lt;p&gt;So first a little background on what I mean by a GC Hole.&amp;#160; A GC Hole is any corruption that happens inside of the managed heaps.&amp;#160; Under normal circumstances, this cannot happen as you don’t have pointers that reference objects in the heap so you can’t corrupt them.&amp;#160; This corruption is generally seen by the GC (Garbage Collector) when it is trying to compact the heaps and release objects no longer referenced.&lt;/p&gt;  &lt;h3&gt;How does a GC Hole get created&lt;/h3&gt;  &lt;p&gt;So if we don’t have access to pointers (not counting managed C++ and that is a whole different conversation), how can this occur?&amp;#160; Well the most common way is by making a native call (P/Invoke) that returns data.&amp;#160; There are actually two things that can happen during this process to cause a GC Hole.&lt;/p&gt;  &lt;p&gt;The first is a buffer overrun.&amp;#160; You can imagine passing a byte array to a native function and having that array be 200 bytes.&amp;#160; If the native function returns 400 bytes worth of data, it will write past the end of the object and corrupt the next object in the managed heap.&lt;/p&gt;  &lt;p&gt;The second happens if the object being passed to a native function isn’t pinned first. &lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;Pinning is a whole topic on it’s own.&amp;#160; There are lots of problems that can happen from pinning object in memory, a great deal of information can be found &lt;a href="http://blogs.msdn.com/yunjin/archive/2004/01/27/63642.aspx"&gt;here&lt;/a&gt; on the most common problem you can have while pinning.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Back to the GC Hole issue.&amp;#160; If the following events occur, it will cause a GC Hole:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Allocate a byte array to be passed to a native call, do not pin it &lt;/li&gt;    &lt;li&gt;Make the native call passing the byte array &lt;/li&gt;    &lt;li&gt;While the native call is processing the request, a Garbage Collection occurs &lt;/li&gt;    &lt;li&gt;Native call returns &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;So what is the problem with the GC running before the native call returns?&amp;#160; Well, the GC has the ability to compact the heap (move object around so they are next to each other) and if it moves our byte array to a new location, the native call will still have the old location as to where to write it’s data to.&amp;#160; So when it returns, it will write the result into memory where the byte array used to be, thus corrupting anything in that memory location currently.&lt;/p&gt;  &lt;p&gt;For example, if we had this when making the native call:&lt;/p&gt;  &lt;p&gt;_______________________________________    &lt;br /&gt;|&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |     &lt;br /&gt;|&amp;#160;&amp;#160;&amp;#160;&amp;#160; int&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160; int&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Free&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160; byte&amp;#160; |&amp;#160;&amp;#160; Free&amp;#160;&amp;#160; |     &lt;br /&gt;|&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |     &lt;br /&gt;_______________________________________&lt;/p&gt;  &lt;p&gt;Then after the GC runs, it will compact the ints and byte next to each other like:&lt;/p&gt;  &lt;p&gt;_______________________________________    &lt;br /&gt;|&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |     &lt;br /&gt;|&amp;#160;&amp;#160;&amp;#160;&amp;#160; int&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160; int&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160; byte&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Free&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |     &lt;br /&gt;|&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |     &lt;br /&gt;_______________________________________&lt;/p&gt;  &lt;p&gt;Now assume objects get created inside that free block, we would then have something like:&lt;/p&gt;  &lt;p&gt;_______________________________________    &lt;br /&gt;|&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |     &lt;br /&gt;|&amp;#160;&amp;#160;&amp;#160;&amp;#160; int&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160; int&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160; byte |&amp;#160; String | String | Free |     &lt;br /&gt;|&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; |     &lt;br /&gt;_______________________________________&lt;/p&gt;  &lt;p&gt;Then when the native calls returns, it will write to the original location of the byte array and thus corrupt the two strings.&lt;/p&gt;  &lt;h3&gt;How to find a GC Hole and fix it&lt;/h3&gt;  &lt;p&gt;This is something I will address shortly in another blog post.&amp;#160; We will look into troubleshooting a GC Hole and see what we can do to find the problem.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;I hope that this post will help developers understand the areas where they need to be careful and help to keep everyone from creating them in the first place.&amp;#160; For information on how to pin an object correctly, take a look at &lt;a title="http://blogs.msdn.com/clyon/archive/2004/09/17/230985.aspx" href="http://blogs.msdn.com/clyon/archive/2004/09/17/230985.aspx"&gt;GCHandles, Boxing and Heap Corruption&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;More great information can be found at, &lt;a title="http://blogs.msdn.com/cbrumme/archive/2003/05/06/51385.aspx" href="http://blogs.msdn.com/cbrumme/archive/2003/05/06/51385.aspx"&gt;Asynchronous operations, pinning&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8963719" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/IIS/default.aspx">IIS</category><category domain="http://blogs.msdn.com/tom/archive/tags/Crash/default.aspx">Crash</category><category domain="http://blogs.msdn.com/tom/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Who uses SOSEX and what for?</title><link>http://blogs.msdn.com/tom/archive/2008/09/22/who-uses-sosex-and-what-for.aspx</link><pubDate>Mon, 22 Sep 2008 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8959634</guid><dc:creator>Tom</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/tom/comments/8959634.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=8959634</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=8959634</wfw:comment><description>&lt;p&gt;Another thing I am curious to know is if anyone is using SOSEX and what advantages it gives you for debugging.&amp;#160; I have talked to the author of it and I think it has a lot of useful commands, just wanted to get everyone’s take on it.&lt;/p&gt;  &lt;p&gt;Let me know any problems you have with it also.&lt;/p&gt;  &lt;p&gt;If you don’t know what SOSEX is, take a look at: &lt;a href="http://www.stevestechspot.com/SOSEXANewDebuggingExtensionForManagedCode.aspx"&gt;Steve’s blog&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8959634" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/IIS/default.aspx">IIS</category><category domain="http://blogs.msdn.com/tom/archive/tags/SOS/default.aspx">SOS</category></item><item><title>High Memory part 6 – Fragmentation revisited</title><link>http://blogs.msdn.com/tom/archive/2008/09/17/high-memory-part-6-fragmentation-revisited.aspx</link><pubDate>Wed, 17 Sep 2008 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8954457</guid><dc:creator>Tom</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/tom/comments/8954457.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=8954457</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=8954457</wfw:comment><description>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;I have talked about a bunch of commands used to troubleshoot a managed memory problem in the past and given some situations of using them like: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;a title="High memory, CPU, or other performance problems with .NET" href="http://blogs.msdn.com/tom/archive/2007/11/26/high-memory-cpu-or-other-performance-problems-with-net.aspx"&gt;High memory, CPU, or other performance problems with .NET&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a title="High Memory continued - Datatables" href="http://blogs.msdn.com/tom/archive/2007/12/26/high-memory-continued.aspx"&gt;High Memory continued - Datatables&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a title="High Memory part 3 - Native Heap" href="http://blogs.msdn.com/tom/archive/2008/02/07/high-memory-part-3.aspx"&gt;High Memory part 3 - Native Heap&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a title="High Memory part 4 - Managed Strings" href="http://blogs.msdn.com/tom/archive/2008/02/13/high-memory-part-4.aspx"&gt;High Memory part 4 - Managed Strings&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;&lt;a title="High Memory part 5 - Fragmentation" href="http://blogs.msdn.com/tom/archive/2008/02/18/high-memory-part-5-fragmentation.aspx"&gt;High Memory part 5 – Fragmentation&lt;/a&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;There is another way that we could see a problem that involves fragmentation.&amp;#160; Let’s consider the situation where we see the same situation we saw before, namely:&lt;/p&gt; &lt;a href="http://blogs.msdn.com/blogfiles/tom/WindowsLiveWriter/HighMemorypart5Fragmentation_DE84/address-summary_2.png" mce_href="http://blogs.msdn.com/blogfiles/tom/WindowsLiveWriter/HighMemorypart5Fragmentation_DE84/address-summary_2.png"&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="479" alt="address-summary" src="http://blogs.msdn.com/blogfiles/tom/WindowsLiveWriter/HighMemorypart5Fragmentation_DE84/address-summary_thumb.png" width="634" border="0" mce_src="http://blogs.msdn.com/blogfiles/tom/WindowsLiveWriter/HighMemorypart5Fragmentation_DE84/address-summary_thumb.png" /&gt;&lt;/a&gt;   &lt;p&gt;&lt;/p&gt;  &lt;p&gt;But we do see a bunch of files listed when we look in &lt;strong&gt;lm&lt;/strong&gt;.&amp;#160; So what should this tell us about the problem?&amp;#160; Well, first off, since the files are showing up in lm, we know the problem isn’t the same as, &lt;a title="Dynamic Assemblies and what to do about them" href="http://blogs.msdn.com/tom/archive/2007/12/05/dynamic-assemblies-and-what-to-do-about-them.aspx"&gt;Dynamic Assemblies and what to do about them&lt;/a&gt;.&amp;#160; So next we would want to look and see specifically what the files are.&lt;/p&gt;  &lt;h3&gt;Situation 1&lt;/h3&gt;  &lt;p&gt;The first common problem that you will see is caused by either have &lt;a href="http://blogs.msdn.com/tom/archive/2007/11/26/high-memory-cpu-or-other-performance-problems-with-net.aspx"&gt;debug=true&lt;/a&gt;, or batch=false is set.&amp;#160; Batch is similar to debug, set in the same place and will tell the compiler to not create one DLL for the entire directory but rather create one DLL for each file we compile.&amp;#160; Underneath, debug=true sets batch=false, so they do the same thing in this regard.&amp;#160; If that is the problem, you will see a bunch of strangely named DLLs in the lm list.&amp;#160; Names like:&lt;/p&gt;  &lt;pre class="code"&gt;  hbdp25u7.dll
  1gxdnryw.dll
  fzgq0g7y.dll
  fhlpqzqm.dll
  su0utu5-.dll
  vhaewyii.dll
  tdkuail7.dll&lt;/pre&gt;

&lt;p&gt;Turning debug off, or batch on will resolve the problem in this situation.&lt;/p&gt;

&lt;h3&gt;Situation 2&lt;/h3&gt;

&lt;p&gt;The second common problem that will cause this is if you have a common framework that you are using in multiple applications.&amp;#160; One common way to do this is the copy the framework files into the bin directory of each application.&amp;#160; This will cause us to create a different in-memory file for each one.&amp;#160; So instead of just having 5 files that make up your framework loaded in memory, you will have 5 * (# of AppDomains) loaded. &lt;/p&gt;

&lt;p&gt;You can identify this problem because in the list of files, you will see the same filename repeated, only some will have some numbers after them:&lt;/p&gt;

&lt;pre class="code"&gt;  MyFramework.dll
  MyFramework_50008012.dll
  MyFramework_50002008.dll
  MyFramework_40101008.dll&lt;/pre&gt;

&lt;p&gt;This is just showing the different versions of the file by showing you the other copies with their base address (load address).&lt;/p&gt;

&lt;p&gt;The easiest solution to this type of problem is to place the framework files into the GAC.&amp;#160; Then you will only have one copy of the files which all the Applications will share.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8954457" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/IIS/default.aspx">IIS</category><category domain="http://blogs.msdn.com/tom/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/High+Memory/default.aspx">High Memory</category></item><item><title>Debugger Extension update</title><link>http://blogs.msdn.com/tom/archive/2008/09/12/debugger-extension-update.aspx</link><pubDate>Fri, 12 Sep 2008 13:00:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8945763</guid><dc:creator>Tom</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/tom/comments/8945763.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=8945763</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=8945763</wfw:comment><description>&lt;p&gt;The debugger extension that we were working on getting out with the debugger package has hit some roadblocks and it looks like it isn't going to be shipping with the debugger anytime soon.&lt;/p&gt;  &lt;p&gt;We are looking to see if we can find an alternative method to get it out.&amp;#160; But for the meantime, just stay tuned and I'll let you know when I find something out.&lt;/p&gt;  &lt;p&gt;I know the additional functions will be a lot of help to everyone that uses it so hopefully we can get something to you soon.&amp;#160; I'll keep this blog updated with the latest.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8945763" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/IIS/default.aspx">IIS</category><category domain="http://blogs.msdn.com/tom/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/SOS/default.aspx">SOS</category></item><item><title>How do you test the performance of your web site?</title><link>http://blogs.msdn.com/tom/archive/2008/09/10/how-do-you-test-the-performance-of-your-web-site.aspx</link><pubDate>Wed, 10 Sep 2008 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8937133</guid><dc:creator>Tom</dc:creator><slash:comments>22</slash:comments><comments>http://blogs.msdn.com/tom/comments/8937133.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=8937133</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=8937133</wfw:comment><description>&lt;p&gt;I wanted to get a feel for what tools people are using to test the performance of their website.&amp;#160; My questions are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Do you check page-load times?&amp;#160; &lt;/li&gt;    &lt;li&gt;Do you dig in to see which files are taking the time&lt;/li&gt;    &lt;li&gt;How do you track down a page that uses a lot of memory&lt;/li&gt;    &lt;li&gt;How do you track down a high CPU consuming page&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I don’t want to list any possible tools as I’d like to hear what people are using without me putting any suggestions out.&lt;/p&gt;  &lt;p&gt;If I don’t see some being mentioned, I’ll be sure to bring it up so we have a holistic view of the tools.&lt;/p&gt;  &lt;p&gt;Look forward to seeing what people use.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8937133" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/IIS/default.aspx">IIS</category><category domain="http://blogs.msdn.com/tom/archive/tags/High+Memory/default.aspx">High Memory</category><category domain="http://blogs.msdn.com/tom/archive/tags/High+CPU/default.aspx">High CPU</category></item><item><title>SOS Tip: Using help</title><link>http://blogs.msdn.com/tom/archive/2008/09/09/sos-tip-using-help.aspx</link><pubDate>Tue, 09 Sep 2008 13:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8936325</guid><dc:creator>Tom</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/tom/comments/8936325.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=8936325</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=8936325</wfw:comment><description>&lt;p&gt;This is just a quick little note to remind everyone that the !help command in sos can be very helpful.&amp;#160; Not only does it list all of the possible commands, but if you run it and pass it the name of a command, it will print out a bunch of really useful information for that command.&lt;/p&gt;  &lt;p&gt;For example, if you look at the help for !dumpheap: !help dumpheap&lt;/p&gt;  &lt;p&gt;You will see, for example, the syntax that you can use to pass the list of objects returned from !dumpheap -mt to a .foreach command.&lt;/p&gt;  &lt;p&gt;I use the help a lot to check on the different switches and also for reminders like with .foreach, when to use a ( and when to use a {.&amp;#160; Those always seem to trip me up.&lt;/p&gt;  &lt;p&gt;Another really useful command inside help is the FAQ.&amp;#160; If you run !help faq, it will print out all sorts of things that are really useful to know.&lt;/p&gt;  &lt;p&gt;If you haven't checked out the !help command much in sos, please do now and let me know what you think.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8936325" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/IIS/default.aspx">IIS</category><category domain="http://blogs.msdn.com/tom/archive/tags/SOS/default.aspx">SOS</category></item><item><title>SOS Best Practice: Match the SOS version with the process being analyzed</title><link>http://blogs.msdn.com/tom/archive/2008/08/25/sos-best-practice-match-the-sos-version-with-the-process-being-analyzed.aspx</link><pubDate>Mon, 25 Aug 2008 13:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8885965</guid><dc:creator>Tom</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/tom/comments/8885965.aspx</comments><wfw:commentRss>http://blogs.msdn.com/tom/commentrss.aspx?PostID=8885965</wfw:commentRss><wfw:comment>http://blogs.msdn.com/tom/rsscomments.aspx?PostID=8885965</wfw:comment><description>&lt;p&gt;I haven’t seen a whole of of issues around this as of yet, but now that 3.5 SP1 has released that may change.&amp;#160; If you copy SOS.dll to another location and then load it out of that directory to troubleshoot dumps, you may start seeing problems.&amp;#160; Namely, things like !clrstack not returning any valid names for functions and just printing out the numbers.&amp;#160; For example:&lt;/p&gt;  &lt;pre class="code"&gt;0:006&amp;gt; !clrstack
OS Thread Id: 0x1154 (6)
ESP       EIP     
0154cfdc 7614f35f [HelperMethodFrame: 0154cfdc] 
0154d080 5fb7b8cd 
0154d094 5fb7b78b 
0154d0bc 5fd26907 
0154d0d0 5f95a87c 
0154d0e8 5f959458 
0154d108 5f9634f1 
0154d124 5f96342d 
0154d150 65525e55 
0154d17c 5e339ea3 
0154d1f8 5e33ce4d 
0154d37c 5e3400cc 
0154d380 5dd60d14 
0154d3b4 5e56def3 
0154d408 5dce876b 
0154d438 5dd12799 
0154d47c 5dd0df51 
0154d4b0 5dcea92a 
0154d714 6e871b4c [GCFrame: 0154d714] 
0154d730 6e871b4c [GCFrame: 0154d730] 
0154d8a4 6e871b4c [ContextTransitionFrame: 0154d8a4] 
0154d9b8 6e871b4c [GCFrame: 0154d9b8] 
0154d99c 6e871b4c [GCFrame: 0154d99c] 
0154ec1c 6e871b4c [CustomGCFrame: 0154ec1c] 
0154ec30 6e871b4c [CustomGCFrame: 0154ec30] 
0154ec44 6e871b4c [CustomGCFrame: 0154ec44] 
0154ec58 6e871b4c [CustomGCFrame: 0154ec58] 
0154ec6c 6e871b4c [CustomGCFrame: 0154ec6c] 
0154ec80 6e871b4c [CustomGCFrame: 0154ec80] 
0154ec94 6e871b4c [CustomGCFrame: 0154ec94] 
0154f060 6e871b4c [TPMethodFrame: 0154f060] 
0154f07c 5dcea109 
0154f0f0 5dce9d1f 
0154f128 5dce9bed 
0154f170 5dce9b3a 
0154f194 5e3bf136 
0154f3f8 6e968c4e [GCFrame: 0154f3f8] 
0154f554 6e968c4e [ComMethodFrame: 0154f554] &lt;/pre&gt;

&lt;p&gt;This is a sign that there may be something incorrect with SOS.&amp;#160; We can clearly see this is a managed stack, but we aren’t getting any values.&lt;/p&gt;

&lt;p&gt;The reason for this is that we don’t guarantee SOS to work with any version of the framework other then the version that was running.&amp;#160; In other words, always match up the SOS version with the framework version.&amp;#160; If you are troubleshooting on the same machine you captured the dump from, or a machine running the same version of the framework, you can easily do this by loading SOS using:&lt;/p&gt;

&lt;pre class="code"&gt;.loadby sos mscorwks&lt;/pre&gt;

&lt;blockquote&gt;
  &lt;p&gt;Please note that the SOS that ships with the debugger package is different from this.&amp;#160; This version will work correctly with any 1.x version of the framework.&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8885965" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/tom/archive/tags/ASP.NET/default.aspx">ASP.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/Debugging/default.aspx">Debugging</category><category domain="http://blogs.msdn.com/tom/archive/tags/IIS/default.aspx">IIS</category><category domain="http://blogs.msdn.com/tom/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/tom/archive/tags/SOS/default.aspx">SOS</category></item></channel></rss>