1
我正在从PerformanceCounters读取'%CPU'和'可用内存'的服务器上工作。 它适用于我的开发机器。但在实际的服务器上,从这两个PerformanceCounter读取真的很慢。至少在前两次读取操作中。
它可能需要长达4-6分钟,在执行以下代码:为什么对PerformanceCounter的调用很慢?
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秒(有时甚至更低)。但是在我们的产品的客户应该使用的实际服务器上,这将需要很长时间。请看下图:
Finished initializing Diagnosticss PerformanceCounters. Time elapsed: 00:03:59.6706860
,这些读操作(以及后来的“读”操作)是很重要的将执行非常快。甚至在一开始。
我的开发机器是Windows 7,64位,8GB RAM。
客户端服务器是Windows Server 2008 R2 Enterprise,64位,4 GB RAM。
我谷歌搜索(和Binged)它,但无法找到任何答案。 这是怎么回事?我该如何解决它?
我不知道问题是什么,但是:“//确保更新的值...... System.Threading.Thread.Sleep(1000);'肯定不能确保值得到更新。 – Tarec
你为什么要将计数器指定为只读模式的访问?作为测试尝试新的PerformanceCounter(“处理器”,“处理器时间百分比”,“_Total”); - 你确定你没有隐藏异常吗?例如UnauthorizedAccessException –
@Tarec你是对的。但如果需要的话,操作系统有足够的时间来更新这些PerformanceCounters。 – m1o2