What does a JIT compiler specifically do as opposed to a non-JIT compiler? Can someone give a succinct and easy to understand description?

- 455
- 1
- 6
- 19

- 34,228
- 15
- 86
- 149
-
1http://www.ibm.com/support/knowledgecenter/SSYKE2_7.1.0/com.ibm.java.aix.71.doc/diag/understanding/jit_overview.html – TechDog Nov 08 '16 at 06:57
-
2[Updated Link](http://www.ibm.com/support/knowledgecenter/SSYKE2_8.0.0/com.ibm.java.win.80.doc/diag/understanding/jit_overview.html) – Akash Narayan Jan 10 '17 at 11:36
-
2I found https://www.youtube.com/watch?v=yQ27DjKnxwo to be useful. – Adam Zerner Jun 11 '17 at 00:55
18 Answers
A JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-in-time, as it's called) into a form that's usually faster, typically the host CPU's native instruction set. A JIT has access to dynamic runtime information whereas a standard compiler doesn't and can make better optimizations like inlining functions that are used frequently.
This is in contrast to a traditional compiler that compiles all the code to machine language before the program is first run.
To paraphrase, conventional compilers build the whole program as an EXE file BEFORE the first time you run it. For newer style programs, an assembly is generated with pseudocode (p-code). Only AFTER you execute the program on the OS (e.g., by double-clicking on its icon) will the (JIT) compiler kick in and generate machine code (m-code) that the Intel-based processor or whatever will understand.

- 98,437
- 31
- 224
- 236
-
31And in contrast to interpreted code, that begins running the bytecode or VM instructions immediately without delay, but will run the instructions slower than machine language. – Aaron Sep 18 '08 at 19:53
-
5A JIT is often used with interpreted code to convert it to machine language but yes, purely interpreted code (without any JITting) is slow. Even Java bytecode without a JITter is really slow. – Mark Cidade Sep 18 '08 at 22:07
-
56The target doesn't have to be machine code, though. JRuby has a JIT compiler which will compile Ruby sourcecode to Java bytecode after a couple of invocations. Then, after another couple of invocations, the JVM JIT compiler kicks in and compiles the bytecode to native code. – Jörg W Mittag Sep 19 '08 at 02:38
-
Good example: the TraceMonkey engine. A JIT compiler for javascript now included in Firefox. https://wiki.mozilla.org/JavaScript:TraceMonkey – Brian Clozel Apr 16 '09 at 09:36
-
12It is worth noting that, as alluded to by Jörg, JIT is not necessarily invoked right away. Often, code will be interpreted until it is determined that it will be worth JITting. Since JITting can introduce delays, it may be faster to **NOT** JIT some code if it is rarely used and thus a fast response is more important than overall runtime. – Adam Jaskiewicz Apr 17 '09 at 19:32
-
@Mark Cidade: Have you got a link to an article, explaining JIT in more technical details (how it compiles to native byte code, how executes, memory model used, ...)? – dma_k Mar 25 '11 at 23:42
-
@dma_k I imagine that the details can be different between platforms but you can start with Wikipedia, I guess: http://en.wikipedia.org/wiki/Just-in-time_compilation – Mark Cidade Mar 26 '11 at 00:24
-
-
I updated the answer: "Conventional compilers build the whole program as an EXE file BEFORE the first time you run it. For newer style programs, an assembly is generated with pseudocode (p-code). Only AFTER you execute the program on the OS (e.g., by double-clicking on its icon) will the (JIT) compiler kick in and generate machine code (m-code) that the Intel-based processor or whatever will understand." – Mark Cidade Jun 06 '12 at 16:13
-
1If I'm a fanboy of anything, I'm a JS fanboy but this line suggests JITs are typically faster than pre-compiled, which I don't think is a feat anybody has pulled off yet: "A JIT has access to dynamic runtime information whereas a standard compiler doesn't and can make better optimizations like inlining functions that are used frequently." – Erik Reppen Aug 16 '12 at 17:20
-
4@ErikReppen: If a new machine comes out, compiling and optimizing a program for that new machine using a conventional compiler will likely yield results faster than a JIT. On the other hand, a JIT that's optimized for that new machine will be able to be able to optimize the performance of code *which was published before that new machine was invented*. – supercat Dec 03 '13 at 18:27
-
@supercat Couldn't you say the same about the source code of a conventionally compiled language? Or is it just that nobody would ever bother to re-compile an old offering for new hardware? – Erik Reppen Dec 04 '13 at 01:02
-
@ErikReppen: There isn't an awful lot of old software being recompiled for new hardware. Generally, software vendors prefer to push new software, but some old software can be nice as long as it runs. I miss my old DOS-based text editor (PC-Write), which Windows 7 effectively forced me to give up. QBasic was sometimes nice too for some simple one-off tasks. I don't think that I could very often finish a one-off task in QBASIC in less time than it takes to even launch Visual Studio, but in many cases it would be pretty close. QBasic, however, hasn't been recompiled in *ages*. – supercat Dec 04 '13 at 15:45
-
1@supercat Have you tried DOSbox? I also like Scite when I've got those "Visual Studio crashed because I typed too fast" blues (and yes, in one environment I worked in, it would crash because I typed the CSS too fast. I'm guessing it was 25 real-time parsers in addition to all the ones I thought I'd turned off all jumping on the problem of helping me write the Razor, HTML, CSS, and JavaScript at the same time. Scite has color-coding. It loads. I've seen it crash once or twice and I'm not sure it was even the app's fault. – Erik Reppen Dec 04 '13 at 15:59
-
@ErikReppen: I've used PC Write occasionally in a VM, but the fact that VM drives are in a separate universe from everything else limits the usefulness. – supercat Dec 06 '13 at 20:57
-
@MarkCidade Can you please take a look at this question: http://stackoverflow.com/questions/28135312/how-does-an-interpreter-interpret-the-code ? – Koray Tugay Jan 26 '15 at 08:39
-
Caution: "A JIT compiler runs after the program has started and compiles the code..." might be misleading without a conceptual understanding. It is right, although it does not mean that it runs *right after* running the program, in a separate step, as in JIT compilation being the first and separate step of running the program – szeb May 13 '20 at 19:02
-
This should not be the accepter answer as JIT doesn't happen right away, the JVM starts out interpreting and then once certain method reach the CompileThreshold they will be optimized and compiled to machine code by JIT. – Diego Ramos Feb 08 '21 at 03:18
In the beginning, a compiler was responsible for turning a high-level language (defined as higher level than assembler) into object code (machine instructions), which would then be linked (by a linker) into an executable.
At one point in the evolution of languages, compilers would compile a high-level language into pseudo-code, which would then be interpreted (by an interpreter) to run your program. This eliminated the object code and executables, and allowed these languages to be portable to multiple operating systems and hardware platforms. Pascal (which compiled to P-Code) was one of the first; Java and C# are more recent examples. Eventually the term P-Code was replaced with bytecode, since most of the pseudo-operations are a byte long.
A Just-In-Time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into the machine code instructions of the running machine, and then invoke this object code instead. Ideally the efficiency of running object code will overcome the inefficiency of recompiling the program every time it runs.

- 15,507
- 6
- 37
- 55
-
5However this phrase *"a Just-In-Time (JIT) compiler is a feature of the run-time interpreter"* causes confusion; e.g. - http://stackoverflow.com/questions/16439512/what-is-the-use-of-jvm-if-jit-is-performing-bytecode-conversion-to-machine-instr/16440092#16440092 – Stephen C May 15 '13 at 22:40
-
12Actually, the JIT was an add-on, and you can still disable it using the -Xint parameter to Java, so it's just a feature. – Craig Trader May 16 '13 at 16:34
-
@CraigTrader - The Hotspot JIT compiler has been an inseparable part of Hotspot JVMs since Java 1.3. These days, calling it "a feature of the interpreter" is confusing. And the fact that you can disable JIT compilation is orthogonal. – Stephen C Jul 28 '14 at 00:23
-
3I don't fully agree. JIT is not evolution - it is alternative of classic compilers. – i486 Nov 03 '14 at 13:12
-
2JIT is one step on the evolutionary path from hard-wiring mechanical switches to specifying search criteria by saying "OK Google" to your smart phone. The current JIT available as part of Java 7/8 is leaps and bounds beyond what was available as part of Java 2 -- that's evolution as well. – Craig Trader Nov 03 '14 at 15:07
-
1@i486 - Sun / Oracle have (AFAIK) never shipped a classical ("ahead of time") compiler for Java that generates native code. It is a stretch to argue that JIT is an alternative ... when they thing it is supposedly an alternative for was never shipped. (I discount the GCJ AOT compiler because that was nothing to do with Sun / Oracle, and it wasn't a complete solution either. It is certainly non-viable now.) – Stephen C Apr 02 '17 at 05:44
-
Silicon Graphics had a native-code compiler for Java, though SGI is gone now. – Craig Trader Oct 28 '17 at 15:57
-
That was a very long time ago. I can't find anything on the web past Java 1.1 releases for SGI. – Stephen C Jan 25 '18 at 01:05
JIT-Just in time the word itself says when it's needed (on demand)
Typical scenario:
The source code is completely converted into machine code
JIT scenario:
The source code will be converted into assembly language like structure [for ex IL (intermediate language) for C#, ByteCode for java].
The intermediate code is converted into machine language only when the application needs that is required codes are only converted to machine code.
JIT vs Non-JIT comparison:
In JIT not all the code is converted into machine code first a part of the code that is necessary will be converted into machine code then if a method or functionality called is not in machine then that will be turned into machine code... it reduces burden on the CPU.
As the machine code will be generated on run time....the JIT compiler will produce machine code that is optimised for running machine's CPU architecture.
JIT Examples:
- In Java JIT is in JVM (Java Virtual Machine)
- In C# it is in CLR (Common Language Runtime)
- In Android it is in DVM (Dalvik Virtual Machine), or ART (Android RunTime) in newer versions.

- 5,753
- 72
- 57
- 129

- 31,670
- 10
- 160
- 241
-
8JIT offers some special advantages in frameworks with support for real generic types; it's possible to define a generic method which would be capable of producing an unbounded range of types, each of would require different machine code, but only have the JIT generate code for types which are actually produced. By contrast, in C++ it's necessary that the compiler generate code for all types a program will ever use. – supercat Dec 02 '13 at 23:29
-
8The JVM doesn't JIT code the first time it runs it. The first few times, it interprets bytecode. Then, if that code runs often enough, it may decide to bother JITting it. – ninjalj Mar 18 '14 at 15:13
-
1You are saying JIT in Java is JVM. However we already provide the compiled code to JVM, isn't it? Then it compiles it again you mean? – Koray Tugay Dec 31 '14 at 10:04
-
@KorayTugay - We provide Bytecodes to JVM and JVM will convert part of that to machine code on demand.so resources are saved. – Durai Amuthan.H Jan 02 '15 at 11:50
-
@Duraiamuthan.H Could you please take a look at Can you please take a look at this question: http://stackoverflow.com/questions/28135312/how-does-an-interpreter-interpret-the-code ? – Koray Tugay Jan 26 '15 at 08:40
-
1
-
@happs - you are right JIT is part of JVM and it makes JVM efficient. – Durai Amuthan.H Mar 13 '15 at 11:07
-
May you give an example of a method which is in machine code and another one which is not? – revo Mar 24 '18 at 11:56
-
up-vote cuz of the last paragraph is really nice to tell new people to JIT at least 3 different JIT examples in the wild – ShifraSec Feb 02 '21 at 11:00
-
@ninjalj I think it depends on the implementation, i.e. which JVM we're using, but anyways [this](https://en.wikibooks.org/wiki/Java_Programming/The_Java_Platform) says different from what you said. It says *"JIT only occurs when the byte-code is executed for the first time."* What do you think about this? – starriet Mar 12 '22 at 00:19
As other have mentioned
JIT stands for Just-in-Time which means that code gets compiled when it is needed, not before runtime.
Just to add a point to above discussion JVM maintains a count as of how many time a function is executed. If this count exceeds a predefined limit JIT compiles the code into machine language which can directly be executed by the processor (unlike the normal case in which javac compile the code into bytecode and then java - the interpreter interprets this bytecode line by line converts it into machine code and executes).
Also next time this function is calculated same compiled code is executed again unlike normal interpretation in which the code is interpreted again line by line. This makes execution faster.

- 66,731
- 38
- 279
- 289
JIT compiler only compiles the byte-code to equivalent native code at first execution. Upon every successive execution, the JVM merely uses the already compiled native code to optimize performance.
Without JIT compiler, the JVM interpreter translates the byte-code line-by-line to make it appear as if a native application is being executed.
-
3My interpretation of JIT is that it acts like memoization, where frequently used functions are 'stored' and the expense of compilation from java bytecode to native ISA-dependent code is bypassed. If this is correct, why doesn't java compile completely to native code from the start? This would reduce any kind of run-time compilation and make java 'native' to the machine? – Michael Choi Mar 27 '18 at 09:29
-
2Because it would delay the launch of the application. JIT enables fast startup and accelerates application execution. It's a tradeoff. – Kacper Cichecki Oct 15 '20 at 00:10
-
-
I also checked the source, but I don't think this is the typical JVM implementation. It depends on the implementation, but this is not the usual modern JVM I think. Could somebody please clarify if this answer is correct or modern JVM is different? – starriet Mar 12 '22 at 00:39
JIT stands for Just-in-Time which means that code gets compiled when it is needed, not before runtime.
This is beneficial because the compiler can generate code that is optimised for your particular machine. A static compiler, like your average C compiler, will compile all of the code on to executable code on the developer's machine. Hence the compiler will perform optimisations based on some assumptions. It can compile more slowly and do more optimisations because it is not slowing execution of the program for the user.

- 14,558
- 15
- 68
- 104
-
Why aren't compiled codes stored in somewhere in user's computer so the next time the application is run JIT doesn't have to recompile them again? – omerfarukdogan May 21 '16 at 17:47
-
Good observations. It is possible to do this, but whether it actually beneficial depends on the platform and usage of the app. JIT optimisation is not necessarily the same as offline, or ahead of time optimisation so the benefit may only be 'not JITting' which may, or may not help much. – Brian Lyttle May 22 '16 at 23:05
After the byte code (which is architecture neutral) has been generated by the Java compiler, the execution will be handled by the JVM (in Java). The byte code will be loaded in to JVM by the loader and then each byte instruction is interpreted.
When we need to call a method multiple times, we need to interpret the same code many times and this may take more time than is needed. So we have the JIT (just-in-time) compilers. When the byte has been is loaded in to JVM (its run time), the whole code will be compiled rather than interpreted, thus saving time.
JIT compilers works only during run time, so we do not have any binary output.
-
2The whole code isn't compiled when loaded into the JVM, as there's little information (read: guide) on how to go about the compilation. Keep in mind that performance is the ultimate goal. JIT is rather selective: monitoring and selecting the most popular methods for optimization. And it keeps doing this until maximum level of optimization has been reached for individual methods. – Yaw Boakye Sep 12 '17 at 09:04
A just in time compiler (JIT) is a piece of software which takes receives an non executable input and returns the appropriate machine code to be executed. For example:
Intermediate representation JIT Native machine code for the current CPU architecture
Java bytecode ---> machine code
Javascript (run with V8) ---> machine code
The consequence of this is that for a certain CPU architecture the appropriate JIT compiler must be installed.
Difference compiler, interpreter, and JIT
Although there can be exceptions in general when we want to transform source code into machine code we can use:
- Compiler: Takes source code and returns a executable
- Interpreter: Executes the program instruction by instruction. It takes an executable segment of the source code and turns that segment into machine instructions. This process is repeated until all source code is transformed into machine instructions and executed.
- JIT: Many different implementations of a JIT are possible, however a JIT is usually a combination of a compiler and an interpreter. The JIT first turn intermediary data (e.g. Java bytecode) which it receives into machine language via interpretation. A JIT can often measures when a certain part of the code is executed often and the will compile this part for faster execution.

- 33,665
- 16
- 190
- 155
Just In Time Compiler (JIT) :
It compiles the java bytecodes into machine instructions of that specific CPU.
For example, if we have a loop statement in our java code :
while(i<10){
// ...
a=a+i;
// ...
}
The above loop code runs for 10 times if the value of i is 0.
It is not necessary to compile the bytecode for 10 times again and again as the same instruction is going to execute for 10 times. In that case, it is necessary to compile that code only once and the value can be changed for the required number of times. So, Just In Time (JIT) Compiler keeps track of such statements and methods (as said above before) and compiles such pieces of byte code into machine code for better performance.
Another similar example , is that a search for a pattern using "Regular Expression" in a list of strings/sentences.
JIT Compiler doesn't compile all the code to machine code. It compiles code that have a similar pattern at run time.
See this Oracle documentation on Understand JIT to read more.
-
"It is not necessary to compile the bytecode for 10 times again and again as the same instruction is going to execute for 10 times" - what about a regular compiler? Does it compile this piece several times? – TT_ stands with Russia Mar 12 '18 at 02:08
I know this is an old thread, but runtime optimization is another important part of JIT compilation that doesn't seemed to be discussed here. Basically, the JIT compiler can monitor the program as it runs to determine ways to improve execution. Then, it can make those changes on the fly - during runtime. Google JIT optimization (javaworld has a pretty good article about it.)

- 2,332
- 3
- 19
- 30
You have code that is compliled into some IL (intermediate language). When you run your program, the computer doesn't understand this code. It only understands native code. So the JIT compiler compiles your IL into native code on the fly. It does this at the method level.

- 24,293
- 14
- 43
- 56
-
4
-
This is not correct, it is run by the interpreter, JIT will only kick in after the CompileThreshold is reached for the method in question – Diego Ramos Feb 08 '21 at 03:25
just-in-time (JIT) compilation, (also dynamic translation or run-time compilation), is a way of executing computer code that involves compilation during execution of a program – at run time – rather than prior to execution.
IT compilation is a combination of the two traditional approaches to translation to machine code – ahead-of-time compilation (AOT), and interpretation – and combines some advantages and drawbacks of both. JIT compilation combines the speed of compiled code with the flexibility of interpretation.
Let's consider JIT used in JVM,
For example, the HotSpot JVM JIT compilers generate dynamic optimizations. In other words, they make optimization decisions while the Java application is running and generate high-performing native machine instructions targeted for the underlying system architecture.
When a method is chosen for compilation, the JVM feeds its bytecode to the Just-In-Time compiler (JIT). The JIT needs to understand the semantics and syntax of the bytecode before it can compile the method correctly. To help the JIT compiler analyze the method, its bytecode are first reformulated in an internal representation called trace trees, which resembles machine code more closely than bytecode. Analysis and optimizations are then performed on the trees of the method. At the end, the trees are translated into native code.
A trace tree is a data structure that is used in the runtime compilation of programming code. Trace trees are used in a type of 'just in time compiler' that traces code executing during hotspots and compiles it. Refer this.
Refer :

- 14,464
- 14
- 99
- 131
Jit stands for just in time compiler jit is a program that turns java byte code into instruction that can be sent directly to the processor.
Using the java just in time compiler (really a second compiler) at the particular system platform complies the bytecode into particular system code,once the code has been re-compiled by the jit complier ,it will usually run more quickly in the computer.
The just-in-time compiler comes with the virtual machine and is used optionally. It compiles the bytecode into platform-specific executable code that is immediately executed.

- 19
- 1
A non-JIT compiler takes source code and transforms it into machine specific byte code at compile time. A JIT compiler takes machine agnostic byte code that was generated at compile time and transforms it into machine specific byte code at run time. The JIT compiler that Java uses is what allows a single binary to run on a multitude of platforms without modification.
20% of the byte code is used 80% of the time. The JIT compiler gets these stats and optimizes this 20% of the byte code to run faster by adding inline methods, removal of unused locks etc and also creating the bytecode specific to that machine. I am quoting from this article, I found it was handy. http://java.dzone.com/articles/just-time-compiler-jit-hotspot

- 67
- 1
- 12
-
Not sure why this was marked -1. I think the point here is that run time statistics are used to help optimize. – eze Dec 16 '14 at 21:56
-
2Yes, but the answer didn't phrase it like that. Literally, JIT does not optimize the hottest 20% of the code. – mabraham Dec 23 '14 at 11:10
Just In Time compiler also known as JIT compiler is used for performance improvement in Java. It is enabled by default. It is compilation done at execution time rather earlier. Java has popularized the use of JIT compiler by including it in JVM.

- 1,135
- 11
- 18
JIT refers to execution engine in few of JVM implementations, one that is faster but requires more memory,is a just-in-time compiler. In this scheme, the bytecodes of a method are compiled to native machine code the first time the method is invoked. The native machine code for the method is then cached, so it can be re-used the next time that same method is invoked.

- 770
- 7
- 11
-
3I would avoid answering question like this if you don't provide something new / better. If you get any reaction it's probably a downvote or criticism: Your answer is imprecise. "JIT" is not limited to a [Java Virtual Machine](http://stackoverflow.com/q/5589409/995891), "faster but uses more memory" is a likely effect but not inherent to the JIT concept, and methods are often not compiled on the first invocation, rather after several when it becomes clear that spent time on JIT'ing is advantageous overall. – zapl Nov 03 '14 at 13:35
JVM actually performs compilation steps during runtime for performance reasons. This means that Java doesn't have a clean compile-execution separation. It first does a so called static compilation from Java source code to bytecode. Then this bytecode is passed to the JVM for execution. But executing bytecode is slow so the JVM measures how often the bytecode is run and when it detects a "hotspot" of code that's run very frequently it performs dynamic compilation from bytecode to machinecode of the "hotspot" code (hotspot profiler). So effectively today Java programs are run by machinecode execution.

- 2,732
- 2
- 14
- 21