C# Memory Leaks

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 this 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 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
 
M

Michael Culley

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

Pens, brushes and fonts should be disposed of asap but I believe you can rule these out because they will not use much memory. I
can't remember exactly what happens when you run out of say a pen, it will either throw an exception or just draw black. Either way,
running out of pens, brushes or fonts will leave you with plenty of system memory.
I would be particularly interested in any objects that should be disposed of
right after they are used (pens, fonts, arrays, brushes, etc)

You need to dispose/close connections and data readers etc but I wouldn't think this would be the problem either because they should
dispose of themselves sooner or later (sooner if your system in running out of memory).

Do you use images at all? I have a memory leak in one of my apps when importing a large number of pictures into the database. I'm
not sure what causes it as I haven't got around to looking at it yet.
 
A

Alexander Arlievsky

Hi,
First - destroy (dispose) all allocated pens, brushes, icons etc.
If X implements IDisposable, and you got it through "new" operator - call
Dispose when you are going to abandon referenced object.
Second - more subtile problem - is dangling reference. If your program often
allocates new data structures (i.e. as response to client request), and you
store somewhere reference to this data structures (and you don't need them
after end of the request handling) they won't be freed by GC.
In C++ it would lead to exception when you access this reference, or stay
unnoticed if noone touch them.
In .Net it "eats" resources - you have or "nullify" all references, or use
WeakReference instead all but one reference if "nullifying" is too
complicated.

--
==============================
Alexander Arlievsky
(e-mail address removed)
"The best tools for debugging are brains"
==============================

Michael Culley said:
Pens, brushes and fonts should be disposed of asap but I believe you can
rule these out because they will not use much memory. I
can't remember exactly what happens when you run out of say a pen, it will
either throw an exception or just draw black. Either way,
running out of pens, brushes or fonts will leave you with plenty of system memory.

You need to dispose/close connections and data readers etc but I wouldn't
think this would be the problem either because they should
dispose of themselves sooner or later (sooner if your system in running out of memory).

Do you use images at all? I have a memory leak in one of my apps when
importing a large number of pictures into the database. I'm
 
V

Valerie Hough

Yes, I do use images. The software puts up a background image for the main
window, and it is a very large image indeed (full screen picture). Also, the
user comes back to this window over and over in the course of the day,
causing it to be loaded each time. I will certainly look into this. Thanks
alot!
 
M

Michael Culley

Valerie Hough said:
Yes, I do use images. The software puts up a background image for the main
window, and it is a very large image indeed (full screen picture). Also, the
user comes back to this window over and over in the course of the day,
causing it to be loaded each time. I will certainly look into this. Thanks
alot!

I'll have a look into the memory leak in my app (it's about time I did) and post the results.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top