在JDK 1.6或更高版本中,Integer类的缓存机制更改的优点是什么?

时间:2013-11-20 09:03:20

标签: java integer

我发现jdk 1.6或更高版本的jdk版本中的缓存机制得到了改进。

在jdk 1.5中,Integer中的缓存数组是固定的,参见

  static final Integer cache[] = new Integer[-(-128) + 127 + 1];

在jdk 1.6或更高版本中,名为 getAndRemoveCacheProperties 的方法和IntegerCache.high属性已添加到Integer类中,

// java.lang.Integer.IntegerCache.high属性的值(在VM init期间获得)

private static String integerCacheHighPropValue;

static void getAndRemoveCacheProperties() {
    if (!sun.misc.VM.isBooted()) {
        Properties props = System.getProperties();
        integerCacheHighPropValue =
            (String)props.remove("java.lang.Integer.IntegerCache.high");
        if (integerCacheHighPropValue != null)
            System.setProperties(props);  // remove from system props
    }
}

通过此更改,可以为缓存配置最高值并使用新的缓存范围。-128 <= cachedValue <= highestValue)。

* 以下是我的问题: *

Q#1 为什么缓存范围在jdk 1.5中使用[-128~127]或jdk 1.6或更高版本的默认缓存?是否仅支持byteschar '\u0000'~ 'u007f'

Q#2 在jdk 1.6或更高版本中为缓存范围指定高值有什么好处?什么样的appilication或sceen适合我们这样做?

请帮我解决这些问题。非常感谢你提前。

以下是Integer类中IntegerCache和valueOf(int i)的源代码。这只是为了报复。

jdk 1.5

private static class IntegerCache {
private IntegerCache(){}

static final Integer cache[] = new Integer[-(-128) + 127 + 1];

static {
    for(int i = 0; i < cache.length; i++)
    cache[i] = new Integer(i - 128);
}
}


public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache 
    return IntegerCache.cache[i + offset];
}
    return new Integer(i);
}

jdk 1.6

   private static class IntegerCache {
    static final int high;
    static final Integer cache[];

    static {
        final int low = -128;

        // high value may be configured by property
        int h = 127;
        if (integerCacheHighPropValue != null) {
            // Use Long.decode here to avoid invoking methods that
            // require Integer's autoboxing cache to be initialized
            int i = Long.decode(integerCacheHighPropValue).intValue();
            i = Math.max(i, 127);
            // Maximum array size is Integer.MAX_VALUE
            h = Math.min(i, Integer.MAX_VALUE - -low);
        }
        high = h;

        cache = new Integer[(high - low) + 1];
        int j = low;
        for(int k = 0; k < cache.length; k++)
            cache[k] = new Integer(j++);
    }

    private IntegerCache() {}
}


public static Integer valueOf(int i) {
    if(i >= -128 && i <= IntegerCache.high)
        return IntegerCache.cache[i + 128];
    else
        return new Integer(i);
}

3 个答案:

答案 0 :(得分:2)

  

Q#1为什么缓存范围在jdk 1.5中使用[-128~127]

因为这是他们设计JDK 1.5的方式。

  

或jdk 1.6或以上版本的默认缓存?

因为这是他们设计JDK 1.6的方式。

  

它只是支持字节和字符'\ u0000'〜'u007f'?

在JDK 1.5中,是的,在JDK 1.6+中,没有。

  

Q#2在jdk 1.6或更高版本中为缓存范围指定高值有什么好处?

这样可以缓存范围更广的值。

  

什么样的应用或场景适合我们这样做?

经常在更广范围内使用值的应用程序。

答案 1 :(得分:1)

出于性能原因,缓存更高的值可能会很有趣。想象一下,你正在使用List<Integer>进行密集操作,数字低于1000.然后使用缓存变得更快,而不是在堆上连续创建Integer对象。

我必须承认,我认为用例非常罕见。

答案 2 :(得分:0)

> +
> + /**
> + * Cache to support the object identity semantics of autoboxing for values between
> + * -128 and 127 (inclusive) as required by JLS.
> + *
> + * The cache is initialized on first usage. During VM initialization the
> + * getAndRemoveCacheProperties method may be used to get and remove any system
> + * properites that configure the cache size. At this time, the size of the
> + * cache may be controlled by the -XX:AutoBoxCacheMax=<size> option.
> + */

取自

Integer Class JDK 7

相关问题