开发者

Does javac perform any bytecode level optimizations depending on the underlying operating system?

My understanding is that the Java bytecode produced by invoking javac is independent of the underlying operating system, but the HotSpot compiler will perform platform-specific JIT optimizations and compilations as the program is running.

However, I compiled code on Windows under a 32 bit JDK and executed it on Solaris under a 32 bit JVM (neither OS is a 64 bit operating system). The Solaris x86 box, to the best of my knowledge (working to confirm the specs on it) should outperform the Windows box in all regards (number of cores, amount of RAM, hard disk latency, processor speed, and so on). However, the same code is running measurably faster on Windows (a single data point would be a 7.5 second operation on Windows taking over 10 seconds on Solaris) on a consistent basis. My next test would be to compile on Solaris and note performance differences, but that just doesn't make sense to me, and I couldn't find any 开发者_如何学PythonOracle documentation that would explain what I'm seeing.

Given the same version (major, minor, release, etc.) of the JVM on two different operating systems, would invoking javac on the same source files result in different optimizations within the Java bytecode (the .class files produced)? Is there any documentation that explains this behavior?


No. javac does not do any optimizations on different platforms.

See the oracle "tools" page (where javac and other tools are described):

Each of the development tools comes in a Microsoft Windows version (Windows) and a Solaris or Linux version. There is virtually no difference in features between versions. However, there are minor differences in configuration and usage to accommodate the special requirements of each operating system. (For example, the way you specify directory separators depends on the OS.)


(Maybe the Solaris JVM is slower than the windows JVM?)


The compilation output should not depend on OS on which javac was called. If you want to verify it try:

me@windows@ javac Main.java
me@windows@ javap Main.class > Main.win.txt

me@linux@ javac Main.java
me@linux@ javap Main.class > Main.lin.txt

diff Main.win.txt Main.lin.txt


I decided to google it anyway. ;)

http://java.sun.com/docs/white/platform/javaplatform.doc1.html

The Java Platform is a new software platform for delivering and running highly interactive, dynamic, and secure applets and applications on networked computer systems. But what sets the Java Platform apart is that it sits on top of these other platforms, and executes bytecodes, which are not specific to any physical machine, but are machine instructions for a virtual machine. A program written in the Java Language compiles to a bytecode file that can run wherever the Java Platform is present, on any underlying operating system. In other words, the same exact file can run on any operating system that is running the Java Platform. This portability is possible because at the core of the Java Platform is the Java Virtual Machine.

Written April 30, 1996.


A common mistake, esp if you have developed for C/C++, is to assume that the compiler optimises the code. It does one and only one optimisation which is to evaluate compiler time known constants.

It is certainly true that the compiler is no where near as powerful as you might imagine because it just validates the code and produces byte-code which matches your code as closely as possible.

This is because the byte-code is for an idealised virtual machine which in theory doesn't need any optimisations. Hopefully when you think about it that way it makes sense that the compiler does do anything much, it doesn't know how the code will actually be used.

Instead all the optimisation is performed by the JIT in the JVM. This is entirely platform dependant and can produce 32-bit or 64-bit code and use the exact instruction of the processor running the code. It will also optimise the code based on how it is actually used, something a static compiler cannot do. It means the code can be re-compiled more than once based on different usage patterns. ;)


To my understanding javac only consideres the -target argument to decide what bytecode to emit, hence there is no platform specific in the byte code generation.

All the optimization is done by the JVM, not the compiler, when interpreting the byte codes. This is specific to the individual platform.

Also I've read somewhere that the Solaris JVM is the reference implementation, and then it is ported to Windows. Hence the Windows version is more optimzied than the Solaris one.


Does javac perform any bytecode level optimizations depending on the underlying operating system?

No.

Determining why the performance characteristics of your program are different on two platforms requires profiling them under the same workload, and careful analysis of method execution times and memory allocation/gc behaivor. Does your program do any I/O?


To extend on dacwe's part "Maybe the Solaris JVM is slower than the windows JVM?"

There are configuration options (e.g., whether to use the client or server vm [link], and probably others as well), whose defaults differ depending on the OS. So that might be a reason why the Solaris VM is slower here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜