Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid Memory Leaks? [closed]

What are some tips I can use to avoid memory leaks in my applications? Are there any gotchas or pitfalls that I can look out for?

like image 524
Josh Avatar asked Apr 27 '09 17:04

Josh


3 Answers

Call Dispose on IDisposable objects or use the using clause. That should take care of most of the leaks I can think of.

like image 135
Otávio Décio Avatar answered Oct 27 '22 20:10

Otávio Décio


Watch that you remove any event handlers that you use. In .NET, they are the most common cause of leaked memory.

like image 20
Joshua Avatar answered Oct 27 '22 19:10

Joshua


As mentioned by ocdecio be sure to call Dispose on Idisposable objects, and remember to remove event handlers when you're done with an object. When building classes that works with unmanaged resources, be sure to implement Idisposable, so the user will know that there are critical resources that'll need to be disposed of.

Also, even though garbage collections do quite a bit a work for you, you should get rid of references to objects that you're done with. Else they'll still have a root, and they won't be GC'ed.

like image 36
Kasper Holdum Avatar answered Oct 27 '22 18:10

Kasper Holdum