如何在 C# 中获得可用或使用的内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/750574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:43:48  来源:igfitidea点击:

How to get memory available or used in C#

c#memory-management

提问by

How can I get the available RAM or memory used by the application?

如何获取应用程序使用的可用 RAM 或内存?

回答by Austin

System.Environmenthas WorkingSet- a 64-bit signed integer containing the number of bytes of physical memory mapped to the process context.

System.Environment具有WorkingSet- 一个 64 位有符号整数,包含映射到进程上下文的物理内存的字节数。

If you want a lot of details there is System.Diagnostics.PerformanceCounter, but it will be a bit more effort to setup.

如果您想要很多详细信息,可以使用System.Diagnostics.PerformanceCounter,但设置起来会更加费力。

回答by Jesper Fyhr Knudsen

You can use:

您可以使用:

Process proc = Process.GetCurrentProcess();

To get the current process and use:

要获取当前进程并使用:

proc.PrivateMemorySize64;

To get the private memory usage. For more information look at this link.

获取私有内存使用情况。有关更多信息,请查看此链接

回答by CMS

You might want to check the GC.GetTotalMemorymethod.

您可能想要检查GC.GetTotalMemory方法。

It retrieves the number of bytes currently thought to be allocated by the garbage collector.

它检索当前认为由垃圾收集器分配的字节数。

回答by DevT

Look herefor details.

详情请看这里

private PerformanceCounter cpuCounter;
private PerformanceCounter ramCounter;
public Form1()
{
    InitializeComponent();
    InitialiseCPUCounter();
    InitializeRAMCounter();
    updateTimer.Start();
}

private void updateTimer_Tick(object sender, EventArgs e)
{
    this.textBox1.Text = "CPU Usage: " +
    Convert.ToInt32(cpuCounter.NextValue()).ToString() +
    "%";

    this.textBox2.Text = Convert.ToInt32(ramCounter.NextValue()).ToString()+"Mb";
}

private void Form1_Load(object sender, EventArgs e)
{
}

private void InitialiseCPUCounter()
{
    cpuCounter = new PerformanceCounter(
    "Processor",
    "% Processor Time",
    "_Total",
    true
    );
}

private void InitializeRAMCounter()
{
    ramCounter = new PerformanceCounter("Memory", "Available MBytes", true);

}

If you get value as 0 it need to call NextValue()twice. Then it gives the actual value of CPU usage. See more details here.

如果您获得的值为 0,则需要调用NextValue()两次。然后它给出了 CPU 使用率的实际值。在此处查看更多详细信息。

回答by PodTech.io

For the complete system you can add the Microsoft.VisualBasic Framework as a reference;

对于完整的系统,您可以添加 Microsoft.VisualBasic Framework 作为参考;

 Console.WriteLine("You have {0} bytes of RAM",
        new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory);
        Console.ReadLine();

回答by Ahmad Payan

In addition to @JesperFyhrKnudsen's answer and @MathiasLykkegaardLorenzen's comment, you'd better disposethe returned Processafter using it.

除了@JesperFyhrKnudsen的回答和@MathiasLykkegaardLorenzen的评论外,您最好在使用后dispose返回Process

So, In order to dispose the Process, you could wrap it in a usingscope or calling Disposeon the returned process (procvariable).

因此,为了处置Process,您可以将其包装在using范围内或调用Dispose返回的进程(proc变量)。

  1. usingscope:

    var memory = 0.0;
    using (Process proc = Process.GetCurrentProcess())
    {
        // The proc.PrivateMemorySize64 will returns the private memory usage in byte.
        // Would like to Convert it to Megabyte? divide it by 2^20
           memory = proc.PrivateMemorySize64 / 2^20;
    }
    
  2. Or Disposemethod:

    var memory = 0.0;
    Process proc = Process.GetCurrentProcess();
    memory = Math.Round(proc.PrivateMemorySize64 / 2^20, 2);
    proc.Dispose();
    
  1. using范围:

    var memory = 0.0;
    using (Process proc = Process.GetCurrentProcess())
    {
        // The proc.PrivateMemorySize64 will returns the private memory usage in byte.
        // Would like to Convert it to Megabyte? divide it by 2^20
           memory = proc.PrivateMemorySize64 / 2^20;
    }
    
  2. Dispose方法:

    var memory = 0.0;
    Process proc = Process.GetCurrentProcess();
    memory = Math.Round(proc.PrivateMemorySize64 / 2^20, 2);
    proc.Dispose();
    

Now you could use the memoryvariable which is converted to Megabyte.

现在您可以使用memory转换为兆字节的变量。