Click here to Skip to main content
11,642,402 members (63,243 online)
Click here to Skip to main content

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

, 18 Jan 2006 51.6K 26
Rate this:
Please Sign up or sign in to vote.
Ngen.exe -- A discussion -- Some Queries (.NET Framework Tools Series)
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

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

Share

About the Author

Namratha Shah
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.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
JayantiChauhan2-Apr-13 2:32
memberJayantiChauhan2-Apr-13 2:32 
GeneralDo I need to run ngen on all exe and assembly(dlls) one by one Pin
sakshismriti27-Jul-09 21:03
membersakshismriti27-Jul-09 21:03 
QuestionHow to actually run the app after an Ngen assembly has been created Pin
w.hooper2-Jul-09 21:52
memberw.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
memberMichael Herman3-Jun-09 18:52 
GeneralOS Cloning Pin
Member 136607226-Mar-09 9:31
memberMember 136607226-Mar-09 9:31 
GeneralNgen Installer/Convenience class Pin
xjs720-Mar-08 13:49
memberxjs720-Mar-08 13:49 
GeneralError! Pin
SalarSoft21-Dec-06 0:27
memberSalarSoft21-Dec-06 0:27 
GeneralNgen Path Pin
AndrewVos27-Jul-06 1:29
memberAndrewVos27-Jul-06 1:29 
GeneralGood article.. Pin
Invincible Poison21-Feb-06 19:26
memberInvincible Poison21-Feb-06 19:26 
GeneralRe: Good article.. Pin
Nacereddine15-Dec-12 3:36
memberNacereddine15-Dec-12 3:36 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 18 Jan 2006
Article Copyright 2006 by Namratha Shah
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid