views:

287

answers:

4

Suppose I have a WinForms .NET application written in C#. This application allocates large amounts of memory from both the managed and unmanaged heaps. I can (relatively) easily figure out the allocation details (who, when, how much, etc.) of the managed objects using tools like Red Gate ANTS Profiler (which I have not used) or JetBrains dotTrace.

Where I run into trouble is when the managed objects use significant native resources (Image for example, or large COM objects) or when I call out to unmanaged third party libraries with significant memory needs. Thus far I really have to do some detective work to figure out 1) is it the managed or unmanaged code that is the memory hog and 2) who is using so much of the unmanaged heap.

The only technique I have at the moment is to set performance counters in perfmon that compare the managed vs. unmanaged heap sizes, allocations, etc. If the unmanaged heap continues to grow while the managed heap is steady, I know to start looking at the unmanaged libraries and/or .NET objects that consume native resources.

What is the best combination of tools and techniques to help developers with significant amounts of managed and unmanaged code?

UPDATE: To be more clear, I would like to know how to detect memory problems (high usage, leaks) in unmanaged code being called from managed code. Products like dotTrace, I believe, only track managed objects.

+1  A: 

You can use any of the many memory profiling tools compatible with .net applications. Some are as follows:

1).NET Profiler API

2)dotTRACE Profiler

3)Memory Profiler

renegadeMind
A: 

You can get an overview by simply looking at the many .NET memory counters available in Perfmon.

If you want to use a free tool, Debugging Tools for Windows (specifically WinDbg + SoS) can tell you why objects are not being collected as expected.

Brian Rasmussen
A: 

review your code with these tips : Common programming mistakes for .NET developers to avoid?

lsalamon
A: 
Stephen Kellett