I was going through CLR Profiler to use as a first step to check memory utilization on every application to make it as a process while delivering projects to clients. Though, there are better code profiler which can let us know more than what CLR Profiler can, I have taken CLR Profiler as it is Microsoft’s and its free. CLR Profiler does profling to know the memory consumption of your application like freed memory, reused memory, and allocated memory of the whole application. Having said, it includes all the .Net object’s memory consumption too, which is too much to have a look at. There is no way you can omit the .Net namespaces and have only the specific namespaces to be proflied. [Note : We can write our own code as the source code for the CLR profiler is available along with the application download. ]
While I was struggling to use it in Vista, I happen to see a new feature called Code Analysis in VS 2008. I was curious to know more about it and visited the Somesegar’s blog (http://blogs.msdn.com/somasegar/archive/2007/10/04/code-analysis-features-in-vs-2008.aspx).
Using Code Analysis feature in VS 2008, we can analyze the code, get the code metrics and also profiler the application to view the memory consumption. Let us see visit each of them.
Code Analysis
There is nothing new about this for people who have used FxCop or StyleCop already. In Visual Studio 2008 Developer Edition and Team Edition, we have a new menu item ‘Analyze’ which will help us in configuring, running and setting the Code analysis to run while code check-in.
Code analysis allows us to check our code against the Design rules, Usage rules, Naming rules etc which are designed to meet the Microsoft code standards and recommendation. We can enable this feature by doing a right click -> Properties -> Code Analysis -> selecting the checkbox ‘Enable Code Analysis on Build’ and choose the specific rules if needed. In case of website, we can choose the menu ‘Website’ in the VS IDE and choose to ‘Code Analysis Configuration’ or choose Analyze -> Code Analysis Configuration. Simillarly, use Website menu, Analyze menu or use the context menu in the solution explorer to Run the Code Analysis on Web site. For libraries, we get to see ‘Run the Code Analysis’ on the context menu (right click on class library in solution explorer).
Code Metrics
This feature in VS 2008 helps us in checking the Code Metrics of our projects / solutions. Please note that this feature will not give us the Code Metrics for website created in C#, Visual Basic projects. But it works fine for libraries.
To do this, choose the ‘Calculate Code Metrics’ from the context menu in the solution explorer by right-clicking on the Solution. The same can be done using the Analyze menu item in the VS 2008 Developer edition / Team edition.
This feature currently supports five different metrics; Maintainability Index, Cyclomatic Complexity, Depth of Inheritance, Coupling of Class and Lines of Code. The explanation for each of these are available in the blogs http://blogs.msdn.com/fxcop/archive/2007/02/28/announcing-visual-studio-code-metrics.aspx and http://blogs.msdn.com/fxcop/archive/2007/10/03/new-for-visual-studio-2008-code-metrics.aspx
Code Profiling
This reduces the burden of using CLR Profiler to know the memory consumption of the whole application which throws up the heap memory usage of even the .Net objects. The Code profiling in VS 2008 lets us know the Types with Most Instances created, Types with Most Memory allocated, and Function allocating most memory.
To do a code profiling, choose ‘Launch Performance Wizard’ from the ‘Analyze’ menu. Follow the wizard to choose the target application to profile and the profile type (Sampling or Instrumentation). Once the Wizard is finished, we will get to see a Performance Explorer window having the performance session created with Target and Report as the sub items under the session tree.
Choose the properties of the session and set the desired properties to profile for Performance counters. We can now launch the profile and get a performance report. We can click on any of the rows to drill down further to check the function calls (stack trace) with memory consumption. The data view once we click on the row will open up to show memory consumption by Functions, Objects, Call Tree, Caller / Callee, Object Life time etc.
References :
Code Profiler
http://msdn.microsoft.com/en-us/library/ms182372(VS.80).aspx
http://msdn.microsoft.com/en-us/library/ms182372.aspx
http://blog.maartenballiauw.be/post/2008/02/Code-performance-analysis-in-Visual-Studio-2008.aspx
Code Metrics
http://blogs.msdn.com/fxcop/archive/2007/02/28/announcing-visual-studio-code-metrics.aspx
http://blogs.msdn.com/fxcop/archive/2007/10/03/new-for-visual-studio-2008-code-metrics.aspx
VS 2008 Edition Comparison
http://www.microsoft.com/visualstudio/en-us/products/teamsystem/default.mspx#compare_products)