Back to list
Views:   22.5K
Replies:  4

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.

Bogusław Dobrogost, Apr 18, 2010
Reply 1
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. 
Murat Eraydin, Sep 08, 2011
Reply 2
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).

Kaushal Patel, Apr 22, 2010
Awesome! Really looking forward to this new release.
Jun 05, 2010
Reply 3
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.
Sreenivas Manyam Rajaram, Apr 21, 2010
I forgot to mention that the Repository Pattern will be included in the upcoming VS 2010 / .NET 4 release.
Apr 01, 2010
Reply 4
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.
Siva M, Apr 20, 2010
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.