如何在 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
How to get memory available or used in C#
提问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
回答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 dispose
the returned Process
after using it.
除了@JesperFyhrKnudsen的回答和@MathiasLykkegaardLorenzen的评论外,您最好在使用后dispose
返回Process
。
So, In order to dispose the Process
, you could wrap it in a using
scope or calling Dispose
on the returned process (proc
variable).
因此,为了处置Process
,您可以将其包装在using
范围内或调用Dispose
返回的进程(proc
变量)。
using
scope: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; }
Or
Dispose
method:var memory = 0.0; Process proc = Process.GetCurrentProcess(); memory = Math.Round(proc.PrivateMemorySize64 / 2^20, 2); proc.Dispose();
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; }
或
Dispose
方法:var memory = 0.0; Process proc = Process.GetCurrentProcess(); memory = Math.Round(proc.PrivateMemorySize64 / 2^20, 2); proc.Dispose();
Now you could use the memory
variable which is converted to Megabyte.
现在您可以使用memory
转换为兆字节的变量。