What is Native Image Generator(Ngen.exe) in .Net?

As we know that, All .NET source code first is compiled to IL and then IL is converted to machine code. Before we can run IL code, it must be compiled to native code for the target machine architecture. .NET Framework provides two ways to perform this conversion:-

1)A .NET Framework just-in-time (JIT) compiler.
2)The .NET Framework Native Image Generator (Ngen.exe).

Just In Time Compiler :-  The JIT compiler is part of the Common Language Runtime (CLR). The CLR manages the execution of all .NET applications. In addition to JIT compilation at runtime, the CLR is also responsible for garbage collection, type safety and for exception handling.

There are two types of JIT compilers available in the latest .NET Framework version.:-

Normal JIT Compiler:- => Compile + Cache
This is the default compiler. It compiles only those methods that are called at runtime. These methods are compiled the first time they are called, and then they are stored in cache. When the same methods are called again, the compiled code from cache is used for execution.

Pre JIT Compiler:- Compilation is done at startup
It compiles the entire assembly before it has been used. Usually the compilation happens during application deployment. You can pre-compile an assembly using Ngen.exe tool (Native Image Generator).

.NET Framework 1.0 and 1.1 had the third JIT Compiler – Econo-JIT. It was a runtime compiler which hadn’t done any optimization and removed methods’ machine code when it is not required. It was done to improve compilation time. The compiler is obsolete since .NET 2.0 and you cannot enable it anymore.
Ngen.exe (Native Image Generator) :-

The Native Image Generator (Ngen.exe) is a tool that improves the performance of managed applications.
Ngen.exe creates native images, which are files containing compiled processor-specific machine code, and installs them into the native image cache on the local computer. The runtime can use native images from the cache instead of using the just-in-time (JIT) compiler to compile the original assembly.

Below are some points to be remembered for Native Image Generator:-

=> Native images load faster than MSIL because JIT compilation and type-safety verifications is eliminated.

=> If you are sharing code between process Ngen.exe improves the performance significantly. As Native image generated Windows PE file so a single DLL file can be shared across applications. By contrast JIT produced code are private to an assembly and cannot be shared.

=> Native images enable code sharing between processes.

=> Native images require more storage space and more time to generate.

=>Startup time performance improves lot. We can get considerable gains when applications share component assemblies because after the first application has been started the shared components are already loaded for subsequent applications. If assemblies in an application must be loaded from the hard disk, does not benefit as much from native images because the hard disk access time shadows everything.

=> Assemblies in GAC do not benefit from Native image generator as the loader performs extra validation on the strong named assemblies thus shadowing the  benefits of Native Image Generator.

=> If any of the assemblies change then Native image should also be updated.

=>You should have administrative privilege for running Ngen.exe.

=> While this can fasten, your application startup times as the code is statically compiled but it can be somewhat slower than the code generated dynamically by the JIT compiler. Therefore, you need to compare how the whole application performance
with Ngen.exe and with out it.

=>To run Ngen.exe, use the following command line

      ngen.exe install <assemblyname>

=>This will synchronously precompile the specified assembly and all of its dependencies. The generated native images are stored in the native image cache.

=>You can uninstall an assembly and its dependencies (if no other assemblies are dependent on them) from the native image cache by running the following command.

ngen.exe uninstall <assemblyname>

=>Native images created using Ngen.exe cannot be deployed; instead, they need to be created on the end user’s machine. These commands therefore need to be issued as part of the application’s setup program. Visual Studio .NET can be used to implement this behavior by defining custom actions in a Microsoft Installer (MSI) package.

Note:- One of the things the interviewer will expect to be answered is what scenario will use a Native Image generator. Best is to say that we first need to test the application performance with Native Image and with out it and then make a decision. If we see that we have considerable performance difference we can then use native image generator.

Reference :- http://msdn.microsoft.com/en-us/library/6t9t5wcf(v=vs.110).aspx

Programming is Easy…