BenchmarkDotNet icon indicating copy to clipboard operation
BenchmarkDotNet copied to clipboard

Question: How to get GC Stats

Open Mattacks opened this issue 9 years ago • 5 comments

How do I report info about the number of GC2,1,0s in a benchmark? thanks Matt

Mattacks avatar Jan 09 '17 18:01 Mattacks

You need to use MemoryDiagnoser. You can find the description here

adamsitnik avatar Jan 09 '17 20:01 adamsitnik

Yes, I saw that. On that page it says "the extra columns on the right-hand side ("Gen 0", "Gen 1", "Gen 2" and "Allocated"

But

  • in the table presented there are no columns labelled Gen1 and Gen2
  • the columns in the tables that come out do not seem to indicate "number of collections" which is what I would like to gather, but amount collected. Am I wrong?

I would like to add my own Diagnostic if it is not built-in, but it is not clear when the method to return the value would be called by the framework,

Mattacks avatar Jan 09 '17 21:01 Mattacks

We display Gen 0/1/2 columns if there were any collections in these generations. Usually there are only Gen 0 collections. We also scale the results, because some benchmarks might get executed 1k times, other 2k times, so if they have equal number of collections it does not mean that they are equal. That's why we scale the results (user see a number of collections per 1k benchmark invocation).

If you want to get the number of collections: Benchmark.Runner returns Summary which has an array of BenchmarkReports, which expose the GcStats which exposes the number of collections.

adamsitnik avatar Jan 10 '17 09:01 adamsitnik

Perfect, thanks

Mattacks avatar Jan 10 '17 09:01 Mattacks

We display Gen 0/1/2 columns if there were any collections in these generations. Usually there are only Gen 0 collections. We also scale the results, because some benchmarks might get executed 1k times, other 2k times, so if they have equal number of collections it does not mean that they are equal. That's why we scale the results (user see a number of collections per 1k benchmark invocation).

If you want to get the number of collections: Benchmark.Runner returns Summary which has an array of BenchmarkReports, which expose the GcStats which exposes the number of collections.

The referred GcStats link exists, but the other links in your answer are missing:

  • https://github.com/dotnet/BenchmarkDotNet/blob/master/src/BenchmarkDotNet.Core/Reports/BenchmarkReport.cs
  • https://github.com/dotnet/BenchmarkDotNet/blob/master/src/BenchmarkDotNet.Core/Reports/Summary.cs

juniormayhe avatar Jan 23 '21 12:01 juniormayhe