One of the topics we often get questions on is about when it makes sense to invest the extra effort to pre-compile assemblies via NGen instead of simply relying on the JIT compiler to generate native code on the fly at application runtime. I thought I would try to answer that question in our first "real" post.
The JIT is optimized to balance code generation time against code quality, and it works well for many scenarios. NGen is essentially a performance optimization; so just like for anything else that's performance-related, you'll want to measure performance with vs. without NGen, and then decide whether it really helps your specific application and scenario. Here are a few general guidelines.
When to use NGen:
When not to use NGen:
If it seems that your application is likely to benefit from NGen, here are a few things you might want to keep in mind:
Note: Currently you need admin privileges to issue commands via ngen.exe.
Finally, another important thing to keep in mind is that NGen isn't a magic solution for application start up time. NGen can speed up application start up by eliminating the time needed for JIT compiling the code executed at start up.
However, regardless of whether you do or don't use NGen, it is important to ensure that your application is architected to be performant. So for instance, if you care about start up time, you should try to minimize the amount of code that needs to get loaded and executed on your application's start up path.
Below are some good resources on measuring and optimizing application start up performance: http://blogs.msdn.com/vancem/archive/tags/Perf/default.aspx
http://msdn.microsoft.com/msdnmag/issues/06/02/CLRInsideOut
http://msdn.microsoft.com/msdnmag/issues/06/03/WindowsFormsPerformance/
And some for NGen:
NGen internals: http://msdn.microsoft.com/msdnmag/issues/05/04/NGen/default.aspx
NGen Service (NGen-ing your assemblies in the background):
http://blogs.msdn.com/davidnotario/ (David isn't on our team any longer)
Thanks for visiting our blog!