Measurement Process memory usage gives very low (incorrect) values

I have the following code to start and monitor a process:

Process process = new Process();
process.StartInfo.FileName = "foo.exe";

long maxMemoryUsage = 0;

process.Start()

while(!process.HasExited)
{
    maxMemoryUsage = Math.Max(maxMemoryUsage, process.PrivateMemorySize64);
}

      

After using this code to run a large application that according to the task manager was using 328MB at its peak ("Private Working Memory Set"). The maxMemoryUsage value and the process.PeakPagedMemorySize64 value are 364544. According to MSDN, this value should be interpreted as bytes, which means just over 300KB , which is a thousand thousand less than the expected cost. Another process. Cleaning up ... The memory properties also report extremely low values ​​(everything under a megabyte except for PeakVirtualMemorySize64 which is 4MB, which is the minimum value for this field in my opinion).

I have tried running different applications (in C # and C ++ from which I have source code) that I know use very little or a lot of memory and memory values ​​that are always very close to the values ​​observed in the process Apparently I am doing something completely wrong.

So my question is, how to measure the maximum memory usage of a process that I spawned from my C # application. (Note that I don't need to have the ealtime value if I know its value after the program exits, I also don't need it very accurately, since I don't care if it was 27.04MB or 30MB, but if you have 30 MB or 100 MB).

Edit: here is a complete reproducible test case

class Program
{
    static void Main(string[] args)
    {
        Process process = new Process();
        process.StartInfo.FileName = @"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe";

        long maxMemoryUsage = 0;

        process.Start();

        while(!process.HasExited)
        {
            maxMemoryUsage = Math.Max(maxMemoryUsage, process.PagedMemorySize64);
        }

        Console.Out.WriteLine("Memory used: " + (maxMemoryUsage / 1024.0) / 1024.0 + "MB");

        Console.ReadLine();
    }
}

      

According to Task Manager, Visual Studio uses 103 MB. After closing Visual Studio, the program reports 0.3984375MB.

+3


source to share


1 answer


The process class is heavily cached. You will only get the cached result, no matter how many times you read any property, unless you call the method call Refresh

. To get a non-cached result, you need to call Process.Refresh .

For a quote from msdn

When a Process component is associated with a process resource, the Process property values ​​are immediately populated according to the status of the associated process. If information about the associated process subsequently changes, those changes are not reflected in the cache values ​​of the Process component . A process component is a snapshot of a process resource at the time they are linked. To view the current values ​​for the associated process, call the Refresh method.



So your code will become:

while(!process.HasExited)
{
    process.Refresh();
    maxMemoryUsage = Math.Max(maxMemoryUsage, process.PrivateMemorySize64);
}

      

Also you can consider properties process.PeakXXX

that will help you, I suppose.

+5


source







All Articles