Memory leaks can cause strange and unexpected behaviors in your applications.  Of course in this day of managed code we don’t see it as often as we used to and many of us are getting rusty in our skill to eliminate these problems.

Recently I ended up with a misbehaving Outlook 2007 add-in which resulted in time spent with Microsoft support.  The first thing that came out of that was a way to detect if an Outlook 2007 is experiencing memory leaks.  Below is a command line command that seems to report back which email items were involved in memory leaks if any occur.

Outlook /reportleakedrenitems

So how do you handle the problem?  Since the unmanged code in this case are COM interop object  you use Marshal.ReleaseComObject from the the System.Runtime.InteropServices namespaces.  Any place where you are explicitly creating an object out of the from the Microsoft.Office.Interop.Outlook namespace you need to explicitly release it using this method.  If all goes well this will exercise the ghost from your system.

Here are a couple of links related to the topic for further reading.

• ReleaseComObject - How it works

• OOM.NET: Part 1 - Introduction and Why Events Stop Firing...

• OOM.NET: Part 2 - Outlook Item Leaks