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. AllocationProfilerThe 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 GraphThe 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.
From the main menu, choose View
Figure 7-41. Histogram allocated typesFigure 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
Figure 7-42. View Objects by Address windowIn 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
Figure 7-43. Histogram by Age windowIn 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
Figure 7-44. View Time Line windowIn 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
|