19 November, 2013

Java -XX: AutoBoxCacheMax option

-XX:AutoBoxCacheMax=size option lets to control cache size for Integers.

In current Oracle HotSpot JVM (1.7) it only affects on Integer objects, created via autoboxing or Integer.valueOf(int i).

public class IntegersDemo {
    public static void main(String[] args) {
        {
            Integer a100 = 100;
            Integer b100 = 100;
            System.out.println(a100 == b100); //true
 
            Integer a200 = 200;
            Integer b200 = 200;
            System.out.println(a200 == b200); //false by default, 
            // but true when -XX:AutoBoxCacheMax=1000
        }
 
        {
            Integer a100 = Integer.valueOf(100);
            Integer b100 = Integer.valueOf(100);
            System.out.println(a100 == b100); //true
 
            Integer a200 = Integer.valueOf(200);
            Integer b200 = Integer.valueOf(200);
            System.out.println(a200 == b200); //false by default,
            // but true when -XX:AutoBoxCacheMax=1000
        }
 
        {
            Integer a100 = new Integer(100);
            Integer b100 = new Integer(100);
            System.out.println(a100 == b100); //always false, 
            // because of objects are allocated via "new"
        }
    }
}
 

No comments: