Sunday, July 03, 2011

Debugging managed code memory leak with memory dump using windbg

Debugging memory leak is a must-have skill for most server side app developers especially when you move to cloud.  Usually you don't have luxury to attach debugger to the running process on the server. Even if you have the luxury, it might not be as easy as you think. A good solution to this problem which many developers are using is getting memory dump for the process in which you suspect there is memory leak and then debugging it with windbg.

In summary, the below are reasons for using windbg to debug managed code memory leak with memory dump.
 1. It is needed for server side applications and especially cloud applications
 2. Creating memory dump is easy and you can analyze it offline as long as you want
 3. Windbg is powerful, relatively lightweight and free!
 
This task is not as hard as it sounds like. I just simply used the below 9 steps to debug one possible memory leak at work a while back.
 1. .symfix  and .reload -f
 2. .loadby sos mscorwks!
 3. !VerifyHeap
 4. !EEHeap
 5. !dumpheap -stat
 6. !dumpheap -type
 7. !do
 8. !threads
 9. kb
 
Let's see how I debug the memory leak easily one step by one step.  I will give brief description for each step.

 1. .symfix  and .reload -f
 The idea here is finding correct symbols. If .symfix doesn't work, you will need to set the symbol path manually from the menu or use .sympath. This is important and make sure to do it right. Debugging  without symbols is harder and very inconvenient.
 
 2. .loadby sos mscorwks
 sos is the debugger extension for managed coding debugging. Type !help to see what command are available and how to use them. The help is really useful and do read it.  The commands are case insensitive and there are also shortcuts for some commands.

Read more: Paul Lou's blog
QR: debugging-managed-code-memory-leak-with-memory-dump-using-windbg.aspx

Posted via email from Jasper-Net