Ngen.exe -- A discussion -- Some Queries (.NET Framework Tools Series)
Today we will discuss Ngen.exe and then there are some questions regarding Ngen which ahve always bothered me. I have managed to get answers to them .... please read thru and let me know your answers to them ... coz I am sure these questions must have bothered u also.
Lets start with the discussion ....
This tool is used to create a native Image from an .NET assembly and installs it into the native image cache on that computer. Since assembly image is present on the local machine cache loading of the assembly becomes faster because .NET reads data from the native image than generating them dynamically (JIT).
This tool is available at :
Lets us check out all the options of Native Image Gernerator.
1) Go to the Visual Studio command prompt and type
2) You will see the below details
/show Show existing native images (show all if no args)
/delete Delete existing native images (delete all if no args)
/debug Generate image which can be used under a debugger
/debugopt Generate image which can be used under a debugger in optimized debugging mode
/prof Generate image which can be used under a profiler
/? or /help Show this message
/nologo Prevents displaying of logo
/silent Prevents displaying of success messages
Usage : ngen [options] <assembly path or display name>
The native Image contains compiled processor-specific machine code.If you create an native image of an assembly then .NET
Runtime tries to look out for that assembly. If it is unable to find the assembly than it reverts back to JIT.
Incase the application is running in a debug mode than .NET runtime looks for a native image created with /debug or /debugopt option.
Note: The assembly image created by Ngen depends on the options that we specify and below computer settings like:
The CPU type.
The version of the operating system.
The exact identity of the assembly (recompilation changes identity).
The exact identity of all assemblies that the assembly references.
To view all the native Images installed on the a machine go to:
<drive>\<windows folder> ... i.e. WINNT\assembly
In the assembly folder (GAC) sort on type and you will find all the native Images installed on that machine.
There are few questions that I have always had in my mind regarding Ngen and the assembly images:
1) How long are these native assembly images valid ?
Since the native assembly image is machine specific its validity depends on all the above factors. Well the are number of scenarios when the native image can become invalid :
- .NET Framework updation causes all native images to become invalid.
- Computer CPU updation
- Any Changes made to the OS.
- Recompilation of an assembly to IL causes existing native images of that assembly to become invalid.
- Recompliation of a referenced assembly causes the native image to the refrencing assembly to become invalid.
- Change in machine security policy.
Hence if there is any change in the computer setting or environment then there are chances of the Native Image becoming invalid.
Note : Its just that the assembly native image has become invalid not the assembly. hence next time the assembly will be loaded with (JIT) its native image will not be loaded.
2) Can you create a native Image of ASP.NET assembly ... does this work ... is it possible ????
Native Image generator creates a local image of an .NET Assembly and installs that image in the native cache on the local machine. But when you create a Native Image of an ASP.NET assembly the .NET Runtime ignores that assembly and reverts back to JIT. This is because CLR cannot load native images in a shared application domain and all ASP.NET applications run in a shared application domain. Hence although the native image of the assembly will be created it will not be installed in the image cache.
3) How do I run Ngen as a part of Installation or deloyment of the application ?
To run Ngen as a part of your application deployment or setup create a custom action during installation which will run the ngen and create images for all your application assemblies. Similarly also create a custom action in you deployment project during uninstallation to remove them from the native cache.
These were questions that I had in my mind ... if u have any other let me know .... if u have more thought opinions about these questions pls post them.
Going ahead lets just explore a few options of Ngen.exe
To create a native assembly
ngen <Assembly name with its full path>You can also create a native images of one or more assemblies at the same time
ngen <assembly1> <assembly3> <assembly2> <assembly4>
To view all the assemblies in the native cache use the/show option
ngen /showTo delete an assembly from the cache
ngen /delete <assembly name>
Namratha Shah a.k.a. Nasha is from orginally from Bombay, India but currently residing NJ, USA. She has to her credit, a Bachelor’s Degree in Microbiology and Biotechnology and a Master's in Computer and Software Applications (1999-2001) from Somaiya College Bombay. She started her career with C and C++ and then moved on to Microsoft Technologies. She has over 7.5 years experience in software architecture, design and development. She is a Certified Scrum Master and a member of the CORE .NET Architecture team. She has been Awarded with Microsoft’s Prestigious Most Valuable Professional (MVP) twice consecutively in years 2005 and 2006 in Visual C#.NET for her outstanding contributions to the .NET community.