Home > Software engineering >  Is GC stats still accessible on perfmon/PerformanceCounter.dll for a .NET6 desktop app?
Is GC stats still accessible on perfmon/PerformanceCounter.dll for a .NET6 desktop app?

Time:12-21

We have a WPF app (x64) running on .NET4.8 and we used to print GC/memory related stats onto log periodically using System.Diagnostics.PerformanceCounter. (Using category: ".NET CLR Memory")

After I migrated it to .NET6, counters on ".NET CLR Memory" category throws error because the instance (which is the process name) is missing.

I then check this from perfmon, and realized that our app is no longer showed in the instance list.

Since we distribute our desktop app to enduser, we record the gc stats periodically onto log while they are running the app. So using external tool adhoc is not an option to us.

I want to know for a .NET6 app:

  • If getting GC stat in-app is still possible?
  • If yes - Is category ".NET CLR Memory" still the right category?
  • If yes - Is there any runtime config required for performanceCounter to expose the app itself for querying?
  • If no - Is there any alternative api to get GC stat in-app?

Many thanks for your help & advice!

I have tried below things, but still the app can't be found on perfmon:

  • Starting the app with (debug) & without visual studio.
  • Starting the app with Any CPU on visual studio (used to be x64).
  • Adding myself to Performance Monitor Users & Performance Log Users group. (I believe this is not related anyways, since the build on .NET4.8 can still be discovered by perfmon without this being set and the data can be read from performanceCounter.dll without issue.)
  • Asking IT to add myself admin right.
  • Start a simple new console app on .NET6 - same result - not discovered on perfmon.

CodePudding user response:

In .NET 6, it is still possible to get GC statistics in-app using the System.Diagnostics.PerformanceCounter class. However, the ".NET CLR Memory" category is no longer available, and you will need to use a different category to get the GC statistics you are interested in.

To get GC statistics in-app using .NET 6, you can use the ".NET Core Memory" category, which includes counters for various GC-related statistics, such as the number of bytes in the GC heap, the number of GC collections that have occurred, and the amount of time spent in GC.

CodePudding user response:

.NET 6.0 has improved built-in GC statistics that can be accessed directly on the GC class instead of using the performance counter infrastructure. You can get a lot of information about the current process' memory usage by calling the new method GC.GetGCMemoryInfo().

  • Related