WYSIWYG

http://kufli.blogspot.com
http://github.com/karthik20522

Thursday, June 11, 2009

Using Debug Diagnostics to extract and analyse Memory Dump

Download Links:

TinyGet: http://www.microsoft.com/downloads/details.aspx?FamilyID=56FC92EE-A71A-4C73-B628-ADE629C89499&displaylang=en

DebugDiag: http://www.microsoft.com/DOWNLOADS/details.aspx?FamilyID=28bd5941-c458-46f1-b24d-f60151d875a3&displaylang=en

Since I primarily work on web-applications (ASP.NET 2.0) it has become a reflexive action to check for any memory leaks or memory buildup and always try to refactor the code for smaller memory footprint and speed optimization. Since most of the primary test is done on the development machine (Windows XP IIS 5.5), I use TinyGet to simulate traffic to the particular page that I intend to test. Though I am a big fan of JMeter but I still prefer using TinyGet due to the ease-of-use.

* Before we can use TinyGet to simulate the traffic, we need to activate the DebugDiag to capture the memory of the IIS worker process. In the Debug Diagnostics tool, navigate to the "Processes" tab and right click on the w3wp.exe or aspnet_wp.exe process and select the "Monitor for leaks" option.



* Once Monitoring is tracking use tinyget to simulate request to the test page. Below command simulates 900 requests to the default.aspx page on localhost (server) and running on port 80. The "-h" parameter displays the header information of the page requested.

c:\>tinyget -srv:localhost -port:80 -uri:/default.aspx -loop:900 -h



* Once the traffic simulation is done using TinyGet, extract the memory dump by right clicking the aspnet_wp process and selecting the "create full memory dump" option.



* After extracting the memory dump file, navigate to the "Advanced Analysis" and choose "Memory Pressure Analyzer" and selected the dump file as the Data File.



By default Debug Diagnostics tool has two Analysis Scripts namely "Crash/Hang Analyzers" and "Memory pressure Analyzers". One another useful script is the "DotNetMemoryAnalysis" script written by Tess Ferrandez [http://blogs.msdn.com/tess/archive/2009/05/12/debug-diag-script-for-troubleshooting-net-2-0-memory-leaks.aspx]. Following is an example screen shot of analysis result:



Debug Diagnostics is a pretty useful and powerful tool for a quick analysis of memory and crash dump analysis. It gives you a quick peak into number of threads, memory usage and availability in the Heap and fragmentation information and lot's more data to drool about. A very handy tool for a performance and debug engineer!

Labels: , ,

Using String.Intern Benefits C#

Recently I was given a daunting task of figuring out why the web-server (IIS) was recycling every couple of hrs and when I analyzed the Crash Dump file there it was obvious that "System.String" type objects were occupying about ~250MB in memory and probably not being released or garbage collected fast enough. While some of String objects were all greater than 85000 bytes it was stored in Gen 2 [LOH - Large Object Heap] instead of Gen 0 and probably highly fragmented. So I decided to try to replicate same issue of my development machine so I hooked up the DebugDiag Memory leak process to my "aspnet_wp.exe" process (IIS 5.5 Windows XP) and simulated traffic using "tinyget" (Microsoft IIS Tool) to one of my test pages which performed similar string manipulation as the live page.



From above screen shot we can see that the String.Object type is ~10MB with 109894 objects in memory which looked quite a lot just to display data from DB and some simple string manipulation.

So while digging around MSDN [http://msdn.microsoft.com/en-us/library/system.string.intern.aspx], I was introduced to String.Intern method. Basically .NET creates a Intern Pool which contains a single reference to each unique literal string declared or created programmatically.

After I modified my application to use String.Intern (basically I added String.Intern in my property (get;set)) in my Business Entity class's and simulated the traffic again and created a new memory dump and following was the result:



As you can see from my above screen shot, the "string.object" memory and the number of instances have reduced to ~3.5MB and 21447 respectively when compared to ~10MB and over 100K objects. Though this string.intern has reduced the string.objects but one main drawback is the fact that the intern pool is not flushed until app recycles. This pool is global and objects in the pool remain for life of the app.

Following is a sample of my BusinessEntity Class:



Syntax: string.intern("string object" or "text")
The reason why I used "string.intern(value ?? "")" is because string.intern does not accept null values. Since I added string.intern in Getter's and Setter's, I had to make sure the value is not NULL and by default I set it to "". Also another example would be string concatenating the same data over and over again like the following example:



As you can see above that "/images/ratingBig-" is being concatenated or basically a constant value which over hear would make sense to use String.Intern to create only one instance of the global string object.

I personally feel that String.intern works wonders or atleast works for my situation but word of caution about it's global scope of intern pool objects.

Labels: ,