java - Java 是否保存其运行时优化?

标签 java optimization caching

我的教授对一个小程序做了一个非正式的基准测试,Java 时间是:第一次运行 1.7 秒,之后运行 0.8 秒。

  • 这是否完全是因为将运行时环境加载到运行环境中?

  • 它是否受到 Java 优化代码和存储这些优化结果的影响(抱歉,我不知道那个的技术术语)?

最佳答案

好的,我找到了我读到的地方。这全部来自“学习 Java”(O'Reilly 2005):

The problem with a traditional JIT compilation is that optimizing code takes time. So a JIT compiler can produce decent results but may suffer a significant latency when the application starts up. This is generally not a problem for long-running server-side applications but is a serious problem for client-side software and applications run on smaller devices with limited capabilities. To address this, Sun's compiler technology, called HotSpot, uses a trick called adaptive compilation. If you look at what programs actually spend their time doing, it turns out that they spend almost all their time executing a relatively small part of the code again and again. The chunk of code that is executed repeatedly may be only a small fraction of the total program, but its behavior determines the program's overall performance. Adaptive compilation also allows the Java runtime to take advantage of new kinds of optimizations that simply can't be done in a statically compiled language, hence the claim that Java code can run faster than C/C++ in some cases.

To take advantage of this fact, HotSpot starts out as a normal Java bytecode interpreter, but with a difference: it measures (profiles) the code as it is executing to see what parts are being executed repeatedly. Once it knows which parts of the code are crucial to performance, HotSpot compiles those sections into optimal native machine code. Since it compiles only a small portion of the program into machine code, it can afford to take the time necessary to optimize those portions. The rest of the program may not need to be compiled at all—just interpreted—saving memory and time. In fact, Sun's default Java VM can run in one of two modes: client and server, which tell it whether to emphasize quick startup time and memory conservation or flat out performance.

A natural question to ask at this point is, Why throw away all this good profiling information each time an application shuts down? Well, Sun has partially broached this topic with the release of Java 5.0 through the use of shared, read-only classes that are stored persistently in an optimized form. This significantly reduces both the startup time and overhead of running many Java applications on a given machine. The technology for doing this is complex, but the idea is simple: optimize the parts of the program that need to go fast, and don't worry about the rest.

我有点想知道自 Java 5.0 以来 Sun 在它方面取得了多大进展。

关于java - Java 是否保存其运行时优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68109/

相关文章:

仅使用奇数除数计算素数速度较慢

optimization - 程序堆栈和数据段之间的距离对 CPU 缓存有影响吗?

java - 在特定时间间隔后提交 jsp 页面

java - 在 xhtml 中使用 doFilter 的嵌入页面不起作用

java - 无法让 Github 在 intellij 上显示受控版本

performance - WordPress 网站上的高延迟为 4-6 秒

java - Vertx 中的定时缓存

java - 在写入该值之前检查变量是否包含特定值是否是明智的优化?

c# - 自安装内存缓存 - 它存在吗?

java - 转义文件路径中的空格