JDK-6959129 : COMPARISON WITH INTEGER.MAX_INT DOES NOT WORK CORRECTLY IN THE CLIENT VM
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 6u21
  • Priority: P2
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2010-06-07
  • Updated: 2011-12-16
  • Resolved: 2010-06-09
Related Reports
Duplicate :  
Description
If you run the following example on Linux and compare the
Client VM versus the Server VM, you will notice that the with the client VM,
the testcase does not terminate :

public class Hailstone {

        public static void main(String[] args) {
        //        System.out.println(827370449 * 3 + 1);
                long start  = System.currentTimeMillis();
                int min = 2;
                int max = Integer.MAX_VALUE;
                System.out.println("maxMove:" + maxMoves(min, max));
                                
                System.out.println("time difference: " +
(System.currentTimeMillis() - start));
                
        }
        /**
         * Imperative implementation that returns the length hailstone moves
for a given number.
         */
        public static long hailstoneLengthImp(long n) {
                long moves = 0;
                while (n != 1) {
                        assert n > 1;
                        if (isEven(n)) {
                                n = n / 2;
                        } else {
                                n = 3 * n + 1;
                        }
                        ++moves;
                }
                return moves;
        }

        private static boolean isEven(long n) {
                return n % 2 == 0;
        }

        /**
         * Returns the maximum length of the hailstone sequence for numbers
         * between min to max.
         *
         * For rec1 - Assume that min is bigger than max.
         */
        public static long maxMoves(int min, int max) {
                long maxmoves = 0;
                for (int n = min; n <= max; n++) {
                        System.out.println(n);
                        long moves = hailstoneLengthImp(n);
                        if (moves > maxmoves) {
                                maxmoves = moves;
                        }
                }
                return maxmoves;
        }
}

% java -client -version
java version "1.6.0_21-ea"
Java(TM) SE Runtime Environment (build 1.6.0_21-ea-b05)
Java HotSpot(TM) Client VM (build 17.0-b15, mixed mode)

Comments
EVALUATION As per the notes, the testcase demonstrates an infinite loop using an int counter and the comparison <= Integer.MAX_VALUE as a condition - this never terminating with the client vm is correct behaviour. With the server VM there is an issue as it does terminate, which the customer now says is their problem. The behaviour is incorrect, although in practice the method should probably be protecting itself from entering an infinite loop (as it takes the loop limit as a parameter).
09-06-2010

WORK AROUND Run with the server VM.
07-06-2010