Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

NGEN - When Your Need Is Speed - A Framework Tool

By Dan Mabbutt, About.com

In the previous article in this series, I introduced ILDASM, the Intermediate Language Disassembler. In addition to a brief look at that, I also pointed out that to execute ILDASM from a Command Prompt, you should probably execute the Microsoft supplied batch file, SDKVARS.BAT to set the "Path" correctly.

I created a much simpler batch program in the default command prompt folder to quickly and conviently execute SDKVARS.BAT. On my system, the default folder is--
C:\Documents and Settings\Dan
The new batch program consists of the single command:

"C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\sdkvars.bat"

The .NET Framework tool we're going to look at today, NGEN, also needs this assist. If you get the message, "NGEN is not recognized as an internal or external command, operable program, or batch file", that's probably the problem.

NGEN is the Native Image Generator and it's used to create platform specific "native code" versions of your systems. To clearly understand what NGEN does, recall that the ILDASM utility runs against the Intermediate Language program produced by the VB.NET compiler.

"Intermediate Language" is an open, standard language that can be ported to any platform.
Yes! Open and Microsoft!
When Microsoft released the .NET platform, they also submitted a standards applications to the ECMA which described Intermediate Language. (The formal name is "CIL" - common intermediate language.)

To actually execute your program, the .NET runtime - the CLR - has to compile it to native code just before it's executed using the .NET Just-In-Time (JIT) compiler ... sometimes called the "Jitter". In some cases - primarily to speed execution by avoiding the JIT compile - programmers want a completely native code version of their program. NGEN creates one and saves it in a special area on your Windows machine.

NGEN is especially handy since you don't have to do a thing except execute it against the assembly (normally the .EXE or .DLL for your program) and the .NET runtime automatically takes care of using the NGEN'ed version. (Well .... with one major exception. The .NET runtime also keeps track of any dependencies for your assembly and if anything changes, the CLR runtime also automatically switches back to the IL version of your program.)

To get started using NGEN, run NGEN /? in a command prompt window just to make sure everything is working, and to see the parameters that NGEN accepts. You should get output that looks like this:

============================================

Microsoft (R) CLR Native Image Generator - Version 1.1.4322.573
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.

Usage: ngen [options] <assembly path or display name> ...

Administrative options:
   /show    Show existing native images (show all if no args)
   /delete    Delete an existing native image (use * to delete all)
   /showversion    Displays the version of the runtime that would be used
   to generate the image (it does not actually create the image)

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

============================================

I created a test program to demonstrate NGEN called - appropriately - testNGEN. I changed the version numbers for the program in Visual Studio to 4.3.2.1 make clearly show how that works too. In order for NGEN to find your program assembly, change to the directory that the assembly is in first. Remember that the single-file assembly is saved in the BIN directory. (This will change in the next version of the .NET Framework.) When your command prompt window is in the BIN directory, create a native code version of your program and with the command:

NGEN testNGEN

The output of the command is:

============================================

Microsoft (R) CLR Native Image Generator - Version 1.1.4322.573
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.
TestNGEN, Version=4.3.2.1, Culture=neutral, PublicKeyToken=null

============================================

Explore Visual Basic

By Category

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. NGEN - When Your Need Is Speed - A Framework Tool

©2009 About.com, a part of The New York Times Company.

All rights reserved.