Native image builder creation hostingProgramSet, and install the image to the local computer's local image cache. The local image cache is the reserved region of the Global Assembly Cache. Once you create a local image for an assembly, the runtime automatically uses the local image every time you run the assembly. You can use a local image for the runtime without executing any other process. Running ngen.exe on an assembly can speed up assembly loading and execution because it can be restored from the local image cache.CodeInstead of generating them dynamically.
Ngen [Options] [assemblyname | assemblypath] parameter description
Assemblyname is the name of the Assembly for the production cost machine image. The Assembly must be in the current directory. You can provide some specified Assembly names (such as myassembly) or fully specified Assembly names (such as myassembly, version = 2.0.0.0, culture = neutral, publickeytoken = 0038abc9deabfle5 ). If you want ngen.exe to locate and use the publisher policy file of the assembly, you must use the fully-specified Assembly name.
Assemblypath is the explicit path of the Assembly for the production cost machine image. You can specify the full path (such as c: \ applications \ MyApp \ myapp.exe), relative path (such as .. \ applications \ MyApp \ myapp.exe), or file name (such as myapp.exe) of the Assembly ).
If you specify a file name (such as myapp.exe) without specifying a full or relative path, the Assembly must be in the current directory.
To enable ngen.exe to recognize an assembly as an executable file and find its configuration file, use the assemblypath parameter to specify an assembly with the. exe extension.
If you specify more than one assembly on the command line, only one of them is an executable file. This tool applies the binding attributes of executable files (Application basics and any configuration files) to other specified assemblies.
Option description
/Debug generates a local image that will be used by the debugger in normal debugging mode.
/Debugopt generates a local image that will be used by the debugger in the optimized debugging mode of the Common Language Runtime Library. For more information about how to activate this mode, see your debugger documentation.
/Delete [assemblyname | assemblypath |
*]
Delete the local image of assemblyname or assemblypath specified in the local image cache. If the "*" parameter is specified, the tool deletes all local images in the local image cache. If you use the/delete option without specifying a parameter, the tool displays an error message.
When a. NET Framework version is uninstalled, the uninstall process uses the/delete option to delete all local images of the. NET Framework version that is uninstalled. This includes the local image created for the. NET Framework Assembly during installation and any local image created for the custom assembly. If both the/show option and/delete * option are specified, the tool displays the list of local images it deletes.
When multiple. NET Framework versions are installed on the same computer at the same time, you must use the same ngen.exe version used to create a local image to delete the local image.
Note that this option only affects ngen.exe-generated local images and does not affect the actual assembly.
/Help displays the command syntax and options of the tool.
/Nologo undisplay Microsoft startup title.
/Prof generates a local image that will be used by the analyzer using the standard code. See your analyzer documentation to determine whether your analyzer requires standard code.
/Show indicates the specified assemblyname or assemblypath to display the current files in the local image cache. If no parameter is specified, the tool displays all content in the local image cache. This option displays the Assembly definition information of the source set and any special code configuration options for each local image.
If this option is specified with the/delete * option, the tool displays the list of local images it deletes.
/Showversion: displays the runtime version used by ngen.exe to generate the local image of the specified assembly. When multiple. NET Framework versions are installed on the same computer at the same time, you can use this option to determine the version that the tool will use. For more information about running multiple database versions, see parallel execution.
Note that this option does not generate a local image.
/Silent cancel display successful message.
/? Displays the command syntax and options of the tool.
Remarks
Ngen.exe does not use standard assembly detection rules to locate the Assembly you specified on the command line. Ngen.exe only searches for the specified assembly in the current directory. Therefore, to enable ngen.exe to locate the Assembly, set the working directory to the directory where the Assembly for which the local image is to be created, or specify the exact path of the Assembly.
A local image is a file that contains compiled processor-specific machine code. Ngen.exe-generated local images cannot be shared between application domains. Therefore, ngen.exe cannot be used in an application scheme (such as ASP. NET) that requires an assembly to be shared between application domains.
Ngen.exe pre-compiled assembly can reduce the startup time of the application, because much of the work required to execute the code has been done in advance. Therefore, if you have determined that the cause of performance degradation is the CPU cycle consumed by JIT compilation in the client application, you 'd better use ngen.exe for the client application.
Because many factors affect the startup time of the application, you should carefully determine which applications will benefit from using ngen.exe. To this end, the following experiment should be performed: the JIT and pre-compiled versions of the Assembly will be run in the environment where the candidate assembly will be used. This allows you to compare the startup time of the same assembly in different compilation schemes.
Note: To Run ngen.exe, you must have administrator privileges.
After you generate a local image for an assembly, the local image is automatically located and used whenever the Runtime Library runs the assembly. For example, if you run an assembly in a debugging or analysis scheme, the runtime looks for the local image generated by the/debug,/debugopt, or/Prof options. If the runtime cannot find a matching local image, it will be restored to standard JIT compilation.
If you run ngen.exe on an assembly with the debugable code attribute, the tool automatically generates code based on the flag of this attribute, just as the/debug or/debugopt option has been specified.
If ngen.exe encounters any methods that it cannot generate in the Assembly, it will exclude these methods from the local image. When the Runtime Library executes this Assembly, It redirects to JIT compilation for methods not included in the local image.
When you use ngen.exe to create a local image of an assembly, the output depends on the command line options you specify and some settings on the computer. These settings include:
. NET Framework version.
CPU type.
The version of the operating system.
The exact identifier of the Assembly (re-compilation will change the identifier ).
The exact identifier of all the Assemblies referenced by the Assembly (the identifier is changed when the Assembly is recompiled ).
Security factors.
Ngen.exe records this information when generating a local image. When you run the assembly, the runtime will look for the options that match the current environment of the computer and set the generated local image. If the runtime does not find a matching local image, it will be restored to the JIT compilation of the Assembly. The following changes to the computer settings and environment will invalidate the local image:
. NET Framework version.
If a patch, QFE, or update is applied to the. NET Framework, all local images manually created using ngen.exe will become invalid. These assemblies can still run, but the runtime does not load the corresponding local image of the Assembly. You must manually create a new local image for these sets.
. NET Framework automatically creates a new local image for the. NET Framework library it has installed.
CPU type.
If you upgrade your computer's processor to a new processor series, all local images stored in the local image cache will become invalid.
The version of the operating system.
If the version of the operating system running on the computer changes, all local images stored in the local image cache will become invalid.
The exact identifier of the Assembly.
If you re-compile the assembly, the corresponding local image of the Assembly will become invalid.
The exact identifier of any Assembly referenced by the Assembly.
If you recompile any Assembly referenced by an assembly, the corresponding local image of the Assembly becomes invalid.
Security factors.
Change the computer security policy to restrict the permissions granted to an assembly. This will invalidate the locally compiled native image of the Assembly. Specifically, revoking any of the following permissions will invalidate the current local image of the Assembly:
The declarative inheritance permission required by the classes of the derived assembly.
The declarative link required by the method called by the Assembly.
Skipverification permission (if the Assembly contains any methods that cannot be verified ). For more information about this permission, see the securitypermissionattribute. skipverification attribute.
Unmanagedcode permission (if the Assembly performs any pinvoke call ). For more information about this permission, see the securitypermissionattribute. unmanagedcode attribute.
If ngen.exe is run on an assembly with code access security disabled, the ngen.exe native image generated by it becomes invalid when code access security is enabled. Note: by default, code access security is enabled.
For details about how to manage code access security and how to use permissions in the Common Language Runtime Library, see code access security
Note that invalid local images are not automatically created or deleted in the public Language Runtime Library version 1.0. You must use ngen.exe to manually create or delete all local images.
If ngen.exe is used to generate a local image of the application during installation, you must specify the application file name and the fully-specified Assembly name of the. dll file referenced by the application during compilation. Provides the fully-specified Assembly name for the DLL file referenced by the application to enable ngen.exe to access the issuer policy file of the referenced assembly. In the future, if you update the DLL file and use the issuer policy to redirect, ngen.exe will apply the issuer policy.
By running ildasm.exe on an application and viewing its assembly list, you can obtain the fully-specified Assembly name to be used. This list shows the Assembly name, version, culture, and public key tag of the DLL file referenced by the application during compilation. For example, if you want to create a local image for an application named clientapp.exe, the application uses the mylibrary whose version is 1.0.0.0, the region is not a specific language, and the Public Key is marked as 0038abc9deabfle5. run the ngen clientapp.exe "mylibrary, version = 1.0.0.0, culture = neutral, publickeytoken = 0038abc9deabfle5" command to compile the DLL file ".
Note that the preceding example does not generate a local image of the Assembly referenced by mylibrary. dll. To determine the exact name of the Assembly referenced by mylibrary. dll, run ildasm.exe on mylibrary. dll. For example. run ildasm.exe on the DLL, and make sure that it references the MySQL math whose version is 1.0.0.0, the region is not a specific language, and the Public Key is marked as 0039def8abcbste7. DLL. Use the following command to generate a local image for the directory tree referenced by the entire assembly of clientapp.exe.
Ngen clientapp.exe "mylibrary, version = 1.0.0.0, culture = neutral, publickeytoken = token", "mymath, version = 1.0.0.0, culture = neutral, publickeytoken = 0039def8abcbste7 ". for more information about this format, see the "example" section after this topic.
The uninstallation process of the application should use the/Delete [assemblyname | assemblypath] Option to remove the local image created when the application is installed. You must use the assemblyname or assemblypath parameter to specify the specific local image to be deleted. Specifying/delete * will remove all local images in the local image cache. specifying the/delete option without specifying the parameter will produce an error.
Example
The following command generates a local image for clientapp.exe in the current directory. If the application has a configuration file, ngen.exe will use it. This tool does not generate a local image for any DLL referenced by clientapp.exe.
Ngen clientapp.exe if clientapp.exe directly references two DLL (mylibone. dll and mylibtwo. dll), you must provide ngen.exe with a fully-specified Assembly name for these DLL to generate a local image for them. Run ildasm.exe on clientapp.exe to determine the name of the fully-specified assembly of the referenced DLL. In this example, mylibone. DLL and mylibtwo. the fully-specified DLL Assembly names are "mylibone, version = 1.0.0.0, culture = neutral, publickeytoken = Taobao" and "mylibtwo, version = 1.0.0.0, culture = neutral, publickeytoken = 0038abc9deabfle5 ". Use the following command to generate a local image of clientapp.exe, mylibone. dll, and mylibtwo. dll. If clientapp.exe has a configuration file, ngen.exe uses it. If mylibone. dll or mylibtwo. dll has a publisher policy file, ngen.exe uses it.
Ngen clientapp.exe "mylibone, version = 1.0.0.0, culture = neutral, publickeytoken = token", "mylibtwo, version = 1.0.0.0, culture = neutral, publickeytoken = 0038abc9deabfle5" in the preceding example, DLL file mylibone. DLL and mylibtwo. dll can reference other assembly. To determine the fully-specified Assembly name of the referenced assembly, run ildasm.exe on mylibone. dll and mylibtwo. dll. In this example, if mylibone. dll does not reference any other assembly, mylibtwo. dll references "mymath, version = 1.0.0.0, culture = neutral, publickeytoken = 0039def8abcbste7 ". With this information, the following command generates a local image for the entire Assembly reference directory tree of the application.
Ngen clientapp.exe "mylibone, version = 1.0.0.0, culture = neutral, publickeytoken = Break", "mymath, version = 1.0.0.0, culture = neutral, publickeytoken = Break", "mylibtwo, version = 1.0.0.0, culture = neutral, publickeytoken = 0038abc9deabfle5 "command to generate a local image of myassembly.exe with the specified path.
The commands under ngen c: \ myfiles \ myassembly.exe generate a local image of mylibrary. dll in the specified path.
Ngen c: \ myfiles \ mylibrary.dllngen.exe searches for the local image cache and deletes the Assembly specified by some assembly names. The following command deletes all local images with the myassembly name.
The commands under ngen/delete myassembly Delete the local image myassembly with the fully specified Assembly name.
Ngen/Delete "myassembly, version = 1.0.0.0, culture = neutral, publickeytoken = 0038abc9deabfle5" the command below shows all local images in the local image cache.
The commands below ngen/show all local images named myassembly in the local image cache.
The commands under ngen/show myassembly show all local images named myassembly and Version 1.0 in the local image cache.
Ngen/Show "myassembly, version = 1.0.0.0"
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/shootero/articles/571337.aspx