BenchmarkDotNet icon indicating copy to clipboard operation
BenchmarkDotNet copied to clipboard

Consider changing `Consume` to not hold onto references for very long

Open AndyAyersMS opened this issue 3 years ago • 1 comments

Currently some of the Consume methods stash the reference passed to them in a local field, keeping the object alive as long as the consume object itself remains live.

https://github.com/dotnet/BenchmarkDotNet/blob/b78ec96631c5065895255165b7113bf5062aa407/src/BenchmarkDotNet/Engines/Consumer.cs#L98-L109

If that object roots a large object graph or large array allocation it can alter GC behavior and impact benchmark timing.

Consider either immediately over-writing the local field with null or nulling out this field at the end of measurement intervals.

AndyAyersMS avatar Mar 11 '22 16:03 AndyAyersMS

I actually ran into this while attempting to measure survived memory in #1596. I ended up adding a Clear method that I ran at the end of the benchmark. Unfortunately that PR never got merged (I guess cause .Net Core still hasn't fixed the measurement error).

timcassell avatar Mar 11 '22 16:03 timcassell