How to debug memory leaks in Windows Store apps?
Asked Answered
D

2

8

So I have a .NET Windows Store app that is leaking memory. What can I do about it? The profiler tools I used for desktop apps from jetBrains or Red-Gate/ANTS don't support Metro Apps (or do they now?)

Dais answered 5/12, 2012 at 19:0 Comment(0)
D
12

For the simplest approach - skip to the bottom to read about the description of doing that with Visual Studio 2013.


Now there might be some new tools - perhaps something in the updated Visual Studio and I would love to find about these, but I tried WinDbg before with some success. Here are my old notes on how to do that:

1. Create dump file from process manager
2. Run WinDbg (X64)
3. File/Open Crash Dump… (Crtl+D)
4. Run following:

lm
.load C:\windows\Microsoft.NET\Framework64\v4.0.30319\sos.dll
.sympath SRV*c:\localsymbols*http://msdl.microsoft.com/download/symbols
.symfix
.reload
!dumpheap -stat

Note that if your process if x86, especially if you are running on a x64 version of Windows - you will need to use an x86 version of the debugger (WinDbg ships both versions) to save the dump. SOS, which is a managed memory debugging extension for WinDbg does not support debugging x64 bit dumps of x86 bit processes. You will then also need to update the sos path respectively, so it looks like this:

.load C:\windows\Microsoft.NET\Framework\v4.0.30319\sos.dll

Possibly not all of these commands are necessary, but this is what worked for me.

Now you can find the type of the object that seems to exist in too many instances

!DumpHeap -type TypeName

where type name is just the name of the type - no fully qualified namespace necessary.

Now you can check what keeps this object in memory:

!GCRoot Object_Address

Live debugging didn't work for me since the app seems to get suspended when you attach a debugger. I think I saw an option somewhere to make the app stay in memory, but I forgot where, but for memory profiling - looking at a static dump file might be enough.


You can download WinDbg as part of Windows SDK or as a standalone download of "Debugging Tools for Windows" from here.

To create a dump file - go to Task Manager, right click a proces and select "Create dump file".


Some more links:

http://blogs.microsoft.co.il/blogs/sasha/archive/2012/10/15/diagnosing-memory-leaks-in-managed-windows-store-apps.aspx

http://blogs.msdn.com/b/delay/archive/2009/03/11/where-s-your-leak-at-using-windbg-sos-and-gcroot-to-diagnose-a-net-memory-leak.aspx

http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/f3a3faa3-f1b3-4348-944c-43f11c339423

http://msdn.microsoft.com/en-us/library/bb190764.aspx

http://blogs.msdn.com/b/dougste/archive/2009/02/18/failed-to-load-data-access-dll-0x80004005-or-what-is-mscordacwks-dll.aspx


*EDIT

According to .NET Memory Allocation Profiling with Visual Studio 2012 by Stephen Toub - PerfView tool supports analyzing leaks in .NET Windows Store apps. Check an article and video walkthrough with Vance Morrison here.


*EDIT 2

Visual Studio 2013 Preview adds a new option to analyze managed memory heaps from dump files. To do it - simply pause your app in the Visual Studio debugger, save your current dump through Debug/Save Dump As, then resume execution and use your app until your suspected leak happens and do another dump. Then go to File/Open/File and open the second dump file. To the right of the Dump Summary in the "Actions" panel you'll see a "Debug Managed Memory" action. Select that and then in "Select Baseline" select your first dump file. You will see a list of objects on the managed heap, grouped by type, with count diffs. Note that you would typically first look at the objects with low, non-zero count differences to track a single leak source. You can drill into the list of objects and see what keeps them in memory by expanding the tree in the Reference Graph view.

Dais answered 5/12, 2012 at 19:0 Comment(3)
More fresher links: blogs.microsoft.co.il/blogs/sasha/archive/2012/10/15/…Dais
Also it seems like etl (Event Trace Log) files that you can produce using the wpr (Windows Performance Recorder) command and read with wpa (Windows Performance Analyzer) can be helpful in finding leaks, though I haven't played with it yet. Check these links that might contain some helpful information idigitalhouse.com/Blog/?p=155 blogs.msdn.com/b/ntdebugging/archive/2012/11/30/…Dais
People should note that the "Debug Managed Memory" option is only available in Visual Studio 2013 Ultimate and the dump is created from a .NET 4.5 process. More info here: blogs.msdn.com/b/visualstudioalm/archive/2013/06/20/…Barnacle
A
1

Telerik JustTrace supports Windows Store Apps: http://www.telerik.com/products/memory-performance-profiler/justtrace-features.aspx

Anaesthesia answered 14/2, 2013 at 7:25 Comment(4)
Thanks! I'll have to try that!Dais
They tweeted me saying it only supports tracing the managed side of things, but that should be enough for many people.Dais
The question was tagged .net, so I forgot to mention that it won't work (in the latest version) with WinJS. I wouldn't be surprised to see it at some point.Anaesthesia
You must disclose your affiliation in your answers. stackoverflow.com/help/promotionTiebout

© 2022 - 2024 — McMap. All rights reserved.