.NET Memory Leaks in C#

  • Thread starter Thread starter Valerie Hough
  • Start date Start date
V

Valerie Hough

Currently our client runs one particular C# .NET executable, and after a few
hours performance slows to a crawl. It would be very useful for me to be
able to rule in (or out) the possibility that it is a result of memory
leakage.

Can someone point me to an article that discusses how bad programming may
produce memory leaks? The application is particularly list box intensive
(owner drawn, so pens, fonts, and brushes abound - all those things I would
religiously destroy immediately after use in my C++ apps). I never see any
messages in the debugger (a la C++) when the application terminates that
report leaks.

The application also does alot of SQL Server querying - are there disposal
issues there?

I would be particularly interested in any objects that should be disposed of
right after they are used (pens, fonts, arrays, brushes, etc)

I would also be very interested in any tools that will allow me to verify
that a particular .exe (or the DLLs it uses)is/are leaking.

Thank you in advance.
Chris Hough
 
Hi

Valerie Hough said:
Currently our client runs one particular C# .NET executable, and after a few
hours performance slows to a crawl. It would be very useful for me to be
able to rule in (or out) the possibility that it is a result of memory
leakage.

Well, why don't you have a look at the TaskManager? It can show you, at
least approximately, how much memory is being used.
Can someone point me to an article that discusses how bad programming may
produce memory leaks? The application is particularly list box intensive
(owner drawn, so pens, fonts, and brushes abound - all those things I would
religiously destroy immediately after use in my C++ apps). I never see any
messages in the debugger (a la C++) when the application terminates that
report leaks.

And you probably never will. There can be no such thing as a memory leak
under a GC environment, only objects that are referenced and thus cannot be
collected.
The application also does alot of SQL Server querying - are there disposal
issues there?

I would be particularly interested in any objects that should be disposed of
right after they are used (pens, fonts, arrays, brushes, etc)

You should dispose every single object that implements the IDisposable
interface. The recommended way of doing this is via the using() block, i.e.:

using (Pen p = new Pen(...))
{
g.DrawLine(p, ...)
}

The same applies for SQL commands:

using (SqlConnection con = new SqlConnection(...))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("...", con))
{
using (SqlDataReader dr = cmd.ExecuteDataReader())
{
etc...
}
}
}
I would also be very interested in any tools that will allow me to verify
that a particular .exe (or the DLLs it uses)is/are leaking.

Don't know if such things exists, but you can use the .NET Memory Profiler
(http://www.scitech.se/memprofiler) or Microsoft's CLR Profiler to examine
the object allocation.
Thank you in advance.
Chris Hough

Hope this helps,
Stefan
 
If you're using a lot of pens and brushes you should definitely call dispose
after use.

Remember that managed wrappers may have unmanaged resources so these should
be explicitly disposed of.

Although GC will prevent memory leaks over the life of your application, in
some cases objects won't be reclaimed until the application closes.

The GDI+ FAQ has an article on when to dispose of graphical objects.

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm
 
Back
Top