我的程序说我剩下0Mb的ram

时间:2016-09-01 14:12:21

标签: c# performance

所以我编写程序在控制台中打印我使用ram,网络,处理器等多少。但是当我只使用我16gb内存的30%时,可用ram(在Mb中)显示0。

我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;


namespace Perf_Monitor
{
    class Program
    {
        static void Main(string[] args)
        {
            PerformanceCounter perfCpuCount = new     PerformanceCounter("Processor Information", "% Processor Time", "_Total");
            PerformanceCounter perfMemDowCount = new     PerformanceCounter("Memory", "Available MBytes");
            PerformanceCounter perfNetDowCount = new     PerformanceCounter("Network Adapter", "Bytes Received/sec", "Intel[R] 82579V     Gigabit Network Connection");
            PerformanceCounter perfNetUpCount = new     PerformanceCounter("Network Adapter", "Bytes Sent/sec", "Intel[R] 82579V Gigabit       Network Connection");



            while (true)
            {
                Thread.Sleep(1000);
                Console.WriteLine("CPU Load:            {0}%",     perfCpuCount.NextValue());
                Console.WriteLine("Available RAM:       {0}",     perfCpuCount.NextValue());
                Console.WriteLine("Network Usage Down:  {0}Mbit/s",     perfNetDowCount.NextValue() / 125000);
                Console.WriteLine("Network Usage Up:    {0}Mbit/s",     perfNetUpCount.NextValue() / 125000);
            }

        }
    }
}

这就是它出现的方式:

My Program

1 个答案:

答案 0 :(得分:2)

您有两次使用perfCpuCount的复制/粘贴错误:

Console.WriteLine("CPU Load: {0}%",     perfCpuCount.NextValue());
Console.WriteLine("Available RAM: {0}", perfCpuCount.NextValue());

应该是:

Console.WriteLine("CPU Load: {0}%",     perfCpuCount.NextValue());
Console.WriteLine("Available RAM: {0}", perfMemDowCount.NextValue());