.NET

Performance Optimization

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

22 preguntas de entrevista·
Senior
1

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

Respuesta

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?

Respuesta

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()?

Respuesta

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 preguntas de entrevista

Domina .NET para tu próxima entrevista

Accede a todas las preguntas, flashcards, tests técnicos, ejercicios de code review y simuladores de entrevista.

Empieza gratis