Thursday, October 07, 2004

Using the Native Image Generator (Ngen.exe) tool, an assembly can be converted into its Native Code or image. This means that calls to the Native Image will load faster since the nend for JIT compilation has been eliminated.When you run the NGen .NET Framework command line utility on an assembly, the Native Image will be generated and installed in the Native Image Cache and subsequent calls to methods of that assembly will be handled by the Native Image of the assembly. E.g. entering Ngen C:/post.dll at the command prompt where post.dll represents a managed assembly will create the Native Image of the assembly.A native image is a file containing compiled processor-specific machine code. Note that the native image that Ngen.exe generates cannot be shared across Application Domains. Therefore, you cannot use Ngen.exe in application scenarios, such as ASP.NET, that require assemblies to be shared across application domains.If Ngen.exe encounters any methods in an assembly that it cannot generate, it excludes them from the native image. When the runtime executes this assembly, it will revert to JIT compilation for the methods that were not included in the native image.

Examples:

The following command generates a native image for drnotes.exe, located in the current directory. If a configuration file exists for the application, Ngen.exe will use it. The tool will not generate native images for any DLLs that drnotes.exe references.

ngen drnotes.exe

If drnotes.exe directly references two DLLs, drnotes1.dll and drnotes2.dll, you must supply Ngen.exe with the fully specified assembly names for these DLLs to generate native images for them. Run Ildasm.exe over drnotes.exe to determine the fully specified assembly names of the referenced DLLs. For the purpose of this example, the fully specified assembly names of drnotes1.dll and drnotes2.dll are "drnotes1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0038abc9deabfle5" and "drnotes2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0038abc9deabfle5". Using this information, the following command generates native images for drnotes.exe, drnotes1.dll, and drnotes2.dll.