I want to share the basics of the Ngen.exe in .Net. So first we try to understand the problem, first, then we will discuss Ngen.exe.
I have gathered this material from various web resources and am trying to explain it in simple terms.
when we run the .net application, code is JIT-compiled. The Machine code generated by JIT is thrown away after the task compilation.
So the same method must have complied again whenever we run the application again.
Compilation of the code, again and again, is a tedious process.
Native Image Generator (Ngen.exe)
It improves the startup time of the application by reducing the
JIT compilation process at runtime N-Gen refers to the process of precompiling (MSIL) executables into the machine code prior to the execution time.
Which in turn reduces the application startup time by avoiding the need to compile code at the run time again and again?
At a time only one NGen is run against an assembly, the resulting native image is placed into the Native Image Cache and when we run the application it uses the Native-cache instead of the recompilation of code.
Where the Ngen.exe creates native images, which are files containing compiled processor-specific machine codes, and installs them into the native image cache on the local computer.
The runtime can use these native images from the cache instead of using the just-in-time (JIT) compiler to compile the original assembly Ngen also allowed sharing
the same Native image cache with other application so it is helpful in terms of memory usage.
Native images are typically named such as "*.ni.dll", "*.ni.exe".
- The open VS command prompt
- Type ngen.exe [option] [assemblyname] [assemblypath]
- Once you have created the native image for an assembly, the runtime automatically uses that native image each time it runs the assembly
- Running Ngen.exe on an assembly allows the assembly to load and execute faster because it restores the code and the data structures from the native image cache rather than generating them dynamically.
We can improve the startup time of .Net applications using Native image generator