Abstract

As an application grows ever more complex, it is necessary to build an efficient and faster .NET application that requires special treatment of .NET assemblies into the Global Assembly Cache to attain faster execution. This article explains how to write and execute high-performance. NET-managed code by employing the Native Image Generator utility. The NGen.exe is a remarkable tool for increasing application performance but some of its disadvantages are also provided and this article provides recommended scenario guidelines in which it is best fitted.

JIT verse Native assembly

Assemblies store code in the MSIL format. When a method is invoked for the first time, the CLR Just-in-Time (JIT) compiler compiles that method into native machine code. The native code is stored in memory and directly used for any subsequent call to this method. In the JIT compilation mode, a method is slow when it is called for the first time because an additional step of compilation is involved, but any subsequent calls to that method will run as fast as the native code itself.

When you view the GAC, note that some assemblies have their type marked as a native image. That implies that these assemblies were precompiled in native code before they were installed in the native image cache. You too can create a native image for your assembly by using the Native Image Generator Tool, which is installed as a part of the .NET Framework SDK.

The NGen.exe (Native Image Generation)

The CLR doesn't interpret IL code. Rather, it uses a Just-in-Time (JIT) compiler to compile IL code into native machine code at run time. So it is clearly transpired that conversion of managed code to native code imposes some performance costs such as, during classes, library loading, and application start-up. In order to overcome such problems and to make retrieval more responsive, the CLR offers ahead-of-time JIT compilation using a technology called NGen.

The NGen.exe utility can be found in your framework directory. It enables you to do this ahead-of-time compilation on the client machine. The outcome of this operation is stored in a central location on the machine called the Native Image Cache. The loader typically knows to look here when loading an assembly or DLL that is signed by a strong name. All of the .NET Framework assemblies are NGen manipulated during the installation of the Framework itself.

NGen.exe Operation

NGen uses the same code generation tactics that the CLR JIT uses to generate native code. The code that is generated is designed to take advantage of the underlying computer architecture. An NGen image might be unusable due to subtle differences in chip capabilities. Thus, image generation must occur on the client machine as part of the installation rather than being done at coding time during deployment. The CLR notices this at load time and will fall back to the runtime JIT. If you apply an NGen operation to your exe file then NGen traverses your application dependencies, generates code for each, and stores the image in the native image cache alongside your program.

In version 4.0 or 4.5 of the framework, a new NGen Windows service has been integrated to take care of queuing and managing NGen applications in the background. This virtually implies that your program can install, add a request to the NGen queue, and then exit the installation. The NGen service will then take care of the compilation asynchronously.

The NGen tool has quite a few switches to control the behavior. Running NGen.exe at the command prompt will show detailed usage information for the tool as in the following.

NGen exe Operation

Here is the brief summary of significant switches for operating NGen.exe

Note. Be sure that the Visual Studio command prompt is being run under Administrative privileges to execute NGEN.exe.

NGen.exe Advantage and Disadvantage

Since the code is compiled at install time, the CLR's JIT compilation does not need to compile the IL code at run time and this can improve the application's performance. The ngen.exe tool is advantageous in the following scenarios.

Despite providing a couple of benefits of managed code, such as garbage collection, verification, and type safety, without hitting the performance problem, there are several potential issues addressed by an NGen file such as,

Significant Guidelines for NGen

Final Note

Due to all the issues related to an NGen file, you should be very cautious when considering the use of NGen.exe. For a client application, it might make sense to improve startup time if an assembly is used by multiple applications simultaneously. The CLR will not need to load the JIT compiler at all, reducing the working set even further. For server applications, NGen.exe makes no sense because only the first client request experiences a performance hit, future requests run at high speed.