I have a Windows service that periodically check database for new records and processes each record in a new thread (it may take up to 10 minutes). When the service is idle right after it's started it takes like 4 MB of RAM. When it starts processing the first record it goes up to 70+ MB and stays there even when the thread is finished (I guess that's OK, because this memory can soon be needed again). Then on the next request it goes from 70 to about 100 MB and also stays there after the thread has completed. So my question is can there be a memory leak in something like this:
public partial class MyWinService : ServiceBase
{
DBService service;
IEnumerable<long> unfinishedRequests;
List<long> activeRequests;
protected override void OnStart(string[] args)
{
System.Timers.Timer timer1 = new System.Timers.Timer(60000);
timer1.Elapsed += Timer_Tick;
timer1.Start();
service = new DBService();
activeRequests = new List<long>();
}
private void Timer_Tick(object sender, System.Timers.ElapsedEventArgs e)
{
unfinishedRequests = service.GetUnfinishedRequests();
foreach (var req in unfinishedRequests)
{
new Thread(delegate() { ProcessRequest(req); }).Start();
}
}
private void ProcessRequest(long requestID)
{
activeRequests.Add(requestID);
// Lots of webservice calls, XML->XSLT->HTML->PDF, byte arrays, database INSERTs & UPDATEs etc.
activeRequests.Remove(requestID);
}
}
Shouldn't all objects created in ProcessRequest() method be destroyed after the thread finishes? And if the program already has 100 MB of memory after the first thread why should it ask for more (in my tests the input data were identical so both threads should use the same amount of memory, I think).