Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disassemble Java JIT compiled native bytecode

Is there any way to do an assembly dump of the native code generated by the Java just-in-time compiler?

And a related question: Is there any way to use the JIT compiler without running the JVM to compile my code into native machine code?

like image 402
tskuzzy Avatar asked Aug 02 '11 11:08

tskuzzy


People also ask

Is Java bytecode JIT compiled?

The JIT compiler is enabled by default, and is activated when a Java method is called. The JIT compiler compiles the bytecodes of that method into native machine code, compiling it "just in time" to run. When a method has been compiled, the JVM calls the compiled code of that method directly instead of interpreting it.

Can Java be compiled to native code?

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.

Is Java a JIT or AOT?

AOT compilation is one way of improving the performance of Java programs and in particular the startup time of the JVM. The JVM executes Java bytecode and compiles frequently executed code to native code. This is called Just-in-Time (JIT) Compilation.

What do you mean by bytecode What is JVM and JIT?

JVM stands for Java Virtual Machine. JIT stands for Just-in-time compilation. JVM was introduced for managing system memory and providing a transportable execution environment for Java-based applications. JIT was invented to improve the performance of JVM after many years of its initial release.


2 Answers

Yes, there is a way to print the generated native code (requires OpenJDK 7).

No, there is no way to compile your Java bytecode to native code using the JDK's JIT and save it as a native executable.

Even if this were possible, it would probably not as useful as you think. The JVM does some very sophisticated optimizations, and it can even de-optimize code on the fly if necessary. In other words, it's not as simple as the JIT compiles your code to native machine language, and then that native machine language will remain unchanged while the program is running. Also, this would not let you make a native executable that is independent of the JVM and runtime library.

like image 83
Jesper Avatar answered Sep 24 '22 09:09

Jesper


If Amigable Clark Kant thought your question was heresy, he will really flip out over this answer. File this under "what is possible" not necessarily under "Justin recommends". :-)

One option is to use IKVM.NET to process your Java code using Mono. IKVM.NET allows you to run Java code on top of .NET or the Mono CLR. Some might say that you are skipping the JVM this way although it would be more accurate to say that IKVM.NET is really an implementation of the JVM that runs on the CLR.

At any rate, if you do this, you now have the option of doing Ahead-of-time (AOT) compilation of your code with Mono. That is, you can compile your code all the way down to machine native which means that you do not need either the CLR or the JVM at runtime.

Unlike what Jesper says above, using AOT compilation can actually result in better performance. While it is true that the JVM (and the CLR) perform some optimizations at runtime that are not possible at compile time, the reverse is also true. Also, one of the options is to use LLVM for code generation which typically results in better performance.

Here is the pipeline in a nutshell:

Your code -> IKVM.NET -> Mono -> LLVM -> Native executable

Or, just use a Java compiler that is designed to AOT generate native executables from the start (like GCJ or Excelsior Jet).

One place that the Mono/IKVM.NET option might be useful is if you want to use a Java library as part of an iPhone app (using MonoTouch). The App Store only allows native binaries.

EDIT: You can also use the LLVM VMKit to AOT compile Java code to machine code. This is conceptually the same process I described with Mono but keeping that nasty CIL stuff out of the picture.

like image 22
Justin Avatar answered Sep 20 '22 09:09

Justin