views:

139

answers:

3

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?
A: 

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.

Peter Tillemans
+9  A: 

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
+2  A: 

@mdma's answer is correct for current JVMs, but needs to be qualified in a couple of respects.

  1. The JITs in future JVMs could possibly support aggressive optimizations for reducing the memory footprint of the native code.

  2. 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.

Stephen C