13

There are several ways that developers can get caught out by unintentional resource leaks in .NET. I thought it would be useful to gather them in one place.

Please add yours with one answer per item, so the best get voted up :)

4

17 Answers 17

8

Failure to remove Event Handlers.

Registering for an event should be paired with un-registering:

   this.myObject.MyEvent += new EventHandler(myObject_MyEvent);
   this.myObject.MyEvent -= new EventHandler(myObject_MyEvent);

There is an example of a system where this happened on CodeProject.

7

P/Invoking to unmanaged code, and not cleaning them up, or not implementing IDisposable to get them cleaned up.

7

Not using Using.

6

Leaving database connections open.

4

Failure to implement Dispose and so not disposing child objects.

1
  • 1
    Or implementing Dispose but not adding a Finalizer that calls Dispose.
    – Matt Ellis
    May 29, 2009 at 18:28
4

WCF client objects do not perform like other IDisposable objects. The client of a WCF service must be aborted if the operation is in a faulted state or else it will keep the connection open. This is usually learned the hard way.

1
  • WCF burnt me with a similar issue recently. I'll post it but it involved calling Dispose on the factory - which does implement IDispose! May 29, 2009 at 17:33
2

Pretty much everything when using the Office APIs. Since they are all COM objects, they must be disposed. You also have to keep a class reference to them if you want to use event handlers, otherwise they lose their reference. In a lot of cases, you even have to manually call the GC in order to clean up the objects

2

Using a WeakReference can lead to the subtle leak where the object held by the WeakReference is cleaned up because it has no strong references but the WeakReference itself is not because you keep a strong reference to it.

You can run into this if you have something like a List or Dictionary of WeakReferences which you never prune. You'll end up leaking WeakReference objects even after the target has been collected.

2

Easy memory leak: make a static field in a class of type List. Add items to List. They'll never get garbage collected, so unless you remember to manually remove your items when you're done with them, the memory is permenantly tied up.

1

Failure to call the 'Close' method on the System.Windows.Window object.

The only way to ensure that all managed resources for a System.Windows.Window object are garbage collected is to call the 'Close()' method on the Window object. Calling Dispose or setting the object reference to null will not destroy the object.

2
  • Can you provide a citation for this? May 30, 2009 at 0:16
  • @John: This was found by a colleague of mine during a resource leak hunt, so I don't have a published reference. Jun 1, 2009 at 13:47
1

If you count managed memory as a "resource" - failing to unhook event handlers is a common source of memory leaks (and various other more serious bugs).

1
  • Managed memory is not a resource, but event subscriptions are.
    – supercat
    Aug 1, 2012 at 23:53
1

Static Lists, Dictionaries and collection based resources that are populated outside of start-up code.

This can happen if you have a Dictionary you use as a global cache instead of a proper LRU based cache.

Anything static requires a lot of extra caution!

0

Impersonation token handles left open.

0

Failure to dispose any Drawing related objects (Graphics, Font, SolidBrush, Pen, etc.) on the .NET Compact Framework. This can cause some serious memory leaks when you don't want (mobile device = limited memory).

0

WPF resource dictionary leaks. Some helpful links:

0

Misconfiguring Spring.NET to create multiple instances of something that should be a singleton.

0

Failed to call Dispose() on Timer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.