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 :)

Thomas Bratt
  • 48,038
  • 36
  • 121
  • 139

17 Answers17

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.

Thomas Bratt
  • 48,038
  • 36
  • 121
  • 139
7

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

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
7

Not using Using.

Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
Even Mien
  • 44,393
  • 43
  • 115
  • 119
6

Leaving database connections open.

Andy White
  • 86,444
  • 48
  • 176
  • 211
4

Failure to implement Dispose and so not disposing child objects.

Thomas Bratt
  • 48,038
  • 36
  • 121
  • 139
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.

Lance Harper
  • 2,216
  • 14
  • 11
  • WCF burnt me with a similar issue recently. I'll post it but it involved calling Dispose on the factory - which does implement IDispose! – Thomas Bratt May 29 '09 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

Jacob Adams
  • 3,944
  • 3
  • 26
  • 42
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.

Matt Ellis
  • 1,051
  • 1
  • 9
  • 8
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.

Ari Roth
  • 5,392
  • 2
  • 31
  • 46
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.

Thomas Bratt
  • 48,038
  • 36
  • 121
  • 139
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).

Jim Arnold
  • 2,238
  • 14
  • 18
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!

Sam Saffron
  • 128,308
  • 78
  • 326
  • 506
0

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

Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
0

Failed to call Dispose() on Timer

Yuan
  • 2,690
  • 4
  • 26
  • 38
0

Impersonation token handles left open.

Otávio Décio
  • 73,752
  • 17
  • 161
  • 228
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).

Tom van Enckevort
  • 4,170
  • 1
  • 25
  • 40