如何“重复”? Java代码是由JVM JIT编译器优化的吗?
我负责维护一个基于 JSP 的应用程序,该应用程序在 IBM WebSphere 6.1 (IBM J9 JVM) 上运行。所有 JSP 页面都有一个静态包含引用,并且在此包含文件中声明了一些静态 Java 方法。它们包含在所有 JSP 页面中,以提供对这些实用程序静态方法的“轻松访问”。我知道这是一种非常糟糕的工作方式,我正在努力改变这种情况。但是,出于好奇,为了支持我改变这一点的努力,我想知道 JVM JIT 编译器如何优化这些“重复的”静态方法。
- 即使具有完全相同的签名,它们也是单独优化的吗?
- JVM JIT 编译器是否“看到”这些方法都是相同的并提供“统一”的 JIT 代码?
I'm in charge of maintaining a JSP based application, running on IBM WebSphere 6.1 (IBM J9 JVM). All JSP pages have a static include reference and in this include file there is some static Java methods declared. They are included in all JSP pages to offer an "easy access" to those utility static methods. I know that this is a very bad way to work, and I'm working to change this. But, just for curiosity, and to support my effort in changing this, I'm wondering how these "duplicated" static methods are optimized by the JVM JIT compiler.
- They are optimized separately even having the exact same signature?
- Does the JVM JIT compiler "sees" that these methods are all identical an provides an "unified" JIT'ed code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每个 JSP 页面都被编译为一个唯一的类,因此包含的代码也将被编译为不同的类。 JIT 不会将代码的各个副本合并为一个。
为了避免这种情况,您可以将导入的代码放入真正的 Java 类中,并将其导入到 JSP 中。这样就不会出现重复,因为您正在重用同一个类。
Each JSP page is compiled to a unique class, and so the included code will also be compiled into distinct classes. The JIT will not consolidate the various copies of the code into one.
To avoid this, you can put the imported code into a real Java class, and import that in the JSP. Then there will be no duplicates, since you are reusing the same class.
@mdma 的答案对于当前的 JVM 来说是正确的,但需要在几个方面进行限定。
未来 JVM 中的 JIT 可能支持积极优化,以减少本机代码的内存占用。
@mdma's answer is correct for current JVMs, but needs to be qualified in a couple of respects.
The JITs in future JVMs could possibly support aggressive optimizations for reducing the memory footprint of the native code.
The flipside is that unless you have thousands of distinct JSPs, the chances are that the overheads of a few static methods per JSP class won't make a lot of difference to your webapp's memory footprint.
您可以使用来自单个类的静态导入:<%@ page import="static foo.*" %>。
然后你就不再有所有的重复。除了导入之外,您不需要接触任何其他内容。
You can use static imports from a single class : <%@ page import="static foo.*" %>.
Then you no longer have all that duplication. And other than the import you would not need to touch aything else.