Native Memory Diagnostic Tools for Visual Studio “14” CTP

Native Memory Diagnostic Tools for Visual Studio “14” CTP

Rate This
  • Comments 17

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.

 

 

Heap Debugging

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:

 

Note: 

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

Leave a Comment
  • Please add 4 and 2 and type the answer here:
  • Post
  • Awesome tool. But why it is not available even in CTP "14"? i61.fastpic.ru/.../73caebd18f5230c9facfd20eac375c7a.png

  • I'm assuming this is only available if using the default allocators. Is there any Debug API that can help with creating custom allocators capable of reporting the same kind of data?

  • When I compare two snapshots, is there any way that I can narrow/filter the Allocations pane to show only the newly-allocated objects? If I have 100,000 objects allocated and there's +4 between two snapshots and I want to see the particulars of those four items I'm in a world of hurt trying to diff two lists of 100,000 and 100,004 addresses.

  • Should "View Heap Contents" work with an application which has some native and some /clr code? It doesn't seem to work even when trying to look at objects allocated in the native portion of my code.

  • Thanks for acknowledging the continued existence of desktop application developers ;-)!

  • @Roman, this is in the Visual Studio "14" CTP.

    @Eddie, you can decorate your custom allocator with a declspec. The declspec goes after linkage and before the return type.

    extern "C" __declspec(allocator) SomeType* some_allocator_function();

    or if you don't need the linkage,

    __declspec(allocator) SomeType* some_allocator_function();

  • @jschroedl,

    You can look at the native allocations of a mixed app. For that you need to select mixed or native in "Memory Usage" settings.  (Start "Performance and Diagnostics" using ALT + F2 and select "Memory Usage" then open the settings link next to the "Memory Usage" and set it to "Mixed" or "Native". )

  • @ jschroedl - comparing two snapshots coming soon.

  • @Roman, I'm standing here with Adam going over your question. I think I misunderstood. You have the Performance Wizard selected. We don't support using the Performance Wizard and Memory Usage tools at the same time.

  • Thanks for the replies @Iyyappa, that's great news about the snapshot comparison coming. I am pretty sure I selected Mixed in the collection options but I'll try again.

  • > You have the Performance Wizard selected. We don't support using the Performance Wizard and Memory Usage tools at the same time.

    Can you please display some kind of alert (popup window?) telling users that you don't support such and such.. UX is as significant as the feature it self...

  • @ Terry - Thanks for your feedback. Sure we will consider your suggestion.

  • Hi,

    This looks very useful. However I wonder if this will be usable to debug an application that is registered as a service or in COM+? This is what my main role involves and there does not seem to be any good memory tools for me to be able to run simulations on the code I am writing and to get any usable debugging information.

    Regards,

  • Hi,

    This looks very useful. However I wonder if this will be usable to debug an application that is registered as a service or in COM+? This is what my main role involves and there does not seem to be any good memory tools for me to be able to run simulations on the code I am writing and to get any usable debugging information.

    Regards,

  • I've tried to get the Memory Usage tool working on a console app and can't, so I'm wondering if it's implemented yet in CTP 2?  I need clarification.  In Visual Studio Professional 14 CTP 2, can you actually use the Memory Usage tool (to observe heap allocations) on a native desktop app such as a simple "hello world" console app? (I'm assuming windows and console subsystems are treated equivalently as "desktop apps" in that they aren't windows store or windows phone apps.)  I see the note that it only applies to x64 (not x86 yet), but there are claims that it works on "native allocations" and I don't know if that also applies to a native desktop app or just a native windows store apps.

Page 1 of 2 (17 items) 12