Click here to Skip to main content
15,893,486 members
Articles / Web Development / ASP.NET
Article

Ngen.exe -- A discussion -- Some Queries (.NET Framework Tools Series)

Rate me:
Please Sign up or sign in to vote.
2.77/5 (11 votes)
18 Jan 20064 min read 71.9K   26   10
Ngen.exe -- A discussion -- Some Queries (.NET Framework Tools Series)

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 :

<drive>:\WINNT\Microsoft.NET\Framework\<version>\ngen.exe
Lets us check out all the options of Native Image Gernerator.

1) Go to the Visual Studio command prompt and type

Ngen.exe /help // this will show us all options

2) You will see the below details

Administrative options:
/show Show existing native images (show all if no args)
/delete Delete existing native images (delete all if no args)
Developer options:
/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
Miscellaneous options:
/? 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 /show
To delete an assembly from the cache
ngen /delete <assembly name>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
United States United States
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.

Comments and Discussions

 
GeneralMy vote of 4 Pin
JayantiChauhan2-Apr-13 2:32
JayantiChauhan2-Apr-13 2:32 
GeneralDo I need to run ngen on all exe and assembly(dlls) one by one Pin
Sakshi Smriti27-Jul-09 21:03
Sakshi Smriti27-Jul-09 21:03 
QuestionHow to actually run the app after an Ngen assembly has been created Pin
w.hooper2-Jul-09 21:52
w.hooper2-Jul-09 21:52 
GeneralError in reason for why you can't use NGen on ASP.NET Pin
Michael Herman3-Jun-09 18:52
Michael Herman3-Jun-09 18:52 
GeneralOS Cloning Pin
Angshuman_Agarwal26-Mar-09 9:31
Angshuman_Agarwal26-Mar-09 9:31 
GeneralNgen Installer/Convenience class Pin
Samuel Allen20-Mar-08 13:49
Samuel Allen20-Mar-08 13:49 
GeneralError! Pin
SalarSoft21-Dec-06 0:27
SalarSoft21-Dec-06 0:27 
GeneralNgen Path Pin
AndrewVos27-Jul-06 1:29
AndrewVos27-Jul-06 1:29 
How would I determine the path of ngen.exe when deploying using a third party installation tool?

Is the version number constant in .NET 2?

Thanks for the great article BTW




GeneralGood article.. Pin
Amit Kukreti (A)21-Feb-06 19:26
Amit Kukreti (A)21-Feb-06 19:26 
GeneralRe: Good article.. Pin
Nacereddine15-Dec-12 3:36
professionalNacereddine15-Dec-12 3:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.