Home  /  Questions  /  Question

113   96.7
Apr 18, 2010

Can you have memory leaks in .NET?

My pair programming buddy and I got into an interesting discussion recently about .NET and memory leaks.

It is my understanding that, although rare, memory leaks do occur in .NET.  I believe that one of the places where this can happen is with non-released event handlers.

His argument was that because .NET is garbage collected, no memory leaks can occur.

So, now I am confused. Can anyone share some light on this?  Ideally with some examples.


380   99.9
Apr 20, 2010
Many people think memory leak can happen in a .NET code only when it uses COM or P/Invoke calls. However, a pure managed application can still cause memory leaks. This is largely constituted by the internal implementation of the .NET framework classes used rather than the developer code itself. I hope you get what I mean.

Here is a quick Google search on Microsoft support site talking about memory leaks in .NET; try it and see for yourself.

Apr 21, 2010
I have personally worked on a memory leak issue in a COM Interop scenario.
The application consumed COM components from .NET and the objects were not getting released.
So, they were eating up valuable RAM.

85   96.4
Apr 22, 2010
Memory leaks in .NET are mostly because not releasing/disposing of critical resources. Examples includes: Database connections, files in a memory stream, network connections, etc. Until and unless you are not releasing these objects; .NET keeps their ROOTS alive and create memory pressure. Though its not a pure memory leak.

In .NET Framework 1.1 or 1.0 there are chances of Memory Leaks specially if you are using ASCX controls in ASP.NET which was confirmed by Microsoft later on.

But, in general, the chances of running into memory leaks with .NET are minimal if you are not using COM+ and P/Invoke.
In case you are playing with VC++.NET there are chances to occur memory leak (this is called the GC Hole).


50   50
Sep 08, 2011
Short answer: I does LEAK.

... not just for P/Invoke things but also managed classes. For example, if you have a Dataset and you do not dispose it, you'll have a leak :)

Also, handle leaks hurt a lot therefore I suggest you to use "using {...}" whereever possible.

Just my 2c.