0

我想知道 Windows 7 何时检测到我的程序使用了太多内存。所以我想处理这个。我如何订阅此事件(在应用程序关闭之前)。

来自 Windows 日志的一些信息:

Windows 成功诊断出虚拟内存不足的情况。以下程序消耗的虚拟内存最多。事件 ID:2004 关键字:与系统提交限制(虚拟内存)耗尽相关的事件。

在 Windows 中检测低虚拟内存条件

4

1 回答 1

2
System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog("System", ".", "Resource-Exhaustion-Detector");
eventLog.EnableRaisingEvents = true;
eventLog.EntryWritten += eventLog_EntryWritten;

static void eventLog_EntryWritten(object sender, System.Diagnostics.EntryWrittenEventArgs e)
{
   if (e.Entry.Message.Contains(Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName)))
   {
      Logger.Error("Our application consumed too much memory `{0}`. So we stopping work right now to prevent reboot OS.", new object[] {e.Entry.Message},MethodBase.GetCurrentMethod());
      GC.Collect();
      //do smth                
   }
}
于 2013-09-17T18:27:15.390 回答