Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JIT vs NGen - what is the difference?

Tags:

.net

clr

jit

ngen

So when CLR runtime load a .NET assembly, it compiles it into machine native code. This process is called JITing. NGen is also the process of compiling .NET assembly into native code. I don't understand what is the difference between two?

like image 524
palm snow Avatar asked Apr 02 '11 18:04

palm snow


3 Answers

The difference is when they occur. The JIT compilation occurs while your program is running. NGen is a typically done at installation time of your program and happens before your program is run. One of the goals of NGen is to remove the JIT penalty from application start up.

like image 87
JaredPar Avatar answered Nov 01 '22 05:11

JaredPar


JIT is only done per-method; it doesn't JIT everything... Only the bits you need. Of course this has a small but measurable hit the first time into a method (plus generics etc). NGEN does this work up-front, but must be done on the same platform/architecture etc - essentially that machine. This also means adding it to the GAC etc, which may need higher access.

In many cases, JIT is fine, especially if the app is open for a long time (web servers, for example).

like image 16
Marc Gravell Avatar answered Nov 01 '22 05:11

Marc Gravell


One very major important difference that has yet to be mentioned is that the native cached images have 'shared code pages', which makes a huge difference in the memory footprint of applications running over Terminal Services or Citrix.

The critical thing to understand about NGEN is that whilst it compiles your code, it also marks the code pages as shareable, so that multiple instances of your application can share parts of the memory space used by the first instance. And that’s really useful if you’re running under Terminal Services.

http://blogs.msdn.com/b/morgan/archive/2009/03/07/developing-net-applications-for-deployment-on-terminal-services-or-citrix.aspx.

This has very important implications for applications being used by multiple users on a single machine as they share memory across processes. This can lead to very strange, difficult to reproduce behaviour and resource management problems if the image caches are not well maintained.

like image 9
Rabid Avatar answered Nov 01 '22 05:11

Rabid