How to: Generate a Garbage Collector Heap Dump

Explains how to generate a heap dump using the XNA Framework Remote Performance Monitor.

Introduction

To help diagnose memory leaks related to objects that are kept alive inappropriately, the XNA Framework includes a remote heap dump utility. The heap dump utility is integrated with Remote Performance Monitor. Because of this, you can view the heap dump while you are running the monitor.

XNA Framework 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.

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

This topic describes how to view, save, open, and compare heap dumps.

Generating a Garbage Collector Heap Dump

To view a heap dump

  1. Start Remote Performance Monitor.

  2. Attach it to a process.

    For more information, see How to: Monitor Performance at Run Time (Xbox 360).

  3. In the Live Counters window, click View GC Heap.

    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 by the application are shown in the view. No .NET Compact Framework class library types are shown in the view.

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

  5. Click Refresh Tree to update the view.

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

  6. If you want to view object references for an object in the heap, either 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

  1. Start Remote Performance Monitor.

  2. On the File menu, click Open GC log, and select a previously saved heap dump file.

To compare heap dumps

  1. Start Remote Performance Monitor.

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

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

  3. On the View menu, click Compare Heap.

    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 heap 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.

See Also

Concepts

How to: Monitor Performance at Run Time (Xbox 360)
Heap Dump File Information