ANTS Profiler Latest version: 4.0
Learning ANTS Profiler - 3.1
Profiling the memory usage of an application
This worked example gives you a guided tour of the memory profiling features of ANTS Profiler. It introduces you to the program's main features and shows you how you can use ANTS Profiler to profile your own applications. This worked example is based on a demonstration .NET desktop application, Shape Painter, that creates a series of objects and draws them on the screen.
You can run the Shape Painter program by double-clicking the executable file located by default in the following folders:
- For C# :
Program Files\Red Gate\ANTS Profiler 3\Tutorials\CS\ShapePainter
- For Visual Basic:
Program Files\Red Gate\ANTS Profiler 3\Tutorials\VB\ShapePainterVB
Note that this example refers to the C# version of the program; if you are using Visual Basic, you can follow the example, but you may see slightly different results.

The application creates random rectangle, ellipse, and triangle objects and plots them in the Shape Painter window. Each shape in the window corresponds to an object on the managed heap. This worked example shows how you can use ANTS Profiler to inspect the objects created by the application.
Setting up the profiler
If you have not yet started ANTS Profiler, select it from your Start menu; if it is already running, click
Project wizard.

Select Memory profiler to investigate the objects that Shape Painter has created and where those objects were created. Note that Memory profiler is a feature of the Pro edition.
Click Next to select the type of application.

Select .NET desktop application because the Shape Painter program is a desktop application. Click Next to specify the executable file.

Under .NET desktop application, click
to browse for the folder where the executable file is located. Locate the file ShapePainter.exe in the folder where you installed ANTS Profiler, for example, Program Files\Red Gate\ANTS Profiler 3\Tutorials\CS\ShapePainter.
Working Directory is the directory the application is launched from and is set to the location of the executable file; you can select a different working directory, if required. Arguments enables you to specify command line arguments for applications that require them; for this example, leave the box empty.
Click Finish.
Profiling the application
When you have finished setting up the profiler, a message dialog box is displayed. Click Yes to continue.
ANTS Profiler displays a message dialog box. Click OK to run the Shape Painter program and start taking snapshots. A snapshot records the state of the program at a particular point in time; to profile memory usage, you take a series of snapshots while your program is running.
The Shape Painter program starts and draws 200 shapes at random.
To take your first snapshot, in the ANTS Profiler toolbar, click
Take snapshot.
ANTS Profiler generates the first set of memory results and shows a summary of the results in the main window.
To prepare the program for the next snapshot, bring the Shape Painter program to the foreground. On the Shapes menu, click Add Rectangles; or click the Add rectangles button
in the toolbar. The Shape Painter program creates a further 10 rectangle objects.
Switch to the ANTS Profiler window, and click
Take snapshot to take the second snapshot.
ANTS Profiler generates the second set of memory results and shows a summary of the results.
Close the Shape Painter program; this automatically stops the profiler.
Viewing the results
The Summary panel shows basic information about the Shape Painter program for the second snapshot you took.

The Summary panel also shows:
- the 10 biggest live objects
- the 10 classes with the most live instances
Under Classes with most live instances, Live count shows the number of objects for each class. Make a note of the number of objects for the ShapePainter.RectangleShape class.
To see the results set for the first snapshot, move your mouse pointer over the Results tab.
The Results panel shows the two sets of results for the snapshots you took.

The results shown in bold are the results that are currently displayed in the main window. To display the first set of results, double-click the first results set.
ANTS Profiler shows the results for the first snapshot in the main window. Look at the Live count for the ShapePainter.RectangleShape class in the Summary panel. You will see that the number of objects differs by 10 between the two snapshots.
To see how many objects have been created in each class, you can click the All classes tab. Before you do this, click the Results tab and display the results for the second snapshot.

In the All classes panel, click the Class name column header to sort the classes by name. Scroll up or down so that you can see the RectangleShape class. Live size (bytes) shows you how much memory was used by all the objects in the RectangleShape class.
The All classes panel also shows you how many objects were added between the snapshots, and how much memory the new objects used. To see this information, you add the New count and New size (bytes) columns. To add the columns, right-click any column header, and click Choose columns. The Choose columns dialog box shows you the columns that you can add to the grid. Drag the New count and New size (bytes) columns to the required column header positions in the grid. For example:

You can see that 10 new rectangle shapes were added after you took the first snapshot.
To see all the objects that were created, click the All objects tab.

The Comparison column shows the state of an object since the first snapshot: New indicates that the object was created after you took the first snapshot, Different indicates that a property was changed between the first and second snapshots, and Identical indicates that the object has not changed.
You can sort, filter or group the objects as required. For example, to group the objects by namespace, drag the Namespace column header to the shaded grouping bar at the top of the panel.

Expand the ShapePainter class, and click one of the RectangleShape objects to select it. You can see the source code that created the object in the Source code panel. The Rectangle Shape objects were created by the method CreateFromRandom.
For full details of how to use the All objects panel, see All objects Panel.
To show details of objects that called the RectangleShape object or objects that were called by the RectangleShape object, click the Hierarchy tab.

You can see that the RectangleShape object is referenced by an instance of Object.
To see which namespace the object belongs to, you can add the Namespace column to the grid.
Right-click any column header in the Referenced from list, and click Choose columns. Drag the Namespace column to the required column header position in the grid. For example:

To see the sequence of method calls that created the RectangleShape objects, on the View menu, click Call stack to display the Call stack panel.

See Also |

Getting started
Worked examples