Tip for Finding Memory Leaks in C#

There are many tools and techniques for finding memory leaks in managed code. Some involve sophisticated tools like the CLR Profiler. While I have found such techniques useful for bugs in the “wild”, I have found a simpler technique that smokes out these errors during development. It’s really two tips.

Use FxCop or the code analysis tool in VS 2005 Pro. It tells you so much about your code and finds objects that you forgot to dispose. Use it every day.

Put break points on your finalizers. If you write classes of any complexity, you likely have implemented the dispose pattern for these objects. If not, you likely should. Add asserts to the finalizers or use break points and run your program as normal. Did you hit any finalizers (you see this mostly during program exit)? If so, these are good candidates for further examination. It’s a good bet that if your finalizers are executing then your objects are living longer than they should.

Other interesting articles about Dispose Patterns

← newer older →
.Net, Technology, Life, Whatever

Recent Posts

VSColorOutput for VS 2017 Released
Quick ReSharper tip for inline templates
NoSQL vs. NewSQL: What's the Difference?
Exploring Mithril, A JavaScript Framework for Building Brilliant Applications
Tweetz 1.1
Tweetz Desktop 1.0
Tweetz 0.11 Released
More... (1073)

Donate with PayPal