Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
890 views
in Technique[技术] by (71.8m points)

jvm - Will java machine code be the same on the same hardware?

I know that java source code will be compiled to bytecode, and JVM will interpret that bytecode and translates it into native machine code for the underlying platform.

But i would like to know if i have a x86 machine, no matter which OS i choose to install, will the machine code which translated by the platform dependent JVM be the same?

question from:https://stackoverflow.com/questions/65662017/will-java-machine-code-be-the-same-on-the-same-hardware

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

TL;DR - No it won't.

The native code generated by the JIT compiler from identical bytecodes, by an identical Java release, and on the same OS could still be different.

  • The generated code could depend on the physical hardware that the JVM is running on; e.g. the number of cores, the chipset, etcetera.

  • The generated code could even depend on the inputs to the program. Before JIT compiling, the JVM uses a bytecode interpreter to run the code. This self-profiling gathers stats about branch probabilities, etcetera. These stats are used by the JIT optimizer. Since the code paths taken while running a program can depend on program inputs, different runs of the same program on the same machine may end up with the JIT compiler generating different native code.

  • Since non-deterministic behavior during the JVM warmup can affect the stats gathered during self-profiling, it is even possible for the generated code to differ between runs of identical code on identical platforms with identical inputs.

That's the nice thing about HotSpot JIT compilation. It adapts to the environment, and what the application does. But this can be a problem if your primary goal is to get reproducible native code and reproducible performance characteristics.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...