Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CPU usage in .net core (at least on Windows)

So PerformanceCounter is gone in dotnet core. I understand it was because it was not Linux-compatible.

This comment here: ( What is the story of Performance Counters for .NET Core? ) seems to suggest that if I was willing to run it only on Windows I could "make use of Windows-specific features", but I don't know how I could integrate that in dotnet core.

The reason I am willing to do this workaround is to keep the dotnet core codebase so I don't have to migrate once there is a cross-platform solution for PerformanceCounter.

So to summarize my question: how can I run the CPU usage in dotnet core when running on Windows?


Other relevant posts that I've found:

  • This solution seems to suggest that Process.GetCurrentProcess() would be enough, which I don't quite understand (PerformanceCounterprovided the full CPU usage for the machine).

  • Looping through all processes (as this other post seem to suggest?) is not feasible (it throws an exception for some processes) and it looks pretty slow as well which is a problem for my use case.

like image 983
Xavier Peña Avatar asked Aug 14 '17 13:08

Xavier Peña


People also ask

What causes high CPU usage C#?

May be your application runs multiple threads. I have same issue that runs multiple threads and in each thread there is continuous update which makes the application usage from 50% to 100%.

How do I debug high CPU usage in Windows?

Click Start, click Run, type the path of the Debug Diagnostics Tool, and then click OK. On the Tools menu, click Options and Settings. On the Performance Log tab, click Enable Performance Counter Data Logging, and then click OK.

How much faster is .NET Core than .NET framework?

NET Core vs. . NET Framework performance has been tested, with one test demonstrating 7 to 13 times faster. The . NET Core has a recompilation feature to optimize compiled code without changes, making it easier for developers to test and deploy code.

How do you analyze CPU usage?

Collect CPU usage data Select Debug > Performance Profiler. Under Available tools, select CPU Usage, and then select Start. After the app starts, the diagnostic session begins and displays CPU usage data. When you're finished collecting data, select Stop Collection.


1 Answers

As of .NET Core 3.0, the Performance Counter APIs are once again part of the BCL and support Linux too:

  • MS: PerformanceCounters or equivalent are available in NETCore 3.0 and higher

  • MS DevBlog: Diagnostic improvements in net-core 3.0

  • SO: Linux example

like image 107
Martin Avatar answered Oct 24 '22 13:10

Martin