Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / September 2003

Tip: Looking for answers? Try searching our database.

.NET Memory Leaks in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Valerie Hough - 30 Sep 2003 03:06 GMT
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
Stefan Simek - 30 Sep 2003 06:48 GMT
Hi

> 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
Bob Powell [MVP] - 30 Sep 2003 15:30 GMT
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

> 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
[quoted text clipped - 19 lines]
> Thank you in advance.
> Chris Hough

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.