Does jitting happen every time a .NET application runs?

263 views Asked by At

When I install a .NET application, I understand that it will get jitted. My question is does this jitting happen everytime I will run this application or does it happen only the first time? If it happen only first time, where can I find jitted version of this application?

2

There are 2 answers

0
hatchet - done with SOverflow On BEST ANSWER

The assemblies are jitted piecemeal, as needed. Your question seems to imply that the whole application is jit compiled at once. That's not what happens. The parts that have been jitted at any particular moment in time are in memory.

JIT compilation takes into account the fact that some code might never get called during execution. Rather than using time and memory to convert all the MSIL in a portable executable (PE) file to native code, it converts the MSIL as needed during execution and stores the resulting native code in memory so that it is accessible for subsequent calls in the context of that process. The loader creates and attaches a stub to each method in a type when the type is loaded and initialized. When a method is called for the first time, the stub passes control to the JIT compiler, which converts the MSIL for that method into native code and modifies the stub to point directly to the generated native code. Subsequent calls to the JIT-compiled method therefore proceed directly to the native code.

Compiling MSIL to Native Code

It doesn't change the answer to your question, but there are some differences in how the JIT compilation happens with .NET 4.5. On multi-core processors, a thread will be tasked to compile methods before they are actually called.

0
ema On

It happens everytime you run it. There's a tool called ngen 1 that generates a binary executable version of the application but it's not used so often.

Ngen