Note to Self: Using WinDbg to See Memory Usage

Thursday, July 29, 2010 / Posted by Luke Puplett /

How to list the memory being used in a .NET application, by type. This can be useful in finding memory usage problems (not always leaks in the strict sense). It's really pretty awful going back to commands and seeing pages of shitty numbers without comma digit grouping, but Microsoft seem not to care to produce a visual debugger.

Download the SDK for the version of Windows and the .NET Framework you have and install it.

Run your app.

Run WinDbg from the Start menu (search for WinDbg).

Start Task Manager and right-click the process and create a dump file. You can also attach to the process from WinDbg.

Drag and drop the .dmp file into the WinDbg shell.

Load the SOS.dll helper thingy using either one of the following commands in WinDbg. The first one instructs the debugger to load the DLL from the same folder as the mscorwks DLL was loaded from. The second is specific, check which version of the Framework your app runs under (I assume 4.0 for my app and forgot I hadn't upgraded it from 3.5).

.loadby sos mscorwks
.load c:\Windows\Microsoft.NET\Framework\v4.0.30319\SOS.dll

Successful commands don't always give any feedback - no news is good news.

All going well, you should now be able to issue some commands to WinDbg and start analysing the app. Enter the following command:

!dumpheap –stat

If you get "Failed to find runtime DLL" (clr.dll), 0x80004005 then it can be because the wrong SOS.dll was loaded, run the command .unload on its own and it should unload it and you can try loading the right version again.

Otherwise a whole heap of stuff will spew out. Heap. See what I did there? Ordered by total bytes consumed, column 3, the biggest hoarder of memory should be last in the list. System.String usually.

Here are a bunch of links useful to this subject.

Windows SDK Home

!dumpheap -stat explained

Memory Leak Hunting

Tracking Memory Leaks - Rico Mariani

Memory Usage Auditing for .NET Applications

Investigating Memory Issues

WinDbg Cheat Sheet

Labels: ,


Post a Comment