Finding Memory leaks

E

Elmo Watson

Is there any way to find any memory leaks within your applications, using
Visual Studio.Net 2005?
 
E

Elmo Watson

If the garbage collector is there to handle it, then, it sounds to me, like
it IS possible - I think I have a .Net component in my app, which has a
memory leak
I need to know how to find this....
 
M

Massimo

If the garbage collector is there to handle it, then, it sounds to me,
like it IS possible

The GC is not there to handle memory leaks: it's there to reclaim memory
which is no longer referenced anywhere in the program; a side effect of this
is you can't have memory leaks, because as soon as you have one, the GC will
reclaim that memory.
- I think I have a .Net component in my app, which has a memory leak
I need to know how to find this....

First of all, you should verify if that's *really* a memory leak, i.e. if
you're losing memory because you lost any track of it. In order to check
this, you should call GC.Collect() and see if your "lost" memory comes back:
if it does, then that actually was a memory leak, but you can handle it by
periodically invoking the GC this way.

If your memory doesn't come back even after calling the GC, then it's not
leaked: it must be referenced somewhere in the program, even if you may not
be aware of it; memory is not considered to be available if there is any
reference to it anywhere (maybe inside some really complex data structure
which made you forget it).


Massimo
 
K

Kevin Spencer

Hi Elmo,

Your instincts are correct. It is certainly possible to have memory leaks in
a .Net application, particularly if that application is a Windows Forms app,
since all Windows Forms components are tied to unmanaged WinAPI components.
This is why all Components and Controls implement the IDisposable interface.

In addition, there are other possible causes of memory leaks. A memory leak
occurs when memory is allocated but not deallocated. While Garbage
Collection exists for the purpose of attenuating memory leaks, it does not
prevent them. Garbage Collection only works when class instances are
completely dereferenced.

Here are a few articles, starting with a Microsoft Support article, that may
help you:

http://support.microsoft.com/kb/318263
http://msdn.microsoft.com/msdnmag/issues/07/01/ManagedLeaks/default.aspx
http://www.codeguru.com/cpp/v-s/debug/memoryissues/article.php/c4411/
http://blogs.msdn.com/ricom/archive/2004/12/10/279612.aspx

There are also commercial apps available that can be used alone or in
conjunction with Visual Studio for this purpose. Just Google for them, if
you want to go that route.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 

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

Top