Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't more Java software compiled natively?

I realize the benefits of bytecode vs. native code (portability).

But say you always know that your code will run on a x86 architecture, why not then compile for x86 and get the performance benefit?

Note that I am assuming there is a performance gain to native code compilation. Some folks have answered that there could in fact be no gain which is news to me..

like image 341
Marcus Leon Avatar asked Feb 27 '10 20:02

Marcus Leon


People also ask

Why is Java not compiled?

In Java, programs are not compiled into executable files; they are compiled into bytecode (as discussed earlier), which the JVM (Java Virtual Machine) then interprets / executes at runtime. Java source code is compiled into bytecode when we use the javac compiler.

Can Java compile to native?

Yes! Native Image The native image feature with the GraalVM SDK helps improve the startup time of Java applications and gives them a smaller footprint. Effectively, it's converting bytecode that runs on the JVM (on any platform) to native code for a specific OS/platform — which is where the performance comes from.

Does Java get compiled to machine code?

The Java classes/bytecode are compiled to machine code and loaded into memory by the JVM when needed the first time. This is different from other languages like C/C++ where programs are to be compiled to machine code and linked to create an executable file before it can be executed.

Does Java compile to C?

The JVM (Java Virtual Machine) may be an interpreter or a JIT (Just In Time) compiler or both. If it is a compiler then it is writing machine code directly. It does not write C code first.


2 Answers

Because the performance gain (if any) is not worth the trouble.

Also, garbage collection is very important for performance. Chances are that the GC of the JVM is better than the one embedded in the compiled executable, say with GCJ.

And just in time compilation can even result in better performance because the JIT has more information are run-time available to optimize the compilation than the compiler at compile-time. See the wikipedia page on JIT.

like image 197
ewernli Avatar answered Nov 09 '22 19:11

ewernli


"Solaris" is an operating system, not a CPU architecture. The JVM installed on the actual machine will compile to the native CPU instructions. Solaris could be SPARC, x86, or x86-64 architecture.

Also, the JIT compiler can make processor-specific optimisations depending on which actual CPU family you have. For example, different instruction sequences are faster on Intel CPUs than on AMD CPUs, and a JIT compiler for your exact platform can take advantage of this information to produce highly optimised code.

like image 22
Greg Hewgill Avatar answered Nov 09 '22 21:11

Greg Hewgill