Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How "duplicated" Java code is optimized by the JVM JIT compiler?

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?
like image 762
Renan Mozone Avatar asked Jun 17 '10 23:06

Renan Mozone


2 Answers

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.

like image 51
mdma Avatar answered Nov 09 '22 20:11

mdma


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

like image 43
Stephen C Avatar answered Nov 09 '22 19:11

Stephen C