How .NET Actually Works
Most .NET developers don't know what happens after F5.
And that's exactly why they can't debug the hard problems.
Here's what actually happens when you run a .NET application:
๐ฆ๐๐ฒ๐ฝ ๐ญ: ๐๐ผ๐บ๐ฝ๐ถ๐น๐ฎ๐๐ถ๐ผ๐ป
Your C# code doesn't compile to machine code. It compiles to IL (Intermediate Language) via Roslyn. IL is platform independent, which is why the same .dll runs on Windows, Linux, and macOS. This is the part most people get right.
๐ฆ๐๐ฒ๐ฝ ๐ฎ: ๐ง๐ต๐ฒ ๐ฅ๐๐ป๐๐ถ๐บ๐ฒ (๐๐ผ๐ฟ๐ฒ๐๐๐ฅ)
CoreCLR loads your assembly and does three things: resolves all dependencies, loads types and metadata, and sets up the garbage collector. This is where most startup performance problems live, and most developers never look here.
๐ฆ๐๐ฒ๐ฝ ๐ฏ: ๐๐๐ง ๐๐ผ๐บ๐ฝ๐ถ๐น๐ฎ๐๐ถ๐ผ๐ป
The JIT compiler converts IL to native machine code, method by method, on first call. That's why the first request to your API is always slower. Tiered compilation helps: Tier 0 compiles fast, then Tier 1 re-compiles hot methods with full optimizations. Dynamic PGO takes it further by using actual runtime data to optimize.
The alternative is NativeAOT, which compiles everything upfront at build time. No JIT, no startup penalty, smaller binaries. Tradeoff: no dynamic code generation.
๐ฆ๐๐ฒ๐ฝ ๐ฐ: ๐๐
๐ฒ๐ฐ๐๐๐ถ๐ผ๐ป
Your native code runs on the CPU while the GC and Thread Pool work alongside it the entire time:
The GC manages memory in three generations. Gen0 collects short-lived objects (most die here). Gen1 is a buffer. Gen2 holds long-lived objects and is expensive to collect. If your app has Gen2 collection spikes, that's where your latency problems come from.
The Thread Pool manages all your async work. Every time you await something, the Thread Pool decides which thread picks it up next.
๐ช๐ต๐ ๐๐ต๐ถ๐ ๐บ๐ฎ๐๐๐ฒ๐ฟ๐
Understanding this pipeline is the difference between guessing why your app is slow and knowing exactly where to look. Startup slow? Check JIT and assembly loading. Random latency spikes? Check GC Gen2 collections. High memory? Check object lifetimes.