メモリ リークという用語の使い方が下手かもしれませんが、それを説明する他の方法がわかりません。ManagementEventWatcher クラスを使用して特定のインスタンスの起動を監視する Windows サービスを作成しました。そのインスタンスを確認すると、それに応じて別のインスタンスを起動します。私はこのサービスを複数のコンピューターでテストすることに成功し、全体的に成功しました.
私が遭遇する問題は、サービスの起動時に平均 2400k を使用することです。ただし、サービスを一晩実行したままにしておくと、朝、タスクマネージャーでプロセスが12000kのプライベートワーキングメモリにあることがわかります。
これは、何かがイベントを処理していないと私に信じさせますが、私は何を見分けることができません。
イベントの到着行と関係があると思います: watcher.EventArrived += new EventArrivedEventHandler ...
これは、add 代入演算子を使用する必要があるためだと思います。この理論をテストしたいのですが、マイナス代入演算子をどこに配置するかを判断するのに苦労しています。プーリングを防ぐために、最初の行の直後に行をクリアしますか? または、私が見逃している明白なクリーンアップ方法を見ている人はいますか? サービスによって呼び出されるコードが添付されています。
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;
}
}