Click here to Skip to main content
15,909,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 queries.
I have created working with cpu monitoring, I have retrieved value for CPU Usage, Ram Usage(memory), were as CPU usage has been calculated in percentage.

1. I need Memory Usage as Percentage value. I have tried but i couldn't reach.

2. When i host this application in Windows Service, How i can identify which machine usage consumption.

C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    static List<float> AvailableCPU = new List<float>();
    static List<float> AvailableRAM = new List<float>();
    protected static PerformanceCounter cpuCounter;
    protected static PerformanceCounter ramCounter;
    private void Form1_Load(object sender, EventArgs e)
    {
        cpumonitor("CL35");
    }
    public void cpumonitor(string machinename)
    {
        cpuCounter = new PerformanceCounter();
        cpuCounter.CategoryName = "Processor";
        cpuCounter.CounterName = "% Processor Time";
        cpuCounter.InstanceName = "_Total";
        //string machinename = System.Environment.GetEnvironmentVariable("COMPUTERNAME");
        cpuCounter.MachineName = machinename;
        ramCounter = new PerformanceCounter("Memory", "Available MBytes");
        try
        {
            System.Timers.Timer t = new System.Timers.Timer(5000);
            t.Elapsed += new ElapsedEventHandler(TimerElapsed);
            t.Start();
            Thread.Sleep(10000);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "catched exception");
        }
    }
    public static void TimerElapsed(object source, ElapsedEventArgs e)
    {
        float cpu = cpuCounter.NextValue();
        float ram = ramCounter.NextValue();
        MessageBox.Show(string.Format("CPU Value: {0}, ram value: {1}", cpu, ram));
        AvailableCPU.Add(cpu);
        AvailableRAM.Add(ram);
    }
}


What I have tried:

I have tried for getting total memory usage.

C#
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static List<float> AvailableCPU = new List<float>();
        static List<float> AvailableRAM = new List<float>();
        protected static PerformanceCounter cpuCounter;
        protected static PerformanceCounter ramCounter;
        protected static PerformanceCounter ramTotMB;
        private void Form1_Load(object sender, EventArgs e)
        {
            cpumonitor("CL35");
        }
        public void cpumonitor(string machinename)
        {
            cpuCounter = new PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";
            //string machinename =           System.Environment.GetEnvironmentVariable("COMPUTERNAME");
            cpuCounter.MachineName = machinename;
            ramCounter = new PerformanceCounter("Memory", "Available MBytes");
            ramTotMB = new PerformanceCounter("Memory", "Total MBytes");
            try
            {
                System.Timers.Timer t = new System.Timers.Timer(5000);
                t.Elapsed += new ElapsedEventHandler(TimerElapsed);
                t.Start();
                Thread.Sleep(10000);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "catched exception");
            }
        }
        public static void TimerElapsed(object source, ElapsedEventArgs e)
        {
            float cpu = cpuCounter.NextValue();
            float ram = ramCounter.NextValue();
            float Totram = ramTotMB.NextValue();
            MessageBox.Show(string.Format("CPU Value: {0}, ram value: {1},Totalvalue: {2}", cpu, ram,Totram));
            AvailableCPU.Add(cpu);
            AvailableRAM.Add(ram);
        }
    }
Posted
Updated 1-Feb-17 22:40pm
v3
Comments
Maciej Los 2-Feb-17 2:40am    
And what's wrong with your code?
vinodh muthusamy 2-Feb-17 2:42am    
i need to get how much memory used in Percentage.
Jochen Arndt 2-Feb-17 2:52am    
usedMem = totalMem - availMem;
percentageMemUsage = 100 * usedMem / totalMem;
vinodh muthusamy 2-Feb-17 3:14am    
S absolutely correct. But i tried to get totalmemory of specified machine. I coudn't fetch value.
Jochen Arndt 2-Feb-17 3:32am    
See my solution.

1 solution

Try
C#
ramTotMB = new PerformanceCounter("Memory", "Total Bytes");

There is no counterName "Total MBytes" property (see Memory Object[^] ).

You might also search the web for other methods to get the physical memory using something like "C# total physical memory".
 
Share this answer
 
Comments
vinodh muthusamy 2-Feb-17 3:47am    
I have tried this . It throws a error as

Could not locate Performance Counter with specified category name 'Memory', counter name 'Total Bytes'.
Jochen Arndt 2-Feb-17 4:19am    
That is why I suggested to use a different method.
The total memory is not really related to performance monitoring but a system property.
Member 14569571 29-Aug-19 8:26am    
i want to get the RAM usage in percentage

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900