Benchmarkdotnet: NativeMemoryProfiler reports false positive leak

Created on 17 Apr 2020  路  7Comments  路  Source: dotnet/BenchmarkDotNet

Hello all,
Thank you for the work on the project, it is much appreciated!

I am reporting an issue we are experiencing with the NativeMemoryProfiler of BenchmarkDotNet. It detects small but constant native memory leaks that seem to be false positives.
The attached reproducer showcases the issue. The benchmark only instantiates a mock object and computes a SHA256 (to avoid spending too little time in the benchmark). The resulting detected native memory leak is 0.06KB on my machine.

I believe the mocking library we use does not do weird things with native memory, so this gives me a strong feeling that this could be a false positive. Please note that this also happens with an industrial library we use in our software and which is heavily tested for memory leaks. While the library is using native memory, we are confident that the provider extensively tested it for any leak, which is another evidence that this could be a false positive. Unfortunately I cannot provide this library for inspection.

I am available for clarifications, should the need arise.

Version of BenchmarkDotNet: 0.12.1
Version of the Windows Diagnostics nuget: 0.12.1
Version of .net: core 3.1
Minimal reproducer: MemoryProfilerReproducer.zip

Diagnosers

Most helpful comment

The following code present starting action:
image

And my calculations take a jitting of method (with their native allocations) between Start and Stop:
image
image
image

All 7 comments

@WojciechNagorski could you please take a look?

I started my investigation.
I run my repro (based on @jjolidon's report) using the following command because I wanted to run a benchmarked method only once.

dotnet run -c Release -f netcoreapp3.1 -- --filter *Just* --launchCount 1 --iterationCount 1 --invocationCount 1 --unrollFactor 1

And I got (2320 B memory leak)
image

Then I use Perfview to analyze a trace file:
image

image

@adamsitnik Maybe you know what it is? If not I need more time to understand this problem.

After I open "Net Os Heap Alloc Stacks" I got a message:
image

It is a minimal repro:
```c#
using System.Diagnostics;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Diagnostics.Windows.Configs;

namespace BenchmarkDotNet.Samples
{

[NativeMemoryProfiler]
public class JustMock
{

    [Benchmark]
    public string MyBenchmark()
    {
        var stackTrace = new StackTrace();

        return stackTrace.GetType().Name ;
    }
}

}

The result:

JustMock.MyBenchmark: Job-SNEMBI(InvocationCount=1, IterationCount=1, LaunchCount=1, UnrollFactor=1)

Native memory allocated per single operation: 3544 B
Count of allocated object: 19
Native memory leak per single operation: 2320 B
Count of not deallocated object: 12
```

After adding additional providers, I received a readable tracking file:
image
But the memory leak is still there.

I have a fix for .Net Framework. .Net Core has different behavior, but I know where a problem is and how to fix it. But I have to find free time to implement a fix.

The following code present starting action:
image

And my calculations take a jitting of method (with their native allocations) between Start and Stop:
image
image
image

I have a fix for .Net Framework. .Net Core has different behavior, but I know where a problem is and how to fix it

Great news!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Turnerj picture Turnerj  路  4Comments

AndreyAkinshin picture AndreyAkinshin  路  4Comments

Vermistuk picture Vermistuk  路  4Comments

AndreyAkinshin picture AndreyAkinshin  路  4Comments

BlueRaja picture BlueRaja  路  4Comments