如何计算ram的百分比(memoryusage) [英] How to calculate percentage for ram(memoryusage)
问题描述
我有2个查询。
我创建了使用cpu监控,我检索了CPU使用率,Ram使用率(内存)的值,因为CPU使用率已按百分比计算。 />
1.我需要内存使用率作为百分比值。我已经尝试了但是我无法达到。
2.当我在Windows服务中托管此应用程序时,我如何识别哪种机器使用消耗。
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.
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);
}
}
我尝试了什么: < br $> b $ b
我试图获得总内存使用量。
What I have tried:
I have tried for getting total memory usage.
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);
}
}
推荐答案
尝试
Try
ramTotMB = new PerformanceCounter("Memory", "Total Bytes");
没有 counterName
Total MBytes属性(参见内存对象 [ ^ ])。
你也可以在网上搜索其他方法来获取物理内存,比如C#总物理内存。
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".
>
这篇关于如何计算ram的百分比(memoryusage)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!