javac 是否根据底层操作系统执行任何字节码级别的优化?

发布于 12-01 20:41 字数 534 浏览 0 评论 0原文

我的理解是,调用 javac 生成的 Java 字节码独立于底层操作系统,但 HotSpot 编译器会在程序运行时执行特定于平台的 JIT 优化和编译。

但是,我在 Windows 上使用 32 位 JDK 编译代码,并在 Solaris 上使用 32 位 JVM 执行代码(这两个操作系统都不是 64 位操作系统)。据我所知(正在确认其规格),Solaris x86 机器在所有方面(内核数量、RAM 数量、硬盘延迟、处理器速度等)都应该优于 Windows 机器。然而,相同的代码在 Windows 上的运行速度明显更快(单个数据点在 Windows 上的操作需要 7.5 秒,而在 Solaris 上则需要 10 秒以上)。我的下一个测试是在 Solaris 上进行编译并记录性能差异,但这对我来说没有意义,而且我找不到任何 Oracle 文档来解释我所看到的内容。

如果两个不同操作系统上的 JVM 版本相同(主要、次要、发行版等),则在相同源文件上调用 javac 会导致 Java 字节码(>.class 文件生成)?有没有任何文档可以解释这种行为?

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 Oracle 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

在梵高的星空下2024-12-08 20:41:23

编译输出不应依赖于调用 javac 的操作系统。
如果您想验证它,请尝试:

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

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
故乡的云2024-12-08 20:41:23

无论如何,我决定用谷歌搜索一下。 ;)

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

Java 平台是一个新的软件平台,用于在联网计算机系统上交付和运行高度交互、动态且安全的小程序和应用程序。但 Java 平台的与众不同之处在于,它位于其他平台之上,并执行字节码,字节码不特定于任何物理机,而是虚拟机的机器指令。用 Java 语言编写的程序编译为字节码文件,该文件可以在任何存在 Java 平台的地方、任何底层操作系统上运行。换句话说,同一个文件可以在运行 Java 平台的任何操作系统上运行。这种可移植性之所以成为可能,是因为 Java 平台的核心是 Java 虚拟机。

写于 1996 年 4 月 30 日。


一个常见的错误是假设编译器会优化代码,尤其是在您进行 C/C++ 开发时。它执行一项且仅有一项优化,即评估编译器时间已知常量。

确实,编译器远没有您想象的那么强大,因为它只是验证代码并生成与您的代码尽可能匹配的字节码。

这是因为字节码适用于理想化的虚拟机,理论上不需要任何优化。希望当您以这种方式思考时,编译器确实做了很多事情,但它不知道代码将如何实际使用,这是有道理的。

相反,所有优化都是由 JVM 中的 JIT 执行的。这完全依赖于平台,可以生成 32 位或 64 位代码,并使用运行该代码的处理器的确切指令。它还会根据代码的实际使用方式来优化代码,这是静态编译器无法做到的。这意味着代码可以根据不同的使用模式多次重新编译。 ;)

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. ;)

扎心2024-12-08 20:41:23

据我了解,javac 仅考虑 -target 参数来决定发出什么字节码,因此字节码生成中没有特定于平台的。

在解释字节码时,所有优化都是由 JVM 而不是编译器完成的。这是特定于各个平台的。

另外我在某处读到Solaris JVM 是参考实现,然后它被移植到Windows。因此,Windows 版本比 Solaris 版本更优化。

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.

情释2024-12-08 20:41:23

javac 是否执行任何字节码级别的优化,具体取决于
底层操作系统?

不能。

要确定程序的性能特征在两个平台上不同的原因,需要在相同的工作负载下对它们进行分析,并仔细分析方法执行时间和内存分配/GC 行为。你的程序有I/O吗?

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?

木緿2024-12-08 20:41:23

扩展一下 dacwe 的部分“也许 Solaris JVM 比 Windows JVM 慢?”

有配置选项(例如,是否使用客户端或服务器 vm [link],可能还有其他),其默认值因操作系统而异。因此,这可能是 Solaris VM 速度较慢的原因。

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.

云淡月浅2024-12-08 20:41:22

不会。javac 不会在不同平台上做任何优化。

请参阅 oracle "tools" 页面(其中 javac 等工具介绍):

每个开发工具都有 Microsoft Windows 版本 (Windows) 和 Solaris 或 Linux 版本。 版本之间的功能实际上没有差异。但是,在配置和使用方面存在细微差别,以适应每个操作系统的特殊要求。 (例如,指定目录分隔符的方式取决于操作系统。


(也许 Solaris JVM 比 Windows JVM 慢?)

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?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文