我正在从PerformanceCounters读取'%CPU'和'可用内存'的服务器上工作。 它在我的开发机器上运行良好。 但是在实际的服务器上,从这两个PerformanceCounter读取真的很慢。 至less在前两个读取操作。
执行以下代码可能需要4-6分钟时间:
Stopwatch watch = new Stopwatch(); Log.Instance.Debug("Going to initialize Diagnostic's PerformanceCounters"); watch.Start(); m_CPUPerformanceCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true); m_MemoryPerformanceCounter = new PerformanceCounter("Memory", "Available MBytes", true); m_CPUPerformanceCounter.NextValue(); m_MemoryPerformanceCounter.NextValue(); //Ensure an updated value... System.Threading.Thread.Sleep(1000); m_CPUPerformanceCounter.NextValue(); m_MemoryPerformanceCounter.NextValue(); watch.Stop(); Log.Instance.Debug("Finished initializing Diagnosticss PerformanceCounters. Time elapsed: {0}", watch.Elapsed);
当我在开发机器上运行这个代码时,它会在不到2秒的时间内完成(有时甚至更less)。 但是在我们产品的客户端应该使用的实际服务器上,这将需要很长时间。 见下文:
Finished initializing Diagnosticss PerformanceCounters. Time elapsed: 00:03:59.6706860
这些读取操作(以及后来的“读取”操作)执行起来非常重要。 甚至在一开始。
我的开发机器是Windows 7,64位,8GB RAM。
客户端服务器是Windows Server 2008 R2 Enterprise,64位,4 GB RAM。
我Google(和Binged),但无法find任何答案。 为什么发生这种情况? 我该如何解决?
该问题很可能是由被监视系统上安装的附加软件提供的计数器。
在这种情况下,您应该为在系统注册表中注册的所有性能计数器执行性能计数器初始化扫描,这可以使用ProcMon完成。