This is a showcase review for our sponsors at CodeProject. These reviews are intended to provide you with information on products and services that we consider useful and of value to developers.
As .NET developers, we know that our applications rarely perform as well as we had hope the first time we write them. Two of the more common issues are:
- Performance bottlenecks – the application can appear to lock up for a noticeable period of time.
- Memory leaks – with the memory usage constantly increasing the longer the .NET application runs.
To efficiently address these sorts of issues, we require an effective means of profiling your application – and this is where ANTS Profiler comes into its own.
ANTS Profiler is a code profiling tool designed for .NET developers who want to get to the bottom of performance and memory issues. Technical testers also use ANTS Profiler to pin down bugs to a specific line of code or to determine the scope for optimization.
The first version of ANTS Profiler gave .NET developers a really simple tool for profiling the performance of their ASP.NET web applications, written in VS.NET 2002. In the 4 years since this first release, Red Gate has continually developed and refined the tool. The latest release adds some really nice new features, and brings the tool right up to date, with full support for profiling on the Windows Vista platform.
ANTS Profiler 2.7 allows you to profile pretty much any .NET code that you may be developing. It supports .NET desktop applications written in any of the .NET languages (C#, VB.NET, Managed C++ etc.) and targeting any of the .NET Framework versions (.NET 1.0, 1.1, and 2.0). You can also profile ASP.NET web applications and .NET Windows Services.
One of the nice features of 2.7 is the way we profile ASP.NET web applications on Windows 2003 Server. You no longer have to restart the web server to profile your web application. In fact, you could keep the original web site running, and profile a separate instance of the site running in a completely separate process!
What ANTS Profiler does
When you investigate the performance of your application with ANTS Profiler, the profiling results will immediately show you the 'Top 10' slowest lines of code that have run. You will be able to see your source code just as you wrote it, allowing you to quickly get on with what you do best – fixing up that code!
You will also be able to see how long every method took to execute, from the slowest to the very fastest. You can also take a snapshot of the memory usage while your application is running to see how much .NET memory you are consuming. ANTS Profiler will highlight the biggest ‘memory hogs’, but will also show you all the live objects and the reasons why they are still in memory. A quick comparison of a couple of snapshots is a really nice way to spot memory leaks in your application.
Profiling an application with ANTS Profiler
Let’s go ahead and profile an application with ANTS Profiler. When you start up ANTS Profiler, the project wizard will walk you through setting up the profiler. You first need to indicate whether you are interested in profiling performance or memory.
Next, you choose the type of application you are profiling (Windows Forms, ASP.NET etc.).
After that, you simply fill in the details of your application, and also the areas that you are interested in profiling – for example, you would typically start off by profiling only the methods for which you have the source code – then you’re ready to go!
When you choose to profile ASP.NET, ANTS Profiler works out from IIS what web sites are currently running, and presents them to you in a drop down list – another nice touch that makes using the tool that bit easier.
Performance profiling: controlling the profiler
When you are profiling the performance of your application, there are a few useful extra features that are worth investigating. For example, on the main toolbar, you can disable the profiler and temporarily stop ANTS Profiler from gathering any statistics. A useful tip is to disable the profiler, load your ASP.NET web application – which will happen much faster with profiling disabled, and then enable the profiler once your main page appears.
You can also reset the profiler so that you can collect performance data from a small subset of your application. This can be a very useful technique if you want to investigate one particular bottleneck.
Getting some results
ANTS Profiler will automatically generate results when you close down your application. However, you can also take snapshots of performance or memory whilst your application is running.
When investigating bottlenecks, the summary of performance results is normally your first port of call. Here, you can see basic information about your application followed, more importantly, by a top 10 list of the biggest potential bottlenecks in your application.
Clicking on a particular slow line of code naturally shows you the source code for that line:
The red bars guide you to the slowest lines of code so you can immediately see where to start making the changes that will really make your application fly.
ANTS Profiler also includes a view that shows all the methods that have been profiled. You can group and filter this with a couple of clicks to get to see the data that is most relevant to you. For example, here I have grouped the method by the assembly to which they belong:
Hunting down memory hogs and memory leaks
.NET provides a managed memory model, so memory leaks are a thing of the past, right? Wrong! Although automatic memory management in .NET makes our lives as .NET developers a whole lot more enjoyable (who enjoyed having to write destructors for every single C++ class), it is still easy to introduce memory leaks into your application. Tools such as Task Manager and Performance Monitor may show you that your application is using up a lot of memory, but only a dedicated memory profiler like ANTS Profiler can really show what objects make up that memory and why they are still there.
.NET uses a garbage collection model to manage the memory. From time-to-time, the garbage collector will run, and work out which objects in your application are still ‘live’. Any objects that are no longer live can be collected and the memory released. Basically, an object is still ‘live’ if it can be reached by another ‘live’ object. You can visualize the .NET memory as an enormous spider's web where the individual strands represent the links between the objects.
The memory profiling side of ANTS Profiler shows you all the live objects along with the specific parts of that ‘spider's web’ that link them, so that you can see what steps to take to release memory and fix the leak.
Typically, a large amount of memory is stored in arrays. The screenshot below shows the hierarchy for just one such array. We see on the right hand side all the objects that are contained in the array. A fairly common error is to add lots of objects to an array, but never remove them (even when they are no longer needed!). This will always result in a memory leak as the array will keep the objects alive forever.
Another common technique for investigating memory problems is to compare two snapshots of the memory. Typically you would:
- take one snapshot of your application when it is up and running
- go ahead and run some part of the application you think is leaking memory, and then
- take a second snapshot.
In ANTS Profiler, you can compare the two snapshots by filtering the ‘All Objects’ panel to only show ‘New’ objects.
This filtered view now shows only the objects that have been created since the first snapshot. If you expect that no memory should have been created, then all of these objects comprise your memory leak. You can use the hierarchy panel to see what other objects are referencing them, and so work out why they have not been garbage collected.
An essential tool for every .NET developer
ANTS Profiler is one of the best .NET profiling tools on the market. It gives you the information you need, simply and quickly. It shows you the pieces of code that need to be optimized to improve the overall performance of your apps, and provide your end users with a more enjoyable experience!
It is simple to use, and the results produced make it easy for any .NET developer to seek out potential bottlenecks in their code. Investigating memory problems is fun and interesting, and makes fixing memory leaks straightforward.
If you want to find out more, there are a number of excellent reviews of ANTS Profiler. One reviewer described it as "an excellent tool that allows you to really pinpoint those portions of your code that cause the most hassle for your end users".
Evaluate ANTS Profiler with your applications to see how well it will work for you - download a free, fully functional, 14-day trial.