Recording a Heap Snapshot

Windows Performance Recorder (WPR) enables snapshot of heap for specific processes on the system.

This heap snapshot is different from the heap analysis recording, as it takes snapshot view of heap. For example, a Heap Snapshot saves all heap allocation information in the database. When the SingleSnapshot command is executed, it exports the allocation information to the trace buffer. The snapshot contains allocation information including all stacks that are created after the snapshot was enabled.

To capture a heap snapshot trace

  1. Enable the heap snapshot trace There two ways of enabling a heap snapshot trace, by process name and by process ID.

The following example uses the process name:

 wpr -snapshotconfig heap -name heaptest.exe           //query snapshot config
 wpr -snapshotconfig heap -name heaptest.exe enable    //enable snapshot config

The following example uses the process ID or PID:

wpr -snapshotconfig heap -pid 8048 enable                //enable snapshot config
  1. Start tracing using wpr or wprui

using wpr :

wpr -start heapsnapshot -filemode                          

If -name option was used to enable heap snapshot, you need to launch the process after the configuration is enabled.

  1. Take the heap snapshot

Each time a snapshot is triggered, the allocation stack database is exported to the trace buffers. A single snapshot (can be used multiple times), for example:

 wpr -singlesnapshot heap <pid for foo.exe>

Periodic snapshot:

 wpr -enableperiodicsnapshot heap <frequency in seconds> <pid for foo.exe>
  1. Save the trace by using wprui, or wpr.
wpr -stop heapsnapshot.etl
  1. Disable the heap snapshot
    You can disable the heap using the PID or Name. Here are examples of both.

Using Name:

wpr -snapshotconfig heap -name Win32Project1.exe disable

Using PID:

 wpr -snapshotconfig heap -pid %_PID% disable

Note: If the -name option is used to enable the heap snapshot, it sets the IFEO registry internally and will apply to all new instances of the process. Disable it by using the SnapshotConfig option when testing is finished to avoid unnecessary heap collection. Also, if the cpu architecture of process and OS does not match ( for example, running win32 app on 64bit OS), use the name option (IFEO).