Introduction
Native code generation occurs when the Just In Time (JIT) compiler compiles the assembly's MSIL code into native code for the local machine, immediately before the method executes the first time. This is temporary code stored in the memory that can be reclaimed by the OS at the end of the process. Native code is regenerated each time when the new process is started.
The Native Image Generator NGen.exe creates a native code image using the JIT compiler and stores it in the hard drive location. Whenever an application requires the assembly, the CLR loads this native image instead of the original assembly. Note that the CLR has information like which assembly has a native image and whether this image was created for debugging purposes.
A native image can improve memory use when the assembly is shared among the processes. NGen.exe can improve the startup performance of some applications as a shared component already loaded for subsequent applications.
Before Entity Framework 6.0, the core libraries of the EF runtime are the the part of .Net framework, and the native image is generated automatically. From Entity Framework version 6.0, all the EF runtime libraries are combined into an Entity Framework Nu-Get package (EntityFramework.dll and EntityFramework.SqlServer.dll), so we can generate a native image using NGen.exe.
How to use NGen.exe
Step 1. Run the Visual Studio Command prompt as an administrator.
Step 2. Change the current working directory to the location of assemblies for which to generate the native image
Step 3. Run the following command
ngen install <<assembly name>>
When to use
We can consider the following assembly to create a native image.
1. Base assembly of Entity Framework-EntityFramework.dll
An Entity Framework-based application executes code from the EntityFramework.dll assembly on the startup and when it first calls the database. Hence creating a native image of this assembly helps to improve startup performance.
2. Assembly provided by Entity Framework and used by our application
Startup performance improvements can be also done by creating the native image of those assemblies provided by the Entity Framework and used by our application. For example EntityFramework.SqlServer.dll.
3. Our application (project) assemblies and other dependencies
We can create a native image of our project assemblies and other third-party dependency assemblies to improve startup performance.
Ngen.exe and Development machine
The .NET JIT compiler will offer a good overall tradeoff for code that changes very frequently during the development. We can generate native images for the compiled dependencies like the Entity Framework runtime assembly (EntityFramework.dll) and reduce time by cutting a few seconds at the beginning of each execution in the development environment.
Normally the Entity Framework runtime assemblies are located in the Nuget package location for the solution. For example, if we use Entity Framework version 6.1.1 and targeting .Net framework 4.5, they are located under the "packages\EntityFramework.6.1.1\lib\net45" path.
To generate a native image on the deployment server we can use a third-party tool like the Wix toolkit or create a custom setup task to execute the ngen.exe command.
Verification
Native images are used by Entity Framework
Using the process explorer, we can verify whether our application uses a native image by looking at loaded assemblies. Normally assemblies that have native images have an extension like "ni.dll" and "ni.exe".
Once native images are installed in the cache, we can verify it by a display command. The following command displays information for a specific assembly
ngen display <<assembly name with path>>
Summary
Native images will help us improve the startup performance and sometimes reduce memory usage. This is not done in all scenarios and is not beneficial equally. NGen.exe allows us to create a native image of assemblies that are not a part of the Global Assembly Cache (GAC). Multiple applications can share the same image that uses the same Entity Framework version.