Skip to content

Commit

Permalink
Updated comment from feedback.
Browse files Browse the repository at this point in the history
Added conditional compilation around thread sleep.
  • Loading branch information
timcassell committed Apr 25, 2024
1 parent 31f280d commit 63b62a9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/BenchmarkDotNet/Engines/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,18 @@ private ClockSpan Measure(Action<long> action, long invokeCount)
var exceptionsStats = new ExceptionsStats(); // allocates
exceptionsStats.StartListening(); // this method might allocate

#if !NET7_0_OR_GREATER
if (RuntimeInformation.IsNetCore && Environment.Version.Major is >= 3 and <= 6 && RuntimeInformation.IsTieredJitEnabled)
{
// #1542
// We put the current thread to sleep so tiered jit can kick in, compile its stuff,
// and NOT allocate anything on the background thread when we are measuring allocations.
// This is only an issue on netcoreapp3.0 to net6.0. Tiered jit allocations were fixed in net7.0,
// This is only an issue on netcoreapp3.0 to net6.0. Tiered jit allocations were "fixed" in net7.0
// (maybe not completely eliminated forever, but at least reduced to a point where measurements are much more stable),
// and netcoreapp2.X uses only GetAllocatedBytesForCurrentThread which doesn't capture the tiered jit allocations.
Thread.Sleep(TimeSpan.FromMilliseconds(500));
}
#endif

// GC collect before measuring allocations.
ForceGcCollect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace BenchmarkDotNet.IntegrationTests
public class MemoryDiagnoserTests
{
// TODO: re-enable allocating tests after https://github.com/dotnet/runtime/issues/101536 is fixed.
private const string AllocatingSkipReason = "System.Runtime.InteropServices.RuntimeInformation allocates during GC collect";
private const string AllocatingSkipReason = "GC collect causes allocations on finalizer thread";

private readonly ITestOutputHelper output;

Expand Down

0 comments on commit 63b62a9

Please sign in to comment.