skip to content

IL, JIT and AOT

What happens between C# source and machine code: IL, tiered JIT, and the AOT options that trade flexibility for startup time. Interviewers ask about JIT versus Native AOT.

on this pageshow

explore

questions

6

In .NET, what does the C# compiler put into an assembly, and when does the JIT turn a method into machine code?

level: juniorimportance: must knowfreq 65%

answer

  1. not machine code yet
  2. instructions plus self-description
  3. types, signatures, references
  4. a stub per method
  5. first call, this process only

basics

~20 s

The C# compiler emits IL, a CPU-independent instruction set, plus metadata describing every type, member signature and reference. At run time the JIT compiles each method to native code on its first call, and later calls in that process reuse it.

solid answer

~50 s

The C# compiler does not produce machine code. A .NET assembly is a PE file holding **IL** (intermediate language), a stack-based, CPU-independent instruction set, and **metadata** tables describing the types, their members' signatures and every member the code references. The runtime reads that metadata to load types and lay out objects, and reflection reads the same tables. Compilation to native code is lazy: when a type is loaded each method sits behind a stub, and the first call hands control to the JIT, which compiles that method's IL for the current CPU and repoints the stub, so later calls run the native code directly. The result lives only in that process's memory, so every start pays for JIT again unless the code was precompiled with ReadyToRun or Native AOT. On .NET 10 that first compile is usually a quick, lightly optimized tier that tiered compilation later replaces for hot methods.

code

csharp · 24 lines
csharp
#:property PublishAot=false
using System.Reflection;
using System.Runtime;
using System.Runtime.CompilerServices;

MethodInfo add = typeof(Calc).GetMethod(nameof(Calc.Add))!;
byte[] il = add.GetMethodBody()!.GetILAsByteArray()!;
Console.WriteLine($"Add IL: {BitConverter.ToString(il)}");

long before = JitInfo.GetCompiledMethodCount(currentThread: true);
int r = Calc.Add(2, 3);          // first call: the JIT compiles Calc.Add now
long after = JitInfo.GetCompiledMethodCount(currentThread: true);
Console.WriteLine($"result {r}, methods jitted by first call: {after - before}");

before = JitInfo.GetCompiledMethodCount(currentThread: true);
r = Calc.Add(4, 5);              // second call: reuses the native code
after = JitInfo.GetCompiledMethodCount(currentThread: true);
Console.WriteLine($"result {r}, methods jitted by second call: {after - before}");

static class Calc
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    public static int Add(int a, int b) => a + b;
}

go deeper

for a junior

Say the two halves of an assembly, IL and metadata, and that the JIT compiles a method the first time it is called. Mention that the result is reused for later calls in the same process.

for a middle

Explain the stub that sends the first call to the JIT and is then repointed, why uncalled methods cost nothing, and why every new process pays the JIT again.

for a senior

Connect lazy JIT to startup symptoms: the first request through new code paths pays for compilation, and ReadyToRun or Native AOT are the levers that move that work to build time.

for a principal

Frame IL as the portability contract: one build runs on every supported CPU, and choosing to precompile trades that portability and some dynamism for startup, per runtime identifier.

## What the C# compiler actually produces When you build a C# project, the compiler (Roslyn) does not emit instructions for an x64 or Arm64 processor. It writes an **assembly**: a file in the Portable Executable (PE) format that holds two things the runtime needs. - **IL (intermediate language)**, also called CIL: a CPU-independent instruction set for a stack machine. It has instructions for loading and storing values, calling methods, creating objects, arithmetic, branching and exception handling. - **Metadata**: tables that describe every type defined in the assembly, the signature of each member, the types and members the code references in other assemblies, and attributes. Because the metadata describes the code completely, an assembly is **self-describing**: no header files or separate interface definitions are needed to call into it. ## IL: a portable stack machine IL operates on an evaluation stack rather than CPU registers. A method `static int Add(int a, int b) => a + b;` compiles to four IL instructions, which `MethodInfo.GetMethodBody().GetILAsByteArray()` returns as the bytes `02 03 58 2A`: ``` ldarg.0 // push a ldarg.1 // push b add // pop both, push the sum ret // return the top of the stack ``` The same IL runs on any architecture the runtime supports, because each runtime ships a JIT for its own CPU. An assembly that contains only IL and calls no platform-specific native libraries can run unchanged on Windows x64, Linux Arm64 or macOS. ## Metadata: what the runtime and tools read The runtime consults metadata constantly: - to **load a type**: find its fields, compute the object layout and build its method table; - to **resolve a call**: a call instruction in IL carries a metadata token that names the target method, which the runtime resolves to a concrete method, possibly in another assembly; - to **check references**: an assembly lists the assemblies it depends on, and the loader binds them; - for **reflection**: `Type.GetMethods()`, attributes and `GetCustomAttribute` read the same tables at run time. `System.Reflection.Metadata` can read these tables directly from a file without loading the assembly for execution. ## When the JIT runs JIT compilation is **lazy and per method**: 1. When a type is loaded, each of its methods starts behind a small stub rather than native code. 2. The first call to a method goes through that stub to the JIT compiler. 3. The JIT compiles that method's IL to native code for the current CPU and stores it in the process's memory. 4. The stub is repointed, so the second and later calls jump straight to the native code. Methods that are never called are never compiled, which saves time and memory. On .NET 10, tiered compilation means the first compile is normally a quick, lightly optimized version, and methods that turn out to be hot are recompiled with full optimization in the background later. `System.Runtime.JitInfo.GetCompiledMethodCount()` lets you watch the count go up by one on a first call and stay flat on the second. ## What it costs, and what changes it JIT output is bound to the process that produced it. It is not written to disk, so a second run of the same app compiles again. That is the startup cost ahead-of-time options address: | Model | What ships | JIT at run time | |---|---|---| | Default JIT | IL + metadata | Every method on first call | | ReadyToRun | IL + metadata + precompiled native code | Less at startup; hot methods still re-jitted | | Native AOT | A native executable, no IL to compile | None | ## Where tiering and precompilation fit The lazy, per-method model stays the same when the runtime gets smarter about it: - **Tiered compilation** changes *how well* a method is compiled on its first call (quickly at first, optimized later for hot methods), not *when* compilation is triggered. - **ReadyToRun** puts native code for many methods into the assembly at publish, so their first call binds that code instead of invoking the JIT; the IL stays for anything that still needs it. - **Native AOT** compiles everything at publish into one native executable, so there is nothing left for a JIT to do and the IL is not shipped for execution. ## Common misconceptions - **"The compiler targets my CPU."** Only the JIT or an AOT compiler produces machine code; the C# compiler stops at IL. - **"The whole assembly is compiled at load."** Compilation happens method by method, on first call. - **"Metadata is debug information."** It is required to run the code; debug symbols live separately in the `.pdb` file. - **"IL is interpreted on every call."** On the default CoreCLR path each method is compiled once per process (and possibly recompiled at a higher tier), then runs as native code.

  • How can you look at the IL and metadata of a .NET assembly from code?
    `MethodInfo.GetMethodBody().GetILAsByteArray()` returns a method's raw IL bytes from a loaded assembly, and reflection (`Type.GetMethods()`, `GetCustomAttributes()`) reads the metadata tables. To inspect a file without loading it for execution, `System.Reflection.Metadata`'s `PEReader` and `MetadataReader` read the same tables directly. IL disassemblers show the same content as text.
  • Can one IL-only .NET assembly run on both x64 and Arm64 machines?
    Yes, when it contains only IL and depends on no platform-specific native library: each runtime's JIT compiles the IL for its own CPU. ReadyToRun and Native AOT output is different, because it carries native code for one runtime identifier, so you build one per target platform.
  • Does the JIT's output survive a process restart?
    No. JIT-compiled code lives in the process's memory and is discarded when the process exits; the next run compiles again. Persisting native code is what ReadyToRun (native code inside the assembly, chosen at publish) and Native AOT (a fully native executable) are for.

IL is a recipe written in a notation every kitchen understands, and the JIT is the cook who prepares each dish the first time someone orders it, not the whole menu up front. The prepared dish lasts only for that shift: tomorrow's shift (the next process) cooks again.

  • The C# compiler produces machine code for the target CPU.
  • The JIT compiles the whole assembly as soon as it is loaded.
  • JIT-compiled code is cached on disk and reused by the next run.
  • Metadata is debug information that a Release build can drop.
  • IL is interpreted on every call, so .NET code never runs natively.
open as a page

In .NET 10, how does tiered compilation move a method from quick JIT to optimized code, and why are a service's first requests slower?

level: middleimportance: must knowfreq 55%

basics

~20 s

Methods start on quick tier-0 code or their ReadyToRun code; hot ones, after 30 calls once startup quiets, are recompiled in the background at tier 1 using dynamic PGO data. Early requests pay for JIT and run the slower first-tier code.

open as a page

For a .NET 10 command-line tool, what does ReadyToRun change at startup compared with Native AOT, and what does each give up?

level: middleimportance: should knowfreq 50%

basics

~20 s

ReadyToRun ships precompiled code beside the IL, so less is jitted at startup, but the JIT stays and re-jits hot methods. Native AOT has no JIT: faster startup, less memory, but trimming, per-platform binaries and no run-time code generation or assembly loading.

open as a page

A .NET desktop host loads customer plugins into a collectible AssemblyLoadContext and calls Unload, yet the plugins never leave memory; why, and how do you prove it?

level: seniorimportance: should knowfreq 28%

basics

~20 s

AssemblyLoadContext.Unload only starts a cooperative unload; the context is freed once nothing references it or anything loaded in it. A cached plugin object, Type, delegate, event handler or running thread keeps it alive; a WeakReference checked after forced collections proves it.

open as a page

As lead of a .NET 10 platform team, would you move a command-line deployment tool and a long-running pricing service to Native AOT, ReadyToRun or plain JIT, and why?

level: principalimportance: should knowfreq 35%

basics

~20 s

Decide per workload: the short-lived deployment tool suits Native AOT if its dependencies are AOT-clean, else ReadyToRun; the pricing service usually stays on the JIT, with ReadyToRun for cold starts, since tiered JIT with dynamic PGO usually wins at steady state.

open as a page

In .NET, what does [LibraryImport] generate at compile time that [DllImport] leaves to the runtime, and what must a project change to switch?

level: seniorimportance: nice to knowfreq 15%

basics

~20 s

[DllImport] has the runtime build and JIT-compile an IL marshalling stub at run time. [LibraryImport] (.NET 7) has a source generator emit that marshalling as C# at build time; methods become static partial and the project needs AllowUnsafeBlocks.

open as a page