Click to See Complete Forum and Search --> : Memory Leaks


jeetendra
November 16th, 2004, 07:39 AM
hi,

I wrote an application in c# using visual studio .Net 2003, 7.1.3088 version.
I ran this application over night on win 2k professional, service pack 4 . Next day i discovered that the application stopped with an exception saying OutOfMemoryException.
I saw the memory usage and it was 75MB (Application started with 10 MB memory usage).
why is this memory leak in .Net and how can it be solved.

thanks in advance.

Ilantir
November 16th, 2004, 10:19 AM
If you could post some code from your project, that we could examine. Usually a memory leak arises when you keep on creating new instances of classes while not disposing of the old ones. That's one thing I can say...

Mr. Tomaszek
November 16th, 2004, 11:14 AM
Maybe it's not the memory leak. Mayby your application is memory consuming one? If you are sure that it is memory leak under .NET, check if garbage collecting is turn off (unsafe - statements may indicate this). Do you have installed .NET Framework 1.1 SP1?

darwen
November 17th, 2004, 03:15 AM
OutOfMemory exceptions aren't just caused with the app running out of memory - in fact it's extremely unlikely that your program ran out of memory using only 75Mb of memory.

And forget memory leaks. You're barking up the wrong tree there with that assumption. If you're not doing any interop to native then it's extremely unlikely.

The exception can be thrown if you've tried to allocate an array which is massive.

Without seeing the code, or finding out which line caused the exception I can't really help any further. It could be just about anything !

Darwen.

jeetendra
November 17th, 2004, 08:39 AM
Thanks guys, I guess i found the problem.
I was allocating classes and buffering them in ArrayList collection.
Another thread was using the data in the ArrayList and calling a clear() on it, but this doesn't seem to be reducing the memory usage (Don't know the reason).

I then replaced ArrayList with Queue, Now it seems to be working fine.

void f()
{
//Call stored procedure
ArrayList pResult = m_DMService.ExecuteProcedure("InsertPOSTrans", pParams);
pResult = null;
}
In the above code after using pResult, i had to assing it to null (absolutely not needed for local variables) only then garbage collection was happening .

darwen
November 17th, 2004, 10:57 AM
ArrayLists don't reset their memory when you run a Clear() - this is to make sequential adding & removing items as fast as possible.

I have also heard that when an array list runs out of members it DOUBLEs its allocated size - which could explain why you were running out of memory.

If this is the case, then it's not a particularly nice implementation.

Darwen.