为什么致电PerformanceCounter的速度很慢? [英] Why a call to PerformanceCounter is slow?

查看:178
本文介绍了为什么致电PerformanceCounter的速度很慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从PerformanceCounters读取%CPU"和可用内存"的服务器上工作. 它在我的开发机器上运行良好.但是在实际的服务器上,从这两个PerformanceCounter读取数据确实很慢.至少在前两个读取操作中.
执行以下代码最多可能需要4-6分钟:

I'm working on a server that reads the '% CPU' and 'Available memory' from PerformanceCounters. It works good on my development machine. But on actual servers, it is really slow to read from these two PerformanceCounters. at least at the first two read operations.
It can take up to 4-6 minutes performing the code below:

        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秒(有时甚至更少)的时间内完成.但是,在我们产品的客户端应使用的实际服务器上,这将花费很长时间.参见下文:

When I run this code on my development machine, it will finish in less than 2 seconds (sometimes even less). But on the actual servers that clients of our product should use, it will take a long time. See below:

Finished initializing Diagnosticss PerformanceCounters. Time elapsed: 00:03:59.6706860

重要的是,这些读取操作(以及后来的读取"操作)将非常快地执行.甚至在开始的时候.

It is important that these read operations (and later 'read' operation) will execute really fast. Even at the beginning.

我的开发计算机是Windows 7、64位,8 GB RAM.
客户端服务器是Windows Server 2008 R2 Enterprise,64位,4 GB RAM.

My development machine is Windows 7, 64 Bit, 8 GB RAM.
Clients servers are Windows server 2008 R2 Enterprise, 64 Bit, 4 GB RAM.

我用Google搜索(并使用Binged了),但找不到任何答案. 为什么会这样呢?我该如何解决?

I Googled(and Binged) it, but couldn't find any answer. Why is this happening? And how can I fix it?

推荐答案

问题很可能与受监视系统上安装的其他软件提供的计数器有关.

The problem is most likely with counters provided by additional software installed on the monitored system.

在这种情况下,应该对系统注册表中注册的所有性能计数器执行性能计数器初始化扫描,这可以通过ProcMon完成.

In this case, you should do a performance counter initialization scan for all performance counters registered in system registry, which can be done with ProcMon.

这篇关于为什么致电PerformanceCounter的速度很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆