Search This Blog

Wednesday, December 8, 2010

Native Image Generator (Ngen.exe)

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 using the just-in-time (JIT) compiler to compile the original assembly.
Ngen.exe has changed significantly in the .NET Framework version 2.0:
·         Installing an assembly also installs its dependencies, simplifying the syntax of Ngen.exe.
·         Native images can now be shared across application domains.
·         A new action, update, re-creates images that have been invalidated.
·         Actions can be deferred for execution by a service that uses idle time on the computer to generate and install images.
·         Some causes of image invalidation have been eliminated.
Examples:

Creating & Installing Image:
a)     The following command generates a native image for ClientApp.exe, located in the current directory, and installs the image in the native image cache. If a configuration file exists for the assembly, Ngen.exe uses it. In addition, native images are generated for any .dll files that ClientApp.exe references.

                         ngen install ClientApp.exe

   An image installed with Ngen.exe is also called a root. A root can be an application or a shared component.
b)     The following command generates a native image for MyAssembly.exe with the specified path.
                         ngen install c:\myfiles\MyAssembly.exe
 
c)     An assembly can have a dependency without a reference, for example if it loads a .dll file 
by using the System.Reflection.Assembly.Load () method. You can create a native image 
for such a .dll file by using configuration information for the application assembly, with 
the /ExeConfig option. 
 
    The following command generates a native image for MyLib.dll, using the configuration information from MyApp.exe.
 
                         ngen install c:\myfiles\MyLib.dll /ExeConfig:c:\myapps\MyApp.exe
 
Assemblies installed in this way are not removed when the application is removed.
  
     To uninstall a dependency, use the same command-line options that were used to install it. 
The following command uninstalls the MyLib.dll from the previous example.
 
                                         ngen uninstall c:\myfiles\MyLib.dll /ExeConfig:c:\myapps\MyApp.exe
 
d)     To create a native image for an assembly in the global assembly cache, use the 
display name of the assembly. For example:
 
ngen install "ClientApp, Version=1.0.0.0, Culture=neutral, 
  PublicKeyToken=3c7ba247adcd2081, processorArchitecture=MSIL"
     NGen.exe generates a separate set of images for each scenario you install.
    For example, the following commands install a complete set of native images for normal operation, another complete set for debugging, and a third for profiling:
ngen install MyApp.exe
ngen install MyApp.exe /debug
ngen install MyApp.exe /profile
 
e)     Displaying Native Image Cache: Once native images are installed in the cache, they can be displayed using Ngen.exe.
     The following command displays all native images in the native image cache.
        ngen display
    The display action lists all the root assemblies first, followed by a list of all the native images on the   computer.
f)      Use the simple name of an assembly to display information only for that assembly.
     The following command displays all native images in the native image cache that match the partial name MyAssembly, their dependencies, and all roots that have a dependency on MyAssembly:
                                ngen display MyAssembly
    Knowing what roots depend on a shared component assembly is useful in gauging the impact of an update action after the shared component is upgraded.
     If you specify an assembly's file extension, you must either specify the path or execute Ngen.exe from the directory containing the assembly:
                                        ngen display c:\myApps\MyAssembly.exe
g)     The following command displays all native images in the native image cache with the name MyAssembly and the version 1.0.0.0.
                         ngen display "MyAssembly, version=1.0.0.0"

 

Updating Images

a)     Images are typically updated after a shared component has been upgraded. To update all native images that have changed, or whose dependencies have changed, use the update action with no arguments.
                                        ngen update
 
b)     Updating all images can be a lengthy process. You can queue the updates for
 execution by the native image service by using the /queue option. For more 
information on the /queue option and installation priorities
 
                         ngen update /queue
 

 Uninstalling Images

a)     Ngen.exe maintains a list of dependencies, so that shared components are removed only
When all assemblies that depend on them have been removed.
     In addition, a shared component is not removed if it has been installed as a root.
The following command uninstalls all scenarios for the root ClientApp.exe:
                                ngen uninstall ClientApp
 
b) The uninstall action can be used to remove specific scenarios. 
 
                     The following command uninstalls all debug scenarios for ClientApp.exe:
 
                    ngen uninstall ClientApp /debug
 
c)     The following command uninstalls all scenarios for a specific version of ClientApp.exe:
 
                    ngen uninstall "ClientApp, Version=1.0.0.0"
 
d)     The following commands uninstall all scenarios for "ClientApp, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=3c7ba247adcd2081, processorArchitecture=MSIL"
, or just the debug scenario for that assembly:
 
ngen uninstall "ClientApp, Version=1.0.0.0, Culture=neutral, 
  PublicKeyToken=3c7ba247adcd2081, processorArchitecture=MSIL"
 
ngen uninstall "ClientApp, Version=1.0.0.0, Culture=neutral, 
  PublicKeyToken=3c7ba247adcd2081, processorArchitecture=MSIL" /debug
 
As with the install action, supplying an extension requires either executing Ngen.exe
 from the directory containing the assembly or specifying a full path.

No comments:

Post a Comment