By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,508 Members | 1,823 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,508 IT Pros & Developers. It's quick & easy.

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

P: 18
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.     }
Sep 12 '08 #1
Share this Question
Share on Google+
7 Replies


Expert 100+
P: 750
PRR
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... )
Sep 12 '08 #2

P: 18
which namespace will be used in this case??
Sep 12 '08 #3

Expert 100+
P: 750
PRR
"which namespace will be used in this case??"
System.Management;
Sep 12 '08 #4

P: 18
thanks, for answer but i am unable to use "System.Management" in .aspx page
Sep 12 '08 #5

Expert 100+
P: 750
PRR
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\"
Sep 12 '08 #6

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

GOD BLESS U
Sep 12 '08 #7

P: n/a
Thanks. Big help for my project, also.
Oct 1 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.