我有一个Windows服务,它定期检查新记录的数据库并处理新线程中的每条记录(可能需要长达10分钟)。当服务在启动后立即处于空闲状态时,需要4 MB的RAM。当它开始处理第一条记录时,它会上升到70+ MB,并且即使线程完成时也会停留在那里(我猜没关系,因为这个内存很快会再次需要使用)。然后在下一个请求中,它从70到大约100 MB,并且在线程完成后也停留在那里。 所以我的问题是哪有的东西内存泄漏这样的:c#Windows服务中的内存泄漏?
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);
}
}
不应该在的ProcessRequest创建的所有对象()方法的线程结束后销毁?如果程序在第一个线程之后已经有100 MB的内存,为什么它要求更多(在我的测试中,输入数据是相同的,所以两个线程应该使用相同数量的内存,我认为)。
+1 Nice anz,需要更多的重新实施在我的应用程序 – Backtrack
非常intresting,得到了同样的问题 – jonathana