How big is the Java – integer cache?

The class integer has a cache that caches integer values So if I use the method valueof or in@R_633_2419 @Ing, the new value will not be instantiated, but obtained from the cache

I know the default cache size is 127, but it can be expanded due to VM settings My question is: what is the default value of cache size in these settings, and can I manipulate this value? This value depends on which VM I use (32-bit or 64 bit)?

I am now adjusting the legacy code, which may need to be converted from int to integer

Clarification: follow the code I found in the Java source code

private static class IntegerCache {
    static final int low = -128;
    static final int high;
    static final Integer cache[];

    static {
        // high value may be configured by property
        int h = 127;
        String integerCacheHighPropValue =
            sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
        if (integerCacheHighPropValue != null) {
            int i = parseInt(integerCacheHighPropValue);
            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) {
    assert IntegerCache.high >= 127;
    if (i >= IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
}

So I think the cache is configurable

Solution

Internal Java implementation and can not be configured, ranging from - 128 to 127 You can check JavaDocs or just look at the source code:

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);
}

UPD. I was wrong (for Marco topolnik) All of the above is related to older Java implementations For Java 7, you can use system properties to implement:

-Djava.lang.Integer.IntegerCache.high=<size>

Or JVM settings:

-XX:Auto@R_633_2419@CacheMax=<size>

UPD. 2 java. math. BigInteger has a hard coded cache with a value of - 16 < = x < = 16 come from:

private final static int MAX_CONSTANT = 16;
    private static BigInteger posConst[] = new BigInteger[MAX_CONSTANT+1];
    private static BigInteger negConst[] = new BigInteger[MAX_CONSTANT+1];
    static {
    for (int i = 1; i <= MAX_CONSTANT; i++) {
        int[] magnitude = new int[1];
        magnitude[0] = i;
        posConst[i] = new BigInteger(magnitude,1);
        negConst[i] = new BigInteger(magnitude,-1);
    }
    }

    public static BigInteger valueOf(long val) {
        // If -MAX_CONSTANT < val < MAX_CONSTANT,return stashed constant
        if (val == 0)
            return ZERO;
        if (val > 0 && val <= MAX_CONSTANT)
            return posConst[(int) val];
        else if (val < 0 && val >= -MAX_CONSTANT)
            return negConst[(int) -val];
        return new BigInteger(val);
    }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>