Question: How to get GC Stats
How do I report info about the number of GC2,1,0s in a benchmark? thanks Matt
You need to use MemoryDiagnoser. You can find the description here
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,
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.
Perfect, thanks
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.RunnerreturnsSummarywhich 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