Getting a process's ram usage

前端 未结 4 1961
刺人心
刺人心 2020-12-14 07:22

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)<

相关标签:
4条回答
  • 2020-12-14 08:02

    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 worked but Performance Counter does !

    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 !

    0 讨论(0)
  • 2020-12-14 08:13

    I found this on msdn and it is working

    System.Diagnostics.Process proc = ...; // assign your process here :-)
    
    int memsize = 0; // memsize in KB
    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();
    
    0 讨论(0)
  • 2020-12-14 08:20

    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."

    0 讨论(0)
  • 2020-12-14 08:20

    Win32 GetSystemInfo function works, too.

    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

    0 讨论(0)
提交回复
热议问题