I was reading different articles on Just-in-time compilers
and it is mentioned that JIT
optimizes code fragments (hotspots) into direct native code (machine code). My question is
does this optimization happens every time I run the .class file
. On the otherhand, traditional compiled
codes is optimized once and for-ever, not just during one execution
(for example).
and where does this kind of hot-spot optimization
"remembered" in the machine
?
That could depend on the JRE implementation, but in general JIT optimization gets redone each time you launch the program. Among other things, hotspot environments may change their optimization based on runtime profiling with the input data actually being processed.
Yes, this is very different from traditionally compiled languages.
One thing to watch out for: Partly because it depends on input, there is no guarantee that a hotspot JIT will optimize the code the same way every time. This makes trying to hand-optimize Java code MUCH more difficult. You really need to do full profile analysis, and you need to do it over extended executions and a complete range of input data, or you're likely to fool yourself. I've seen 20% performance variation on the same machine running the same code over the same input, apparently because the JIT optimized in a different order and ran into limits on how much native code it was willing to produce.