工作集 PerformanceCounter 的负载测试未显示超过 4GB

Load test doesn't show more than 4GB for Working Set PerformanceCounter

我正在尝试为某些应用程序创建 load test我只想获取我的应用程序进程的内存使用情况。为此,我将 Process / Working Set 添加到我的计数器集

问题是 Working Set PerformanceCounter 以字节为单位读取值并且没有计算超过 4294967296 等于 4 GB 的值

但我的应用程序 "runs in 64-bit mode" 使用了 超过 4 GB 的内存
从 TaskManager 中可以清楚地看出,我看到它大约需要 6GB,但是这个值 没有出现在负载测试图表 中。

那么如何创建自定义的 PerformanceCounter 以完全像 Process/Working Set 一样,但使用 千字节 而不是字节我可以获得真实值。或者任何其他使我能够计算我的应用程序在负载测试中使用了多少内存的解决方案。

根据PerformanceCounter.RawValue Property的文档:

If the counter type is a 32-bit size and you attempt to set this property to a value that is too large to fit, the property truncates the value to 32 bits.

所以你必须使用正确的PerformanceCounterType(那些,后缀为64)。

我找到了解决办法。感谢您的所有评论,所有评论都非常有帮助。

第一步正常安装新的PerformanceCounterCategory只是最重要的是设置为PerformanceCounterCategoryType.MultiInstance 例如

var countersToCreate = new CounterCreationDataCollection();
var memoryCounterData = new CounterCreationData("Memory Usage", "Memory Usage", PerformanceCounterType.NumberOfItems64);
countersToCreate.Add(memoryCounterData);
PerformanceCounterCategory.Create("KB Memory Usage", "KB Memory Usage", PerformanceCounterCategoryType.MultiInstance, countersToCreate);

下一步 是拥有简单的 windows 服务或控制台应用程序,它应该从 process.WorkingSet64 读取每个进程的值并将它们设置为您的PerformanceCounter此应用程序或服务应该 运行 在您 运行 进行负载测试时当然是在 x64 模式下 。 例如

static void Main(string[] args)
{
    while (true)
    {
        Thread.Sleep(500);
        foreach (var process in Process.GetProcesses())
        {
            var memoryUsage = new PerformanceCounter("KB Memory Usage", "Memory Usage", process.ProcessName, false);
            memoryUsage.RawValue = process.WorkingSet64/1024;
        }
    }
}