Late bound thoughtshttp://geekswithblogs.net/tiagomargalho/Default.aspx
about problems (and possible solutions)en-UStiagomargalhotiagomargalho@gmail.comSubtext Version 0.0.0.0Late bound thoughtshttp://geekswithblogs.net/images/RSS2Image.gifhttp://geekswithblogs.net/tiagomargalho/Default.aspx
7760Unmanaged or managed memory leaks?Memory Leak Analysishttp://geekswithblogs.net/tiagomargalho/archive/2012/01/23/unmanaged-or-managed-memory-leaks.aspx
<p>Originally posted on: <a href='http://geekswithblogs.net/tiagomargalho/archive/2012/01/23/unmanaged-or-managed-memory-leaks.aspx'>http://geekswithblogs.net/tiagomargalho/archive/2012/01/23/unmanaged-or-managed-memory-leaks.aspx</a></p>Its been a while since I posted something in this blog.<div><br /></div><div>I've been dealing with a memory leak issue for the past week so I thought about sharing the experience I gained with you.</div><div><br /></div><div>First of all, when it comes to memory management in .NET some people think that it shouldn't be trusted so GC.Collect is triggered everywhere they think it will help lowering the memory working set of their application. It's just plain wrong. I had very rare occasions where I needed to explicitly collect objects from the GC.</div><div><br /></div><div>A good way of diagnosing memory problems is to use the tools available to you. DebugDiag, VMMap and WinDbg should suffice. DebugDiag and VMMap are quite easy to use while WinDbg needs some training before hand.</div><div><br /></div><div>Things might not be what they seem to be :) In my case I was getting working sets that were about 1,4-1,7GB with .NET Heaps consuming only around 250-300MB. This can lead you into thinking it is an unmanaged leak because you have a lower .net heap memory usage than the process (unmanaged) heap. While it could be the sign of an unmanaged memory leak it can be in fact managed objects being pinned in the GC and keeping references to unmanaged data allocated by them. Example: Bitmaps.</div><div><br /></div><div>If you take a look at how Bitmaps are structured (you can run !do &lt;bitmapaddress&gt;) you will see a property called nativeImage. This property is nothing more nothing less than a IntPtr that points to an address in the "unmanaged" heap. This is where it gets interesting. The unmanaged heap is keeping all the bitmap data allocated with Gdi+ (used under the hood by System.Drawing).</div><div><br /></div><div>You might think that by looking at the output of !eeheap -gc you can see how much memory your .NET application is using but this will in fact tell you the total size in the .NET Heaps that your .NET objects consume. Not all your objects' values.</div><div><br /></div><div>So in the Bitmap case, you will get very low instance size while they can be in fact keeping a great amount of memory in the unmanaged heap.</div><div><br /></div><div>In case you are experiencing OutOfMemory exceptions or see that your app process is consuming a lot of virtual memory (VAC or &lt;unclassified&gt; - see !address -summary) and the #bytes in all heaps counter is a lot lower than that, this might help you:</div><div><ul><li>Run DebugDiag and run a Memory Analysis test. </li><ul><li>Exclude the warnings about clr.dll. In my case I was getting warning about WindowsCodecs.dll doing a lot of zcalloc (WindowsCodecs!zcalloc). I actually tried to get the stack of the Heap Allocs that were leading to this.</li></ul><li>What objects are being pinned? </li><ul><li>Use WinDbg to find out: !gchandles. If you see a big number of Bitmaps or System.Drawing.Internal.GPStream you are in big trouble :)</li></ul><li>Why are those objects pinned? </li><ul><li>Use WinDbg to find out: !gcroot &lt;objectaddress&gt; and try to understand the dependency between objects in the output. In my case I had a tooltip keeping a reference to all my buttons (in a data navigator used widely in the application).</li></ul><li>Make sure you Dispose the objects that are keeping your objects pinned.</li></ul><div><br /></div></div><div><br /></div><div><br /></div> <img src="http://geekswithblogs.net/tiagomargalho/aggbug/148453.aspx" width="1" height="1" />tiagomargalhohttp://geekswithblogs.net/tiagomargalho/archive/2012/01/23/unmanaged-or-managed-memory-leaks.aspxMon, 23 Jan 2012 22:11:50 GMThttp://geekswithblogs.net/tiagomargalho/comments/148453.aspxhttp://geekswithblogs.net/tiagomargalho/archive/2012/01/23/unmanaged-or-managed-memory-leaks.aspx#feedbackhttp://geekswithblogs.net/tiagomargalho/comments/commentRss/148453.aspxFighting Architecture and Design ErosionArchitectureToolshttp://geekswithblogs.net/tiagomargalho/archive/2011/06/08/fighting-architecture-and-design-erosion.aspx
<p>Originally posted on: <a href='http://geekswithblogs.net/tiagomargalho/archive/2011/06/08/fighting-architecture-and-design-erosion.aspx'>http://geekswithblogs.net/tiagomargalho/archive/2011/06/08/fighting-architecture-and-design-erosion.aspx</a></p><p>It is no big news that whatever the architecture you plan and implement, soon or later you'll start to see some code <a href="http://en.wikipedia.org/wiki/Code_smell">"bad smells"</a>. In the early phase of implementation you might do some team training and explain how your developer team should write code using the planned architecture and design (patterns). Despite this effort, you cannot expect people to just follow your implementation recommendations every time.</p>
<p>At first, you might consider using gated-checkins or CI builds with <a href="http://msdn.microsoft.com/en-us/library/dd264939.aspx" target="_blank">VS code analysis</a>. The problem with this approach is that you <u>cannot accurately see</u> what is changing or braking in your architecture so this is not enough.</p>
<p>The solution is: use an architecture and design visualization tool!</p>
<p>I am a total C# and .NET lover so the tool I would recommend and will talk about is <a href="http://www.ndepend.com/">NDepend</a>.</p>
<p>This tool has direct integration with Visual Studio (2005, 2008 and 2010) and can be a great replacement for Visual Studio's integrated Code Analysis.</p>
<p><img height="180" width="400" src="http://www.ndepend.com/NS/images/Treemap.jpg" style="border: 3px black solid;" alt="Treemap" /></p>
<p>It not only allows you to graphically see your dependencies in code using a Dependency Matrix or Dependency Graph but also allows you to query your code using CQL (Code Query Language):</p>
<p><a target="_blank" href="http://www.ndepend.com/NS/images/CodeQuery.jpg"><img height="180" width="400" style="border: 3px black solid;" src="http://www.ndepend.com/NS/images/CodeQuery.jpg" alt="CQL - Code Query Language" /></a></p>
<p>Let's say you are implementing the <a target="_blank" href="http://martinfowler.com/eaaDev/PassiveScreen.html">MVP pattern (passive view)</a> and don't want anyone referencing concrete view types in the presenters. You would just write the following CQL:</p>
<p><em>WARN IF Count &gt; 0 IN SELECT TYPES FROM NAMESPACES "AgnosticApp.Presenters.*"<br />
WHERE IsDirectlyUsing "AgnosticApp.Views.*"</em></p>
<p>The CQL language gives you an easy way of creating code validation rules and the Visual NDepend editor has Intellisense-like dropdown hints that appears while you're writing code.</p>
<p>The report you get gives you very detailed information regarding <a href="http://www.ndepend.com/Features.aspx#DependenciesView" target="_blank">Dependencies</a>, <a href="http://www.ndepend.com/Features.aspx#CQL" target="_blank">CQL Rules</a> violations and <a href="http://www.ndepend.com/Features.aspx#Metrics" target="_blank">Code Metrics</a> (over 82 metrics). You can even define a baseline and get Diffs between analysis runs!</p>
<p>Ohh almost forgot! You can even <a href="http://www.finalbuilder.com/articles.aspx?ID=47">integrate it with FinalBuilder!</a></p>
<p><strong>Links:</strong></p>
<ul>
<li><a href="http://www.ndepend.com/GettingStarted.aspx" target="_blank">NDepend Getting Started</a> - a must to understand the basics of this tool!</li>
<li><a href="http://www.ndepend.com/Features.aspx" target="_blank">NDepend Features</a></li>
</ul>
<p> </p> <img src="http://geekswithblogs.net/tiagomargalho/aggbug/145780.aspx" width="1" height="1" />tiagomargalhohttp://geekswithblogs.net/tiagomargalho/archive/2011/06/08/fighting-architecture-and-design-erosion.aspxWed, 08 Jun 2011 19:44:47 GMThttp://geekswithblogs.net/tiagomargalho/comments/145780.aspxhttp://geekswithblogs.net/tiagomargalho/archive/2011/06/08/fighting-architecture-and-design-erosion.aspx#feedbackhttp://geekswithblogs.net/tiagomargalho/comments/commentRss/145780.aspxMicrosoft .NET Framework 4 Platform Update 1 - Runtime UpdateWF4http://geekswithblogs.net/tiagomargalho/archive/2011/04/19/microsoft-.net-framework-4-platform-update-1---runtime-update.aspx
<p>Originally posted on: <a href='http://geekswithblogs.net/tiagomargalho/archive/2011/04/19/microsoft-.net-framework-4-platform-update-1---runtime-update.aspx'>http://geekswithblogs.net/tiagomargalho/archive/2011/04/19/microsoft-.net-framework-4-platform-update-1---runtime-update.aspx</a></p><p>It seems that Microsoft has made available a runtime update for the .Net Framework 4.0 that allows a developer to create State Machine Workflows (event-driven workflows). This is great news for everyone that had this feature available back in WF3. They are also providing support for usage of SQL Workflow Instance Store together with SQL Azure.</p>
<p><strong>LInks</strong>:</p>
<ul>
<li><a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=e85a2f1b-031c-419c-95b2-0610e90bafd7&amp;displaylang=en">Download link</a></li>
<li>List of new features can be found <a href="http://support.microsoft.com/kb/2478063/en-us">here</a>.</li>
<li><a href="http://blogs.msdn.com/b/rjacobs/">Ron Jacobs</a> has a great article about this update <a href="http://blogs.msdn.com/b/endpoint/archive/2011/04/11/the-net-4-platform-update-and-wf4-state-machine.aspx">here</a>.</li>
<li>WF4 State Machine User Experience article <a href="http://blogs.msdn.com/b/endpoint/archive/2011/04/20/wf4-state-machine-user-experience.aspx">here</a>.</li>
</ul>
<p> </p> <img src="http://geekswithblogs.net/tiagomargalho/aggbug/144952.aspx" width="1" height="1" />tiagomargalhohttp://geekswithblogs.net/tiagomargalho/archive/2011/04/19/microsoft-.net-framework-4-platform-update-1---runtime-update.aspxTue, 19 Apr 2011 15:36:22 GMThttp://geekswithblogs.net/tiagomargalho/comments/144952.aspxhttp://geekswithblogs.net/tiagomargalho/archive/2011/04/19/microsoft-.net-framework-4-platform-update-1---runtime-update.aspx#feedbackhttp://geekswithblogs.net/tiagomargalho/comments/commentRss/144952.aspxVisual Studio 2010 - ResGen.exe using 25% CPU, very slow BuildVisual Studio 2010http://geekswithblogs.net/tiagomargalho/archive/2011/04/13/visual-studio-2010---resgen.exe-using-25-cpu-compilation-taking.aspx
<p>Originally posted on: <a href='http://geekswithblogs.net/tiagomargalho/archive/2011/04/13/visual-studio-2010---resgen.exe-using-25-cpu-compilation-taking.aspx'>http://geekswithblogs.net/tiagomargalho/archive/2011/04/13/visual-studio-2010---resgen.exe-using-25-cpu-compilation-taking.aspx</a></p><p>Recently I've found that one of our Hotfix Branches was taking a long time to compile (15min or more). This was strange because our main development branch was OK and compiling the same Visual Studio solution in under 3 minutes. The only difference was that in the hotfix branch we still had the project in .NET 2.0.</p>
<p>I saw right away that ResGen.exe (Resource Generator) was executing for a long time and consuming 25% CPU (I guess it would use more CPU if it was multi-threaded :))</p>
<p>The problem you face when you try to solve problems regarding compilation time is that it is very hard to see what is actually taking more time to compile because MSBuild.exe does not provide a complete and easy way to get time metrics.</p>
<p>Although you can call MSBuild in verbose mode it does not tell you within each project what took longer to compile.</p>
<p>The technique I used to figure out what was going on was to use Process Monitor from Microsoft (made by Mark Russinovich, which I had the opportunity to meet at TechEd Europe last year, and Bryce Cogswell).</p>
<p>So I ran ProcessMonitor and filtered all the results by process name "ResGen.exe" and watched the log while the solution was compiling.</p>
<p>What I found was quite horrific :) ResGen was trying to repeatedly find .NET assemblies in specific versions that do not exist like System.Windows.Forms, Version=4.0.0.0 or System.Drawing, Version=4.0.0.0. I then had to get time differences between each RESX file read operation and so I realized what files were taking longer to generate the resources.</p>
<p>I had RESX files that were taking about 200 seconds to process.</p>
<p>I still do not know why the RESX files from my projects now and then get this malformed references but one thing I know for sure: if I replace the version attribute to 2.0.0.0 the problem goes away and I get the normal compilation times for my solution.</p>
<p><strong>Links</strong>:</p>
<ul>
<li>MSBuild Logging: <a href="http://msdn.microsoft.com/en-us/library/ms171470.aspx">http://msdn.microsoft.com/en-us/library/ms171470.aspx</a></li>
<li>Process Monitor: <a href="http://technet.microsoft.com/en-us/sysinternals/bb896645">http://technet.microsoft.com/en-us/sysinternals/bb896645</a></li>
</ul> <img src="http://geekswithblogs.net/tiagomargalho/aggbug/144831.aspx" width="1" height="1" />tiagomargalhohttp://geekswithblogs.net/tiagomargalho/archive/2011/04/13/visual-studio-2010---resgen.exe-using-25-cpu-compilation-taking.aspxTue, 12 Apr 2011 23:11:47 GMThttp://geekswithblogs.net/tiagomargalho/comments/144831.aspxhttp://geekswithblogs.net/tiagomargalho/archive/2011/04/13/visual-studio-2010---resgen.exe-using-25-cpu-compilation-taking.aspx#feedback7http://geekswithblogs.net/tiagomargalho/comments/commentRss/144831.aspx