How to: Generate a Garbage Collector Heap Dump

.NET Framework 2.0

Managed code applications can experience memory leaks, although the garbage collector mitigates this issue by releasing objects that are no longer referenced.

The garbage collector walks the application roots to identify storage locations for objects on the managed heap that are no longer needed. The term object root refers to the object's associated application root, such as a global or static object pointer. The garbage collector will not release any object that is kept alive by a root, even if the application does not need the object any longer. For example, an object might be rooted by a static variable. Because static variables live for the life of the process, unless they are explicitly set to null, the objects they reference will be kept alive as long as the process is alive.

To help diagnose memory leaks related to these issues, the .NET Compact Framework version 2.0 SP2 includes a heap dumping utility. The heap dumping utility is an extension of Remote Performance Monitor; therefore, you can view the heap dump while you are running the monitor.

Viewing the heap dump provides the following information about the heap:

Garbage collector heap statistics.

Root of each object in the heap.

Object references for each object in the heap.

In addition, you can use the heap dumping utility to save heap dumps, view previously saved heaps, and compare object instances in different heap dumps

To view a heap dump

This generates a heap dump in Remote Performance Monitor. The main view of the heap provides general statistics, a tree view of each object and its root, and a table that contains the number of instances of each type together with its cumulative size. By default, only types that are defined in the application are shown in the view. No .NET Compact Framework class library types are shown in the view.

Select the check boxes that correspond to the specific types that you want to see in the view of the heap.

Select Refresh Tree to update the view.

You can use this view to determine the root that keeps a particular object instance alive.

If you want to view object references for an object in the heap, right-click the object and click View Object References.

- or -

On the File menu, click View Object References.

The object references appear in a new window.

To save a heap dump

Save the current heap dump when you close the view of the current heap dump and you are prompted to save.

To open a previously saved heap dump

Start Remote Performance Monitor.

Click Open log from the File menu and select a previously saved heap dump file.

To compare heap dumps

Start Remote Performance Monitor.

In the Live Counters window, select View GC Heap multiple times to generate heap dumps for comparison.

You can also open previously saved heap dumps to include these in the comparison.

Click Compare Heap from the View menu.

This generates a table view of all heaps that are currently open. Each column represents a heap, with the oldest heap appearing first and the most recent appearing last. Each row in the column displays the number of instances of the specified type contained in the heap. If this number differs from the previous heap, the difference is shown in parentheses. For example, an entry of 152(+90) indicates that the specified object had 90 more instances than it had in the previous heap.