Mark Levison

Musings on No Touch Deployment, .NET development and photography

<November 2008>
SuMoTuWeThFrSa
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456


Navigation

Other

Blogroll

Subscriptions

Post Categories



Thursday, September 30, 2004 - Posts

Memory Leaks? Leaking IDisposable objects? MemProfiler is your friend.

For the past few days I've been tracking down some nasty memory leaks in our product and SciTech's MemProfiler has saved my bacon. It turns out that we had a number of short lived objects (foo) subscribing to events on long lived objects (bar) and never releasing those events. As a result the objects the foo objects lasted forever.

The documentation is a bit weak so for the benefit of first time users, here is how I use it:

  1. Start your application and do "some work"
  2. Take a snapshot
  3. Do some more work and take another snapshot
  4. Now the fun begins: Go to the classes tab (left most)
  5. Sort by the delta either on number of instances or bytes and look for unexpected growth
  6. Double Click on a suspicious class (MemProfiler switches to the class details tab)
  7. Double Click on an interesting instance (MemProfiler switches to the instance tab)
  8. Laid out in front of you are:
      list of Objects that reference you
      paths back to the root for each instance
      the allocation stack for this object
  9. Find the reference to your object that is keeping it alive and fix the code
  10. Rerun the process to verify
  11. Jump for joy that this problem was so damn simple to fix
Now I just have to figure out how to write unit tests that will detect these leaks. Nothing's perfect and I have few feature requests for SciTech:
  1. Given an instance I would like to be able to display a graph of what it references and what references it. Often the references we've stumbled across are loops and so shouldn't stop the objects from being garbage collected.
  2. In the Classes tab, I would like a field that gives the total number of bytes for this object and all its dependants. MemProfiler tells me our application has allocated a large number of int32's. By itself that's not very useful - knowing what data structures they belong to is.
  3. Work NUnit/MbUnit without crashing - the current version has some problems with reflection. Andreas has promised the next version will fix this.
  4. More information for 'undisposed instances' (IDisposeable objects that weren't disposed before they went out of scope). Specifically where these objects allocated. Otherwise its like tracking down a needle in haystack. We allocate over 200 Bitmaps - most of which we handle well. It was a pain to find the few that SciTech said were leaking.
  5. Better documentation

Best part it only costs $100 US.

posted Thursday, September 30, 2004 10:13 AM by mlevison with 1 Comments




Powered by Dot Net Junkies, by Telligent Systems