-1

I know that the way Java works is that the JVM and its Just-In-Time compiler turn the Java bytecode produced by the "compiler" into actual computer code to be run by the target machine. I know that the reason that Java compiles to an intermediary language instead of directly to machine code is because it's much more effective to let the JVM optimize it for the target system rather than compile platform-specific binaries for every possible circumstance.

My question is: once the JVM has fully internalized the classes and done all of its optimizations in the process of running the program, why doesn't it produce a binary that has all of those effects? Why does the bytecode need to be JIT-compiled every time?

I understand the portability JIT offers, I just don't understand why it needs to be done more than once.

ABadHaiku
  • 65
  • 1
  • 6
  • It could store the code fragments locally, with virus risks, the needed reloading of meta information on the fragments and such. That would take time too. And costs dev time – Joop Eggen Aug 27 '23 at 05:43
  • 1
    Because every time you run the program it could be with different classes. – user207421 Aug 27 '23 at 06:30
  • Because the JIT compiler optimizes for the behavior of the current application *run*; e.g. depending on input data, command line parameters, etc. – Stephen C Aug 27 '23 at 06:39
  • And your assumptions are wrong. Some JVMs *can* save JIT compiled code in a shared (persistent) code cache. For example: https://docs.oracle.com/cd/E13188_01/jrockit/docs142/userguide/codecach.html. But it turns out to be not as helpful as you would think. For a variety of reasons. – Stephen C Aug 27 '23 at 06:42
  • Granted ... a persistent code cache caches compiled classes not the entire executable. But it doesn't make sense to produce / save an executable image from the JIT compiled, because 1) JIT compilation never really finishes, and 2) such an executable would behave differently from regular Java *vis a vis* dynamic loading, binding, etc. Also, it is not *clear* that it would be technically feasible, or that the result would be worth the development *and maintenance* effort. – Stephen C Aug 27 '23 at 06:56
  • If you want faster startup, you should be looking at GraalVM's AOT native compilation capability. – Stephen C Aug 27 '23 at 06:58

0 Answers0