对于Working Set PerformanceCounter,负载测试的显示不超过4GB

Wah*_*tar 13 c# performance performancecounter load-testing performance-testing

我正在尝试为某些应用程序创建负载测试.而且我希望只为我的应用程序的进程获取内存使用情况.为此,我添加Process / Working Set到我的计数器中

在此输入图像描述

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

在此输入图像描述

但我的应用程序"以64位模式运行"使用超过4 GB的内存
从TaskManager可以清楚地看到它需要大约6GB,但这个值不会出现在负载测试图中.

那么如何创建自定义的PerformanceCounter来完全像Process/Working Set一个但使用Kilobytes而不是字节我可能得到真正的值.或者任何其他解决方案,使我能够计算我的应用程序在负载测试中使用内存的程度.

Wah*_*tar 4

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

第一步是正常安装新的,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);
Run Code Online (Sandbox Code Playgroud)

下一步是拥有简单的 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)