### 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<10;i++) generate(Integer.MAX_VALUE); for(int i=0;i<10;i++) generate2(Integer.MAX_VALUE); System.out.println("End of Main"); } // generate primes. public static void generate(int limit) { int lastPrime = 0; for (int i = 3; i <= limit; i += 2) if (isPrime(i)) lastPrime = i; System.out.println("1: Stopped generating at lastPrime= "+lastPrime); } // smae as generate() but need to be JITed again. public static void generate2(int limit){ int lastPrime = 0; for (int i = 3; i <= limit; i += 2) if (isPrime(i)) lastPrime = i; System.out.println("2: Stopped generating at lastPrime= "+lastPrime); } // checking for primes public static boolean isPrime(int n) { double sqrt = Math.sqrt(n); for (int i = 2; i <= sqrt; i++) if (n % i == 0) return false; return true; }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: Stopped generating at lastPrime= 3 1: Stopped generating at lastPrime= 3 2: Stopped generating at lastPrime= 49603 2: Stopped generating at lastPrime= 2039 2: Stopped generating at lastPrime= 2039 2: Stopped generating at lastPrime= 3 2: Stopped generating at lastPrime= 3 2: Stopped generating at lastPrime= 3 2: Stopped generating at lastPrime= 3 2: Stopped generating at lastPrime= 3 2: Stopped generating at lastPrime= 3 2: Stopped generating at lastPrime= 3 End of Main

## Comments

## Post a Comment