Specializations

Sunday, August 10, 2014

How to get CPU Usage and memory Utilization

Use performace Counter for this.


CPU Usage
Prepare PerformanceCounter object.


  Cpuperf.CategoryName = "Processor";
                Cpuperf.CounterName = "% Processor Time";
                Cpuperf.InstanceName = "_Total";
                float first = Cpuperf.NextValue();
 float second= Cpuperf.NextValue();

Note: first value is always 0. So better to take zecond nect value. then it will return the exact value for this.



Memory Utilization:

 PerformanceCounter ramCounterAvailable = new PerformanceCounter("Memory", "Available Bytes");
                PerformanceCounter ramCounterCommitted = new PerformanceCounter("Memory", "Committed Bytes");
                PerformanceCounter ramCounterComitLimit = new PerformanceCounter("Memory", "Commit Limit");
MemoryAvailable = ramCounterAvailable.NextValue();
                MemoryCommitted = ramCounterCommitted.NextValue();
                MemoryComitLimit = ramCounterComitLimit.NextValue();
                var total = MemoryAvailable + MemoryCommitted;
                var MemoryAvailablePercentage = MemoryAvailable / total * 100;
                var MemoryUtilizationPercentage = MemoryCommitted / total * 100;
                second = Cpuperf.NextValue();
                var MemoryUtilizationvalue = (int)Math.Round(MemoryUtilizationPercentage);


Example:
  public void GetCpuAndMemory()
        {
            float second = 0;
            try
            {
PerformanceCounter Cpuperf=new PerformanceCounter();
                PerformanceCounter ramCounterAvailable = new PerformanceCounter("Memory", "Available Bytes");
                PerformanceCounter ramCounterCommitted = new PerformanceCounter("Memory", "Committed Bytes");
                PerformanceCounter ramCounterComitLimit = new PerformanceCounter("Memory", "Commit Limit");

                //Cpuperf = new PerformanceCounter();
                Cpuperf.CategoryName = "Processor";
                Cpuperf.CounterName = "% Processor Time";
                Cpuperf.InstanceName = "_Total";
                float first = Cpuperf.NextValue();
                ramCounterAvailable.NextValue();
                ramCounterCommitted.NextValue();
                ramCounterComitLimit.NextValue();
                var MemoryAvailable = ramCounterAvailable.NextValue();
                var MemoryCommitted = ramCounterCommitted.NextValue();
                var MemoryComitLimit = ramCounterComitLimit.NextValue();
                System.Threading.Thread.Sleep(500);
                MemoryAvailable = ramCounterAvailable.NextValue();
                MemoryCommitted = ramCounterCommitted.NextValue();
                MemoryComitLimit = ramCounterComitLimit.NextValue();
                var total = MemoryAvailable + MemoryCommitted;
                var MemoryAvailablePercentage = MemoryAvailable / total * 100;
                var MemoryUtilizationPercentage = MemoryCommitted / total * 100;
                second = Cpuperf.NextValue();
                var finalCPUusageValue = (int)Math.Round(second);
                var MemoryUtilizationvalue = (int)Math.Round(MemoryUtilizationPercentage);
            }
         
            catch (Exception ex)
            {
             
            }
        }

No comments:

Post a Comment