Click here to monitor SSC
  • Av rating:
  • Total votes: 85
  • Total comments: 3
Amirthalingam Prasanna

The Why and How of .NET Profiling

12 May 2008

Amirthalingam Prasanna gives a simple and practical guide about why you need to profile your .NET applications and how you would go about doing it.

Profiling your .NET code

Profiling gathers information about an executing application, allowing you to determine those improvements that are required in your application. Profiling is overlooked or not performed on applications despite the fact that it can have a massive benefit to the performance and scalability of these applications. This article starts with a "Top 10" list of reasons why you should give careful consideration to the task of profiling your .NET code. It then introduces the two basic tools that will get you started with this task, namely the CLR Profiler and Perfmon.

Ten reasons why you should bother profiling your .NET code

1) Focus on portions of your .NET code that really require attention

Profiling allows you to focus on critical sections of your .NET code. When you have a very large application it is very difficult to identify areas in your .NET code that require improvement. Profiling your application will pinpoint .NET code sections that really require improvement or tuning.

2) Identify code blocks with performance issues

Performance is a very hard thing to measure and trace. The task of identifying code blocks or methods that have performance issues is tedious. Profiling your .NET code helps you to identify those code lines, blocks or methods that require performance improvement.

3) Compare alternative approaches

During your development, you might come across alternate ways of achieving a task. For any given task you might have two different implementations that you wish to compare, in order to find out which implementation is better in terms of performance, scalability, and resource usage. By comparing your different implementations with a profiler, you can select the most efficent code block.

4) Get accurate code execution response times

Often, when you examine the performance of your application, you will want to find out how long a line of code, or a block of code, or a method, takes to execute. By profiling your .NET code you can get accurate execution times.

5) Avoid guessing performance issues

Have you ever looked at your application and felt that it is executing slower than usual? You cannot just go by your instinct and start making changes to your application. By profiling your application, you can confirm whether performance issues exist in your application, and where those issues are.

6) Visualize performance and memory usage

A visual depiction of execution times and memory usage for your application helps you to make informative decisions very quickly. Once you have the means to see a graph of the execution times or memory usage, it is much easier and quicker to understand issues and fix them.

7) Track the lifecycle of your .NET objects

You might be using a resource intensive object in your .NET code. Tracking the lifecycle of your .NET object will allow you to make optimizations in your code. For example you might be creating the resource intensive object too early in your application. Profiling will uncover these issues.

8) Avoid unnecessary loading or initialization of your program

During development of your application you might have had some tests that are loaded and initialized. Prior to deployment of your application you will want to ensure that any unnecessary loading or initialization is removed. It would be very difficult to track such portions of your application without profiling it.

9) Optimize your looping constructs in .NET

Looping constructs are a common source of performance issues. Profiling your code allows you to understand and eliminate unnecessary loops within your looping constructs. Improving your looping constructs in turn will improve the overall performance of your .NET application.

10) Identify memory leaks in your application

Memory leaks in your application can be very difficult to identify. Profiling your .NET code allows you to identify any unnecessary memory usage and therefore optimize the memory usage in your .NET application.

How to profile your .NET code

Now that we have looked at some compelling reasons to consider profiling, let us look at a couple of the basic, free tools that allow you to profile your .NET code.

CLR Profiler

The CLR Profiler is a free application that allows you to profile Windows applications, ASP.NET applications or services. You can download the CLR profiler from here. Once you have downloaded it, you can extract it and run the CLRProfiler.exe. When you run the CLR profiler your code will appear to execute slower than usual since your code will be instrumented to analyze the performance.

Let us create a simple Windows application and then analyze it using the CLR Profiler. You can create the following method as part of your application and invoke it (you can download Visual C# 2008 Express Edition from here ):

C# Code

public void CodeToProfile()

{

string str = string.Empty;

for ( int i = 0; i < 2000; i++)

{

str = str + "|" + i.ToString();

}

MessageBox.Show(str);

}

Start the CLR profiler, and you will see the following initial screen:

Selecting "Allocations and Calls", allows us to profile the methods as well as memory usage. You can click on "Start Application" and select the Windows application we created. The CLR profiler will load the Windows application and you can execute it to call the method we wrote earlier.

When you close the Windows application, the performance of the application is analyzed and a summary screen is presented:

You can drill into any specific areas on the performance. For example, if you want to find out how much memory was allocated on the Heap, you can click on the "Histogram" for Allocated Bytes, in the summary screen. The View menu in the main CLR profiler screen also includes menu items for the options you see on the summary screen for your application.

The above screen shows that a large amount of memory is being allotted for the strings, since we are doing concatenation and each concatenation is creating a new string instance. Let us save the current profile results.

To fix the above problem, let us replace the string concatenation with a StringBuilder. So, our new method implementation looks as follows:

C# Code

public void CodeToProfile()

{

StringBuilder str = new StringBuilder();

for ( int i = 0; i < 2000; i++)

{

str.Append( "|").Append(i.ToString());

}

MessageBox.Show(str.ToString());

}

Let us go through the steps of profiling the application again. After profiling, the "Histogram by Size for Allocated Byes" screen looks as follows:

You can see that the memory allotted is considerably reduced using an alternate implementation.

Using PerfMon

Another tool you can use to analyze the performance of your application is the PerfMon (Performance Monitor). This tool is available as part of Windows Administrative tools and contains performance counters to analyze various aspects of your application. For example, in Windows XP, navigate to Administrative tools and select Performance. This will start the Microsoft Management Console for PerfMon.

The initial view shows the System Monitor monitoring a few performance counters for the entire system. To monitor .NET application-specific performance, click on the "new" option on the toolbar. This will clear the current view and you can add performance counters for the specific aspects for your application

To add a performance counter, click on "Add". This brings up the "Add Counters" interface that allows you to add a performance counter. PerfMon contains a lot of counters that are specific for .NET applications. For example, to monitor the memory allocated on the heap for your .NET objects, you can select the .NET CLR memory Performance object and then select the "# Bytes in all Heaps" performance counter. If you want to monitor all .NET CLR memory related counters, you can select "All counters". You can also select a specific instance or application for which you want to monitor the .NET CLR memory.

This creates a graph that will show any spikes in the view when you execute your application. This allows you to graphically visualize the memory usage while you interact with your .NET application. If there are any unusual spikes while executing your application, you can try improving your code and running the application again while monitoring the performance in PerfMon.

Other than the .NET CLR specific performance counters, you also get a lot of counters that are specific to ASP.NET web applications. For example, you can have a view that shows the total requests that failed for the ASP.NET application against the total requests received, and also monitor how specific resources such as memory were used when the number of requests is high.

Conclusion

Profiling your .NET code is a very important task in ensuring your application performs as expected. This article shows 10 reasons why you should seriously consider profiling your .NET code and then explains how to use the CLR profiler and the PerfMon tools.

Amirthalingam Prasanna

Author profile:

Prasanna is a software engineer, technical author and trainer with many years of development and consulting experience in the software development industry. He is a Microsoft MVP in the Visual developer category, and a MCPD on enterprise application development. He has authored many articles and has worked on content creation for many Microsoft certification exams and courses. He is also a frequent speaker at Microsoft technology conferences and events. You can read his blog at www.prasanna.ws and e-mail him at feedback@prasanna.ws

Search for other articles by Amirthalingam Prasanna

Rate this article:   Avg rating: from a total of 85 votes.


Poor

OK

Good

Great

Must read
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.


Subject: Nice ARticle
Posted by: Srinivas Neti (not signed in)
Posted on: Tuesday, June 24, 2008 at 6:30 AM
Message: Hi Damon.,

I appreciate Your article. It is simple, precise and so accurate I ever seen.

Best Wishes.,
Srini.

Subject: Sorry
Posted by: Srinivas Neti (not signed in)
Posted on: Tuesday, June 24, 2008 at 6:33 AM
Message: Hi Amirthalingam Prasanna,
Sorry., I by mistake., mentioned another author's name Damon(just copy paste mistake!)
Regards
Srini.

Subject: Good One
Posted by: iammuruga (view profile)
Posted on: Tuesday, January 06, 2009 at 12:00 PM
Message:
Really a nice article.

thanks Amirthalingam Prasanna.

 

Top Rated

Acceptance Testing with FitNesse: Multiplicities and Comparisons
 FitNesse is one of the most popular tools for unit testing since it is designed with a Wiki-style... Read more...

Acceptance Testing with FitNesse: Symbols, Variables and Code-behind Styles
 Although FitNesse can be used as a generic automated testing tool for both applications and databases,... Read more...

Building Performance Metrics into ASP.NET MVC Applications
 When you're instrumenting an ASP.NET MVC or Web API application to monitor its performance while it is... Read more...

Acceptance Testing with FitNesse: Documentation and Infrastructure
 FitNesse is a popular general-purpose wiki-based framework for writing acceptance tests for software... Read more...

TortoiseSVN and Subversion Cookbook Part 11: Subversion and Oracle
 It is only recently that the tools have existed to make source-control easy for database developers.... Read more...

Most Viewed

A Complete URL Rewriting Solution for ASP.NET 2.0
 Ever wondered whether it's possible to create neater URLS, free of bulky Query String parameters?... Read more...

Visual Studio Setup - projects and custom actions
 This article describes the kinds of custom actions that can be used in your Visual Studio setup project. Read more...

.NET Application Architecture: the Data Access Layer
 Find out how to design a robust data access layer for your .NET applications. Read more...

Calling Cross Domain Web Services in AJAX
 The latest craze for mashups involves making cross-domain calls to Web Services from APIs made publicly... Read more...

Web Parts in ASP.NET 2.0
 Most Web Parts implementations allow users to create a single portal page where they can personalize... Read more...

Why Join

Over 400,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.