I have been developing a quite large application, and I uploaded it to my server some days ago. Now I have found out it has several memory leaks – Uh oh.
My server is running Windows Server 2008 on 1GB ram. When I have 0 people online, only 550-600mb is used. When one people comes online the memory starts skyrocketing, and if 3-4 people are online all 1GB ram is used.
The application is made in ASP.NET with AJAX. It has many updatepanels which runs every second and quite a lot of javascript. It uses 5-7 sessions at all times. I use LINQ to SQL as database communication.
I tried perfmon.exe on my server, and I found:
- Gen 0 collections goes from 0% to
100% within minutes - Gen 1 collections
goes from 0% to 50% within 5 minutes - Gen 2 is very close to 0% at all
times - Total heap bytes goes up to
100% very fast
I also ran an analysis of my program with Visual Studio. 8% Of my total runtime is done in .ToList() methods, which properly is caused by LINQ to SQL.
My theories….
(1) Linq to SQL dataContext
This might be a crazy thing to do, but: In my data access layer I have a load of methods:
- AddSomethingToDatabase();
- AddSomethingElseToDatabase();
- DeleteSomethingFromDatabase();
Each of these has the following initialization:
GameDataContext db = new GameDataContext();
Which means the above statement runes nearly every second or more.
(2) No objects implement IDisposable
I have to be honest: I have never worked with IDisposable. As far as I have read, this might be a problem.
Also, if this is the leak, which classes should implement it? I do not have any I/O work or others, only the DataContext.
(3) Loads of UpdatePanels and jQuery
I have some fear loads of updatepanels can give problems with performance, but I do not know how to check it.
So my question is: Any ideas on what the memory leak could be? Any ideas on how to find the memory leak? And any ideas on how to solve it?
I would love to hear from someone who has experience with the situation above!
Thanks,
Lars
You absolutely must ensure that
IDisposableobjects getDisposecalled when you are done with them. The simplest way to do this is to use using:If you still have problems after doing this throughout, then profiling is needed, but fix this first since it’s a no-brainer.
Your own objects usually only need to implement
IDisposableif they encapsulate unmanaged resources for which you wish to guarantee deterministic release back to the OS, so that those resources – file handles, sockets, and so on – are not sitting around waiting for GC for an interval of time you cannot rely on.I don’t have an answer for your question 3), sorry.