Visual Studio Hacks [Electronic resources]

Andrew Lockhart

نسخه متنی -صفحه : 172/ 100
نمايش فراداده

Hack 67. Profile Heap Allocations

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 Figure 7-39.

Figure 7-39. AllocationProfiler

The Profiling Active box should be checked and Allocations should be chosen from the Profile radio button list on the right.

Choose Profile Application from the File menu and browse to AP_example1.exe.

The Allocation Profiler will execute your application and log everything that goes through the garbage collector. When the application has finished executing, it will automatically launch the Allocation Graph, which is shown in Figure 7-40.

Figure 7-40. Allocation Graph

The Scale option allows you to choose the number of pixels to display proportional to allocated memory.

The Detail option shows more objects the lower you go in the scale. If you choose 0 (everything), you will see every object allocated. By choosing 20 (coarse), you will get a higher-level view of the core objects in your application.

Figure 7-40 shows you a hierarchical drill-down of the managed memory allocated in your application. In this example, the application has allocated 126 KB of managed memory. Of the initial 126 KB (100%), 117 KB (93.15%) has been initialized from the AP_example1::Main method. Since the only thing in the main method was a loop creating the AP_example_class object, AP_example1.AP_example_class is also 117 KB (93.15%) of the managed memory in the application.

The lowest levels of objects in the graph represent the percentage of the entire application, not the percentage of their parent.

From the main menu, choose View Histogram Allocated Types. The result is shown in Figure 7-41.

Figure 7-41. Histogram allocated types

Figure 7-41 shows you kilobytes allocated by object. By hovering over each item in the histogram, it will show you the number of instances created and their average size. You can right-click on the bar to show where the instance(s) were allocated and export the data.

From the main menu, choose View Objects by Address to open the window shown in Figure 7-42.

Figure 7-42. View Objects by Address window

In Figure 7-42, each vertical bar shows you an address range for each GC heap. There are two heaps for the system build, the normal heap and the large object heap. In this example, if you hover over the first bar in the graph, it will show you the object address for each instance you allocated. In addition, it will show you the size (in bytes) and how long the object was allocated in memory.

From the main menu, choose View Histogram by Age, which is shown in Figure 7-43.

Figure 7-43. Histogram by Age window

In Figure 7-43, each vertical bar in this graph shows you a histogram of created objects by time. The time the object was in memory determines which bucket the object''s instance will be included in.

From the main menu, choose View Time Line, which is shown in Figure 7-44.

Figure 7-44. View Time Line window

In Figure 7-44, the vertical axis of this chart shows you the memory addresses allocated. The horizontal axis shows you the time in which they were allocated. In this example, somewhere around .2 seconds into the application, almost 100% of the objects were allocated. You can right-click at any point in the timeline to view objects by address at that time or by any of the recent features I covered in this hack.

The preceding example was intended to show you the analysis the Allocation Profiler provides. In a real-life application, you would use the Allocation Profiler to verify that the allocated objects are the ones you expected, view potential memory leaks, view how long objects are on the GC heap, and view what percentage each object allocates in your overall application memory usage. For more information, download the Allocation Profiler from and refer to the included documentation.

Jayme Davis