Skip to content

Why does an empty method report allocating bytes? #2402

Closed as not planned
Closed as not planned
@dqwork

Description

@dqwork

Hello,

I noticed some weird results from our benchmarks when we moved our app from .net framework to .net 6.0.

Methods that should not allocate at all were reporting allocations of 8 bytes.

I've investigated and in trying to make a nice simple reproducible example I discovered that an empty method reports allocating 8 bytes

// * Summary *

BenchmarkDotNet v0.13.7, Windows 10 (10.0.19045.3208/22H2/2022Update)
12th Gen Intel Core i7-1265U, 1 CPU, 12 logical and 10 physical cores
.NET SDK 7.0.400
  [Host]     : .NET 6.0.21 (6.0.2123.36311), X64 RyuJIT AVX2
  Job-NXFHSY : .NET 6.0.21 (6.0.2123.36311), X64 RyuJIT AVX2

Runtime=.NET 6.0  InvocationCount=64  IterationCount=10
LaunchCount=1  WarmupCount=8

|      Method |   Mean |  Error | StdErr | StdDev |    Min |     Q1 | Median |     Q3 |    Max |     Op/s | Rank | Allocated |
|------------ |-------:|-------:|-------:|-------:|-------:|-------:|-------:|-------:|-------:|---------:|-----:|----------:|
| EmptyMethod | 0.0 ns | 0.0 ns | 0.0 ns | 0.0 ns | 0.0 ns | 0.0 ns | 0.0 ns | 0.0 ns | 0.0 ns | Infinity |    1 |       8 B |

I've include the very basic set up I have below

Class Library defining the benchmarks - Target framework net6.0, single class

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;

namespace BenchmarkTests
{
    [MemoryDiagnoser]
    [Config(typeof(Config))]
    [RankColumn]
    [AllStatisticsColumn]
    public class Benchmarks
    {

        [Benchmark]
        public void EmptyMethod()
        {

        }


        private class Config : ManualConfig
        {
            public Config()
            {
                AddJob(
                    new Job(EnvironmentMode.Default, RunMode.Short)
                    {
                        Environment = { Runtime = CoreRuntime.Core60 },
                        Run = { InvocationCount = 64, WarmupCount = 8, IterationCount = 10 }
                    });
            }
        }
    }
}

Console App that runs the benchmarks - Target framework net6.0, single class to define entry point

using BenchmarkDotNet.Running;

namespace BenchmarkTests.Runner
{
    internal class Program
    {
        static void Main(string[] args)
        { 
            BenchmarkRunner.Run<Benchmarks>();
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions