In Update 2 for Visual Studio 2013, memory diagnostic tools were added for Windows Store and Windows Phone that enable developers to monitor live memory consumption and take heap snapshots of their applications for further analysis. The Visual Studio team is extending the existing tools to work for Windows Desktop applications in the next major release of Visual Studio. For this CTP, the Visual C++ team has made some improvements to the current tools which now display the C++ type for individual heap allocations.
Starting a Diagnostic Session
To start a memory diagnostic session, select “Performance and Diagnostics” in the “Debug” menu or simply press Alt+F2. This will bring up the Visual Studio Diagnostics Hub where you can select the “Memory Usage” option which will currently default to native memory for a C++ application, although managed and mixed memory are also supported via the memory tool’s settings. The memory tool in the CTP does not support using other performance tools during the same session, so be sure to deselect any other tools before starting a memory diagnostic session. Information about other performance and diagnostic tools inside Visual Studio can be found on Dan Taylor’s blog on the Diagnostics Hub.
Starting a session will automatically launch your application and show the live memory consumption. During the session, snapshots can be taken of the heap state at different points in time to show relative growth and decrease in memory allocations.
Viewing Heap Snapshots
Stopping the session will allow you to open up snapshots and view the native heap with a list of allocations in memory. Each snapshot will display the amount of memory allocated and the number of allocations as blue links. For snapshots beyond the first, there are additional blue links that show the relative difference in memory between two heap snapshots (seen in Snapshot #2 below).
Clicking any of the links within a snapshot will bring up the native heap window with two views. At the top of the page is a list of frames in the call stack, and selecting one of these identifiers will show the allocations associated with the chosen function in the lower section. In the following example, there are five Volcano objects in memory that represent the major stratovolcanoes in Washington. The Volcano::Volcano constructor is selected in the top window of stack frames, and the list of allocations made for each Volcano object are listed below in the “Allocations” window. The list is aggregated by caller or callee which can be selected in the upper right.
When analyzing a heap snapshot, the debugger can be attached to the snapshot by right-clicking anywhere in the memory table and selecting “View Heap Contents” (seen above). The debugger brings some powerful functionality into heap analysis including expression evaluation on memory and data tips for allocations.
The animation below demonstrates navigating between the type view and the instance view of the native debug heap, as well as bringing up the source code for the Volcano class via “Go To Definition”. Initially the heap is presented as a list of object types with their count in memory, and selecting a type will bring up a list of the instances of each type.
Selecting a particular instance of the current type will display the call stack in a window below the native memory window. Here is the call stack for the Mount St. Helens Volcano object:
- Types are only shown for allocations originating from code compiled with the CTP’s C++ compiler and for which the .pdb is available.
- When the Just My Code setting is turned on during a memory diagnostic session, expression evaluation will fail during heap debugging.
- This is a pre-release and is subject to change; these tools are not ready for production use.
For pointers on diagnosing memory issues in Visual Studio 2013 Update 2, please consult the “Troubleshooting native memory” section of Harikrishna Menon’s blog post which provides a walkthrough of how to use the tools to fix a memory leak in a Windows Phone application.
We greatly appreciate your feedback and will take it into consideration for future improvements in these tools. Please let us know what you think by submitting feedback either online (UserVoice, Connect) or through the Feedback button in the upper right corner of Visual Studio (). Thanks!
*Updated 6/9/2014: Added information to “Starting a Diagnostics Session” section.