Memory leak

M

Mike

..NET 1.1

Hi.

My C# .NET application has a memory leak - if I leave it running for hours
then the system ends up using 500MB+ of virtual memory, which is released
immediately when I close the app. Now, I thought this would be impossible
with automatic garbage collection, but it seems this is not the case.

The application is threaded and all work is done within spawned threads. No
thread runs for the lifetime of the app - they are started and ended as
required, so I thought that even if I did not explicitly release an object's
resources, GC would at some point do this for me. As far as I can see
though, I AM releasing all resources.

Can someone give any advice on:
i) how this situation could occur in the first place,
ii) how to track down where the memory leak is occuring.

Any help is appreciated.

Thanks,

Mike
 
L

Lloyd Dupont

could it be static collection ?
I mean something like that:

class MyClass
{
static ArrayList someInfo = new ArrayLits();
}
and you keep addiung stuff in it ?


could it be event handler ?

something like
class AGlobalOrPersistentInstance
{
public event MyEvent;
}
and you register plenty of handler which get never deregistered ?


anyway and instance tracking tool would be nice, I'm curious to heard about
any ....
 
M

Mike

Hi Mike,
As Lloyd has pointed out you might have some subtle areas where you
are keeping references around to objects.

One tool to start with in tracking down the "who what where" is the
CLR profiler. There is some documentation, and a link to download the
tool here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenethowto13.asp

Thank you both for your advice.

I found this excellent tool for analysing the heap:
http://www.scitech.se/memprofiler/
which eventually lead me to a block of code with a lock { } around it.
Removing the lock { } (it wasn't necessary for my application) solved the
memory leak, but this leaves me even more confused than before as to the
cause of the leak - any ideas?

Mike
 
S

Scott Allen

That's interesting. Perhaps you could post some code that demonstrates
the problem?

--s
 
A

AlexS

Mike,

this is standard situation with applications, which create big quantities of
objects during run-time. Problem is easily appearing when speed of object
creation is higher than speed with which GC collects free references. Simple
example of code, which is fine in samples and "bad" in real application is
....
for (...) {
using (MyObject o = new MyObject()) {
... some work
}
}
....

It works in most cases, especially in simple benchmarking tests (as usual),
but when something causes big number of calls to method - for example
continuous repaints or some other external action, heap might be easily
flooded with MyObjects.

There are also other types of code, which are also "harmful" for memory
consumption, including simple string str = "..." + myString.

Remember that GC is background thread, so, if you don't give it enough time,
it wouldn't be able to collect all free references. Because GC is not
deterministic the only protection from such situations is to use memory
profiling and analysis of dynamics of allocations and relocations.

More details on MSDN:
http://msdn.microsoft.com/architecture/default.aspx?pull=/library/en-us/dnpag/html/scalenet.asp

HTH
Alex
 

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

Memory Leak 23
Memory leak detection 1
Memory leak using pInvoke 3
dataGridView Memory Leak 4
Clean up of memory leak in unmanaged code 7
GetHicon memory leak 3
Managed WMI memory problem 7
Memory Leak Detection 4

Top