Monday, February 08, 2010

Debugging Native memory leaks with Debug Diag 1.1

I often get questions about debugging native memory leaks. In other words, cases where you have high memory usage in your application but you can see that .net memory usage is not all that high.

Recently I was helping out on such a case and this post details both generally how you go about troubleshooting these issues as well as what troubleshooting steps we went through in this particular case.

Essentially you would go through these steps to troubleshoot a native memory leak:

   1. Find out if your memory usage / leak is mostly .net memory or native memory
   2. If it is native memory that is “leaking”, use a tool like debug diag 1.1. to track allocations and deallocations
   3. Look at the main allocators and the stacks associated with the biggest chunks of allocations to determine who is making the allocations and why the memory is not released.

1. Find out if your memory usage / leak is mostly .net memory or native memory

There are various ways to find out if your memory usage is mostly .net or native.

Using perfmon to determine where your memory is going

The best way is probably to use performance monitor and collect a log over the lifetime of the process that is leaking with at least the following counters for the specific process (note that _total or _global counters are not very helpful here)

   * .NET CLR Memory/#Bytes in all heaps
   * Process/Private Bytes
   * Process/Virtual Bytes

Read more: If broken it is, fix it you should

Posted via email from jasper22's posterous