C# 获取进程的 ram 使用情况
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/842585/
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
Getting a process's ram usage
提问by Meiscooldude
I have been having some trouble figuring out how exactly I get a process's ram usage. (How much ram it is currently consuming, not how much is reserved, or its max or min)
我在弄清楚我如何获得进程的 ram 使用量时遇到了一些麻烦。(它当前消耗了多少内存,而不是保留了多少,或者它的最大值或最小值)
Lets say I have a process running in the back ground, Java.exe, it is allowed to use 1024mb of ram, how can I tell how much ram it is currently using.
假设我有一个进程在后台运行,Java.exe,它被允许使用 1024mb 的内存,我怎么知道它当前使用了多少内存。
I am starting the process myself, so I have access to the Process object, I would just like a little more clarification on what property is the one for me.
我自己开始这个过程,所以我可以访问 Process 对象,我只想更清楚地说明什么属性适合我。
采纳答案by Matthew Flaschen
If you are purely interested in physical memory, you probably want WorkingSet64, which gives "the amount of physical memory allocated for the associated process." Understand that this value constantly fluctuates, and the value this call gives you may not be up to date. You may also be interested in PeakWorkingSet64, which gives "the maximum amount of physical memory used by the associated process."
如果您纯粹对物理内存感兴趣,您可能需要WorkingSet64,它给出“为相关进程分配的物理内存量”。了解此值不断波动,此调用给您的值可能不是最新的。您可能还对PeakWorkingSet64感兴趣,它给出了“相关进程使用的最大物理内存量”。
回答by Laz Nikolaj Andersen
I found this on msdn and it is working
我在 msdn 上找到了这个,它正在工作
System.Diagnostics.Process proc = assign your process here :-)
int memsize = 0; // memsize in Megabyte
PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "Process";
PC.CounterName = "Working Set - Private";
PC.InstanceName = proc.ProcessName;
memsize = Convert.ToInt32(PC.NextValue()) / (int)(1024);
PC.Close();
PC.Dispose();
回答by Mau Mills
great, I wanted this to get the same as depicted in task manager,and tried:
太好了,我希望它与任务管理器中描述的一样,并尝试:
Process.PrivateMemorySize64
Process.PeakVirtualMemorySize64
Process.PeakPagedMemorySize
Process.PagedSystemMemorySize64
Process.PagedMemorySize64
Process.NonpagedSystemMemorySize64
Process.WorkingSet64
and none of those workedbut Performance Counterdoes !
而这些都不工作,但性能计数器呢!
PerformanceCounter PC = new PerformanceCounter();
PC.CategoryName = "Process";
PC.CounterName = "Working Set - Private";
PC.InstanceName = "processNameHere";
memsize = Convert.ToInt32(PC.NextValue()) / (int)(1024);
PC.Close();
PC.Dispose();
thanks a lot !
多谢 !
回答by KulaGGin
Win32 GetSystemInfo functionworks, too.
Win32 GetSystemInfo 函数也能工作。
It is actually successfully used on practice for array of bytes scanning in the process here in this Memory.dll project: https://github.com/erfg12/memory.dll/blob/042db0cf75e4152a7adf1ea47e6f23f1ad763fb6/Memory/memory.cs#L1909
在这个 Memory.dll 项目中,它实际上成功地用于实践中的字节数组扫描:https: //github.com/erfg12/memory.dll/blob/042db0cf75e4152a7adf1ea47e6f23f1ad763fb6/Memory/memory.cs#L1909