
266
|
Chapter 7, Help and Research
#67 Profile Heap Allocations
HACK
HACK
#67
Profile Heap Allocations Hack #67
Use the Allocation Profiler to research how your objects are being handled on
the heap.
This hack is an introduction to the Allocation Profiler tool available at www.
gotdotnet.com. The Allocation Profiler tool allows developers to analyze allo-
cations on the garbage collection heap. The Allocation Profiler answers
questions about the object(s) in your application such as: How many times
was my object created in memory? At what point in execution time was my
object allocated? How long was my object alive?
Let’s write a very basic C# application to see the Allocation Profiler in its
simplest form. The application will run a loop and create 10,000 instances
of our simple class.
Here is the code for AP_example1.exe:
// main.cs
using System;
namespace AP_example1
{
/// <summary>
/// Summary description for main.
/// </summary>
// basic class that creates a string object
public class AP_example_class
{
string s = String.Empty;
}
public class AP_example1
{
public static void Main( )
{
// create 10,000 instance of our class
for(int x=0;x<10000;x++)
{
AP_example_class aec = new AP_example_class( );
}
}
}
}
After compiling AP_example1, open AllocationProfiler.exe, which is shown
in Figure 7-39.