How do I prevent NGEN from rebasing my code (negatively affecting performance)?

Posted on

Problem :

I simply want to speed up my .NET-base client side app and am considering NGEN-ing the code.

Jeffery Richter wrote this warning about ngening code:

•Inferior Load-Time Performance
(Rebasing). When Windows loads an
NGend file, it checks to see if the
file loads at its preferred base
address. If the file cant load at its
preferred base address, then Windows
relocates the file, fixing-up all of
the memory address references. This is
extremely time consuming because
Windows must load the entire file into
memory and modify various bytes within
the file. For more information about
rebasing please see my book:
Programming Applications for Microsoft
Windows, 4th Edition (Microsoft

Since I don’t know much about this topic, what should I know before I start changing settings within my project, and what settings should I change?

Solution :

According to this Microsoft blog, “There may be some marginal cases where setting base addresses in Vista+ OSes has a benefit, but these can be largely ignored.” So, while using ngen does improve startup times, it is no longer necessary to set the base addresses unless you support pre-Vista OSes. This is a side-effect of the new Address Space Layout Randomization security feature.

The relocating of your DLLs only occurs at load time, once loaded there are no further performance hits due to the relocation process. Of course depending on the number and size of the DLLs (the number of relocations) the load time can be significantly impacted, which is a problem is your application is frequently started and stopped.

Rebasing DLLs to improve load times requires continous monitoring and tuning, if you have not left enough head room between DLL load locations you end up with collisions as the DLLs grow or new DLLs get added to the project.

Here is an MSDN article discussion ways to improve application startup time.

NGEN allows you to specify a base address (also exposed in VS settings). If you’re going to NGEN, you basically want to ensure you don’t have any overlap between DLLs. If you have overlap, then the CLR will be forced to rebase them when they’re loaded.

Leave a Reply

Your email address will not be published. Required fields are marked *