CLR, GC, memory, threading, profiling
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start free.NET is a cross-platform, open-source runtime supporting Windows, Linux, and macOS, while .NET Framework is Windows-only and considered legacy. .NET offers unified versioning and LTS releases, making it suitable for modern development.
The Common Language Runtime (CLR) is the execution engine for .NET applications. It manages memory, handles garbage collection, and provides services like security and exception handling.
There are three generations: Gen 0 for short-lived objects, Gen 1 for objects surviving one collection, and Gen 2 for long-lived objects. This generational model helps optimize memory reclamation.
The LOH is a special heap for objects larger than 85KB. It is collected less frequently and can become fragmented, affecting memory usage.
Tiered compilation allows the JIT to compile methods quickly for fast startup, then recompile hot methods with optimizations. This approach balances startup time and throughput, improving overall performance.
I use server GC for high-throughput, multi-core environments like web servers, as it performs parallel collections. Workstation GC is better for desktop apps where responsiveness is more important than throughput.
Span<T> allows slicing and processing arrays and buffers without allocating new memory on the heap. Since it's stack-only, it helps avoid unnecessary allocations and improves performance, especially in tight loops.
Thread pool starvation happens when all thread pool threads are busy, causing delays in task scheduling. I diagnose it by monitoring thread pool metrics and checking for blocking calls or excessive synchronous work.
I use dotnet-counters for live metrics like CPU, GC, and thread pool usage. For deeper analysis, I collect traces with dotnet-trace and capture dumps with dotnet-dump when needed.
Memory<T> can be used across async boundaries and supports heap allocation, making it suitable for scenarios where data needs to persist beyond the current stack frame. Span<T> is stack-only and can't be used in async methods.
By segregating objects by their lifespan, generational GC reduces the frequency and cost of collecting long-lived objects. Most collections happen in Gen 0, which is fast and minimizes application pauses.
LOH fragmentation can waste memory and cause allocation failures. To mitigate, I minimize large allocations, reuse buffers, and monitor LOH usage with profiling tools. If fragmentation is severe, I consider redesigning data structures.