Reputation: 5836
I'm trying to monitor the memory of another process with C#. However, the maximum memory value that Process.WorkingSet64 or Process.PrivateMemorySize64 will output is 4294967295. I've also have the same problem with Performance Counters.
Here's my code:
using System;
using System.Diagnostics;
namespace perfmon
{
class Program
{
static void Main(string[] args)
{
int pid;
if (int.TryParse(args[0], out pid))
{
var p = Process.GetProcessById(pid);
var ramCounter = new PerformanceCounter("Process", "Working Set", p.ProcessName);
Console.WriteLine($"ProcessName:{p.ProcessName}");
var ram = ramCounter.NextValue();
p.Refresh();
Console.WriteLine("WorkingSet64\tPrivateMemorySize64\tRam PC");
Console.WriteLine($"{p.WorkingSet64}\t{p.PrivateMemorySize64}\t\t{ram}");
}
}
}
}
Running on Windows Server 2012 R2 with .net 4.61.
Output:
C:\XXX\perfmon>perfmon.exe 15800
ProcessName:XXX.Windows.Services
WorkingSet64 PrivateMemorySize64 Ram PC
4294967295 4294967295 4.294967E+09
Powershell output for process:
PS C:\Users\xxx> get-process -id 15800 | select-object -property workingSet64
WorkingSet64
------------
5079859200
Tasklist output for process:
C:\Users\xxx>tasklist /FI "PID eq 15800"
Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
XXX.Windows.Services 15800 Services 0 5,031,424 K
As you can see, the C# process stops at 4294967295. However using powershell or tasklist continues to measure memory above 4 GB.
Is this a problem in my code or is this a known issue with memory measurement with C#/.net?
Upvotes: 2
Views: 1231
Reputation: 941455
You cannot reliably monitor a 64-bit process from a 32-bit app, numbers are cooked by the Wow64 emulator to prevent them from overflowing UInt32.MaxValue. Just remove the jitter forcing so you can run as a 64-bit process as well.
Project > Properties > Build tab > Platform target = AnyCPU, untick Prefer 32-bit.
Upvotes: 5
Reputation: 6570
I don't see any problem in your code and while I suspected some very improbable weird interaction between the c# 6.0 features and the 32bit vs 64bit properties, the NextValue() method on PerformanceCounter is a float, so no way.
That leads me to believe the problem is not .Net and not the code, but something in your system, like the WMI. Possible related: http://msdn.developer-works.com/article/13358955/Interesting+issue+with+ManagementEventWatcher+monitoring+registry+changes
Upvotes: 1