Connecting Tech Pros Worldwide Help | Site Map

how to get total cpu utilization and RAM memory utilization in percentage.

Newbie
 
Join Date: Sep 2008
Posts: 18
#1: Sep 12 '08
hi all,

i am new in .NET, some body please help me regarding calculating total cpu utilization and RAM memory utilization in percentage (Accurately) on page load event of ASP.NET.

i found the following code from web, but it is not running properly and also i have no idea what it will do, i heared that there will be used performance monitoring and donot know how to use it.

please help me with coding instructins.


Expand|Select|Wrap|Line Numbers
  1.     public PerformanceCounter cpuCounter;
  2.     public PerformanceCounter ramCounter;
  3.  
  4.       cpuCounter = new PerformanceCounter();
  5.         cpuCounter.CategoryName = "Processor";
  6.         cpuCounter.CounterName = "% Processor Time";
  7.         cpuCounter.InstanceName = "_Total";
  8.  
  9.         ramCounter = new PerformanceCounter("Memory", "Available MBytes");
  10.  
  11.         /* 
  12.         Call this method every time you need to know 
  13.         the current cpu usage. 
  14.         */
  15.     }
  16.     public string getCurrentCpuUsage()
  17.     {
  18.      return  cpuCounter.NextValue() + "%";
  19.     }
  20.  
  21.     /* 
  22.     Call this method every time you need to get 
  23.     the amount of the available RAM in Mb 
  24.     */
  25.     public string getAvailableRAM()
  26.     {
  27.         return ramCounter.NextValue() + "Mb";
  28.     }
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 700
#2: Sep 12 '08

re: how to get total cpu utilization and RAM memory utilization in percentage.


you can get lot of information using Windows Management Instrumentation or WMI...

here is a sample code ..

Expand|Select|Wrap|Line Numbers
  1. ManagementObjectSearcher searcher =new ManagementObjectSearcher("root\\CIMV2","SELECT * FROM Win32_Processor");
  2.  
  3.  
  4. foreach (ManagementObject queryObj in searcher.Get())
  5.                 {
  6. string s =Convert.ToString(queryObj["LoadPercentage"]).Trim();
  7. }
  8.  
  9.  
  10. ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_OperatingSystem");
  11.  
  12.                 foreach (ManagementObject queryObj in searcher.Get())
  13.                 {
  14.  
  15. Convert.ToString(queryObj["FreePhysicalMemory"]).Trim())
  16. Convert.ToString(queryObj["FreeVirtualMemory"]).Trim())
  17. Convert.ToString(queryObj["TotalVirtualMemorySize"]).Trim()));          
  18. Convert.ToString(queryObj["TotalVisibleMemorySize"]).Trim())
  19. }
  20.  
  21.  
Look up for Win32_operating system n other classes...
Processor load is in percentage.. ram you will have to calculate...

Performance counters is another way of doing this... though i think wmi queries from performance counters... (i may be wrong on this... )
Newbie
 
Join Date: Sep 2008
Posts: 18
#3: Sep 12 '08

re: how to get total cpu utilization and RAM memory utilization in percentage.


which namespace will be used in this case??
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 700
#4: Sep 12 '08

re: how to get total cpu utilization and RAM memory utilization in percentage.


"which namespace will be used in this case??"
System.Management;
Newbie
 
Join Date: Sep 2008
Posts: 18
#5: Sep 12 '08

re: how to get total cpu utilization and RAM memory utilization in percentage.


thanks, for answer but i am unable to use "System.Management" in .aspx page
PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 700
#6: Sep 12 '08

re: how to get total cpu utilization and RAM memory utilization in percentage.


Quote:

Originally Posted by balach

thanks, for answer but i am unable to use "System.Management" in .aspx page

did you add reference to System.Management?.... can you tell me wat exactly are you tryin? If you are tryin to get client info (client PC) by adding System.Management, code in .aspx... then the result that you get will be of the server PC... although you can remotely connect to client PC.. that require series of permission.. which are generally denied...still i think you can gather information ...by specifyin computer name in @"root\CIMV2\"
Newbie
 
Join Date: Sep 2008
Posts: 18
#7: Sep 12 '08

re: how to get total cpu utilization and RAM memory utilization in percentage.


thanks alot "dirtbag", i was missing to add reference in my project. this was little thing but i was much confused.

GOD BLESS U
Reply