.NET

Performance Optimization

Profiling, BenchmarkDotNet, memory allocation, boxing/unboxing, Span<T>, performance best practices

22 gespreksvragen·
Senior
1

What is the main advantage of BenchmarkDotNet over a simple Stopwatch for measuring performance?

Antwoord

BenchmarkDotNet performs warm-ups, measures multiple iterations with statistical stabilization, avoids JIT and GC biases. A simple Stopwatch can give unrepresentative results as it doesn't control these factors. BenchmarkDotNet also provides detailed metrics like memory allocations per operation.

2

Which Microsoft tool is recommended for analyzing memory allocations and identifying memory leaks in production?

Antwoord

dotMemory from JetBrains and PerfView from Microsoft are recommended tools for profiling memory in .NET. PerfView is free, open-source and can analyze heap allocations, stack traces and GC events. It is particularly suitable for diagnosing memory leaks in production without significant overhead.

3

How to identify hidden memory allocations in a LINQ method like FirstOrDefault()?

Antwoord

Using BenchmarkDotNet with MemoryDiagnoser attribute shows allocations per operation. LINQ lambdas create hidden allocations via delegates. Replacing with a simple foreach loop eliminates these allocations. PerfView can also capture allocation stack traces to identify sources.

4

In BenchmarkDotNet, which attribute to use for measuring CPU performance and identifying code hotspots?

5

How to interpret a BenchmarkDotNet result showing Mean: 125.3 ns, StdDev: 45.2 ns?

+19 gespreksvragen

Beheers .NET voor je volgende gesprek

Krijg toegang tot alle vragen, flashcards, technische tests, code review-oefeningen en gespreksimulatoren.

Begin gratis