• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java code and JIT compilation  RSS feed

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,
Java code is compiled to bytecode which it is portable across many platforms.
But Java also is JIT compiled which happens on the fly.
Does this mean Java is compile twice? first by us to produce the bytecode and the second by the JVM?
Thanks.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Todd wrote:Hey,
Java code is compiled to bytecode which it is portable across many platforms.
But Java also is JIT compiled which happens on the fly.
Does this mean Java is compile twice? first by us to produce the bytecode and the second by the JVM?
Thanks.


Well, definitely once -- when you compile the java source into byte code.

As for the JIT, that depends on the JVM. Some JVMs, particularly the older ones, just interprets the byte codes. Most modern JVMs today will indeed compile the byte code into native code. Some JVMs will even actually use the JIT compiler more than once -- the first time to compile it to native, and after a while of running it, and collecting profiling data, it will compile the bytecode to native again, is an optimized fashion, using the data that it collected from the first JIT'ed code.

Henry
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can be, yes.
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
John Todd wrote:Hey,
Java code is compiled to bytecode which it is portable across many platforms.
But Java also is JIT compiled which happens on the fly.
Does this mean Java is compile twice? first by us to produce the bytecode and the second by the JVM?
Thanks.


Well, definitely once -- when you compile the java source into byte code.

As for the JIT, that depends on the JVM. Some JVMs, particularly the older ones, just interprets the byte codes. Most modern JVMs today will indeed compile the byte code into native code. Some JVMs will even actually use the JIT compiler more than once -- the first time to compile it to native, and after a while of running it, and collecting profiling data, it will compile the bytecode to native again, is an optimized fashion, using the data that it collected from the first JIT'ed code.

Henry

Collecting profiling data of the running code.
Like what? would you please give me an example?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code has typical execution paths for a given run. That data can be collected and used to optimize the generated machine code.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Todd wrote:
Collecting profiling data of the running code.
Like what? would you please give me an example?


How often does a method get called? And from where? How often does a loop run? and what sizes? etc. etc. etc. There is no reason to spend extra time optimizing code if it doesn't get run too often. There is no reason to pay the price for certain optimizations, such as method inlining, if it doesn't get run too often.

Basically, figure out how the code is run, and how to best optimize it. This is actually what is done with the Sun hotspot JIT compiler -- except the hotspot compiler uses the interpreter to collect the profiling data before using the server JIT compiler to create optimized native code. The tiered model uses the client JIT compiler to generate profiling native code, and then follows it up with the server JIT compiler to create optimized native code, after the profiling data has been collected.

Henry
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow this is very complicated
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is, but for the most part, we never have to worry about it. It's also why if someone says "java is slow" there's a very good chance they don't know what they're talking about, or have paid zeronattention to java since it used to be slow.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!