Search code examples
c#garbage-collectionperformancecounterperfmon

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


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.

Solution

  • .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().