When the JIT gets it wrong
Overview I have often wondered what you would see if the JIT compiled code incorrectly, but it has been a long time since I found an example. A not so infinite loop There are many way to write an infinite loop. One confusing way is to write for(int i=0; i <= Integer.MAX_VALUE; i++) This is an infinite loop because i as an int value is always less than or equal to the maximum value, by definition. The JIT can detect this and take action. However the action taken in Oracle Java 6 update 25 is surprising. It just stops the loop, first at a rather random point and later after one iteration. public static void main(String[] args) { for(int i=0;i prints 1: Stopped generating at lastPrime= 39367 1: Stopped generating at lastPrime= 55291 1: Stopped generating at lastPrime= 3 1: Stopped generating at lastPrime= 3 1: Stopped generating at lastPrime= 3 1: Stopped generating at lastPrime= 3 1: Stopped generating at lastPrime= 3 1: Stopped generating at lastPrime= 3 1: Stopp...