我可能没有很好地使用“内存泄漏”一词,但是我不确定是否有其他描述方式。我编写了一个Windows服务,该服务使用ManagementEventWatcher类来监视特定实例的启动。一旦看到该实例,它将启动另一个实例。我已经成功地在多台计算机上成功测试了该服务,并获得了成功。
我遇到的问题是,服务启动时平均使用2400k。但是,我发现这些服务在一夜之间仍处于运行状态,该进程位于任务管理器中的12,000k专用工作内存中。
[这使我相信,某些事物并不能解决事件,但是我无法辨别是什么。
我相信这可能与事件到达线有关:watcher.EventArrived + =新的EventArrivedEventHandler ...
我相信这是因为它需要使用添加赋值运算符。我想测试一下这个理论,但是在辨别减法赋值运算符的位置时遇到了麻烦。第一行之后会直接清除该行以防止池化吗?还是有人看到我缺少的任何明显的明显清除方法?附带的是服务调用的代码:
class Monitor
{
//Pulls process watching and launch path as well as args from xml config file.
private static string procWatch = ConfigurationManager.AppSettings["watchFor"];
private static string filePath = ConfigurationManager.AppSettings["filePath"];
private static string filePath2 = ConfigurationManager.AppSettings["filePath2"];
public static ManagementEventWatcher watchforProcess()
{
//Creates ManagementEventWatcher Object to return to the service.
ManagementScope scope = new ManagementScope(@"\\.\root\CIMV2");
WqlEventQuery query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 1), "TargetInstance isa \"Win32_Process\"");
ManagementEventWatcher watcher = new ManagementEventWatcher(scope, query);
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
return watcher;
}
public static void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
//Event handler that launches a process if it's name matches proc watch
string instanceName = ((ManagementBaseObject)e.NewEvent["TargetInstance"])["Name"].ToString();
if (instanceName.ToLower() == procWatch)
{
EventLog.WriteEntry("KaceWatcher", "Kace process found.");
Process.Start(filePath, filePath2);
}
//Should I place a null here to clear up each instanceName.
}
public static void finalize()
{
//Used for service stop
procWatch = null;
filePath = null;
filePath2 = null;
}
}
[在使用ManagementEventWatcher之后,我们刚刚遇到了相同的问题,除了更极端的情况之外,由于我们经常触发WMI事件,因此内存使用量飙升至数GB范围。