Process.WorkingSet64 返回最大值 4294967295 [英] Process.WorkingSet64 returns max value of 4294967295
问题描述
我正在尝试使用 C# 监视另一个进程的内存.但是,Process.WorkingSet64 或 Process.PrivateMemorySize64 将输出的最大内存值是 4294967295.我也遇到了与性能计数器相同的问题.
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.
这是我的代码:
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}");
}
}
}
}
在带有 .net 4.61 的 Windows Server 2012 R2 上运行.
Running on Windows Server 2012 R2 with .net 4.61.
输出:
C:\XXX\perfmon>perfmon.exe 15800
ProcessName:XXX.Windows.Services
WorkingSet64 PrivateMemorySize64 Ram PC
4294967295 4294967295 4.294967E+09
进程的 Powershell 输出:
Powershell output for process:
PS C:\Users\xxx> get-process -id 15800 | select-object -property workingSet64
WorkingSet64
------------
5079859200
进程的任务列表输出:
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
如您所见,C# 进程在 4294967295 处停止.但是使用 powershell 或 tasklist 继续测量超过 4 GB 的内存.
As you can see, the C# process stops at 4294967295. However using powershell or tasklist continues to measure memory above 4 GB.
这是我的代码中的问题还是使用 C#/.net 进行内存测量的已知问题?
Is this a problem in my code or is this a known issue with memory measurement with C#/.net?
推荐答案
您无法从 32 位应用程序可靠地监视 64 位进程,Wow64 仿真器会处理数字以防止它们溢出 UInt32.MaxValue.只需移除抖动强制,您也可以作为 64 位进程运行.
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,取消勾选 Prefer 32-bit.
Project > Properties > Build tab > Platform target = AnyCPU, untick Prefer 32-bit.
这篇关于Process.WorkingSet64 返回最大值 4294967295的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!