3

I just read C++ performance vs Java/C#.

As already said in the previous posts, JIT can compile IL/bytecode into native code at runtime. The cost of that was mentionned, but not to its conclusion:

JIT has one massive problem is that it can't compile everything: JIT compiling takes time, so the JIT will compile only some parts of the code, whereas a static compiler will produce a full native binary: For some kind of programs, the static compiler will simply easily outperform the JIT.

And I got a curiosity why java doesn't compile everything while installing it in device.

If it does, we don't need to think about performance loss with compilation time, and conform to diverse devices.

Community
  • 1
  • 1
Jeonghan Joo
  • 225
  • 2
  • 10
  • 2
    It doesn't do that because it isn't defined to do that. Your question? – user207421 Oct 04 '15 at 09:46
  • This has pros and cons. One advantage of JIT compiling the code is that the compiler has an idea how the code is going to be used when it compiles it. If you Ahead of Time compile it this isn't possible (unless you feed it data from a previous run) – Richard Tingle Oct 04 '15 at 10:09
  • Java does control the whole device, you could change any file or any JAR without using the JVM to do this so it has no idea making the process error prone. For Android, it knows when you install an app or undate it so it knows what to compile and change. Also it is more expensive to be compiling on the fly in terms of startup time for a device. For servers this matters less than the number of user's it can support and long term efficiency. – Peter Lawrey Oct 05 '15 at 11:04

1 Answers1

5

Actually it's JVM dependent. The new Google's JVM uses AOT:

ART uses an ahead-of-time (AOT) compiler that compiles to machine code when you install the app.

If you're asking why former Sun, now Oracle's JVM doesn't use AOT - it was Sun's engineers choice at the time. For desktop Java there is (usually) no step of installation of the application that would allow to do the AOT and compiling the whole classpath upon load is too time consuming.

More here and of course at Google android site.

MirMasej
  • 1,592
  • 12
  • 17
  • AOT disadvantage: Could be like you have an app which could have lot of code changes, new updates, bug fixes, patches and continuous deployment nature. So, your code has to be re-compiled more frequently and get machine code which becomes an overhead. For many mobile apps, you may not expect such frequent code changes, fixes, etc and hence quite stable code has to be compiled once for all and use it. Whereas, consider a system software widely used, with distributed Dev teams, CI/CD etc. You can expect repeated compilation required. Hence, JIT is a better choice in such a case. – Chaitanya P Oct 05 '15 at 10:30