Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Resource Leak Gotchas

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

like image 513
Thomas Bratt Avatar asked May 29 '09 16:05

Thomas Bratt


2 Answers

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.

like image 191
Ari Roth Avatar answered Nov 06 '22 22:11

Ari Roth


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

like image 38
Ian Boyd Avatar answered Nov 06 '22 21:11

Ian Boyd