Class Integer
has cache, which caches the Integer
values. So if I use method valueOf
or inboxing the new value will not be instantiated, but get from the cache.
I know that the default cache size is 127
but can be extended due to VM settings. My question is: how large is the default value of cache size in these settings and can I manipulate this value? Is this value depended on which VM I use (32 or 64 bits)?
I'm now on tuning of a legacy code, and probably will need the conversion from int to Integer.
Clarification: Following code I've found in Java source
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.
From the Javadoc:
But you really shouldn't rely on this or try to change anything about this. This is an implementation detail of the JVM which should not influence your implementation.
For Java 7, I wasn’t able to find in Oracle’s java documentation is a description of exactly what would change when -XX:+AggressiveOpts is used. So I ran a process to determine the effect aggressiveopts has on jvm settings on one of our app servers in the lab that had java version: {quote} java version "1.7.0_15" Java(TM) SE Runtime Environment (build 1.7.0_15-b03) {quote}
The process is to run java with aggressiveopts disabled: java -d64 -server -XX:-AggressiveOpts -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version
The process is to run java with aggressiveopts enabled: java -d64 -server -XX:+AggressiveOpts -XX:+UnlockDiagnosticVMOptions -XX:+PrintFlagsFinal -version
This process produced these differences:
bool AggressiveOpts false --> true {product}
intx AutoBoxCacheMax 128 --> 20000 {C2 product}
intx BiasedLockingStartupDelay 4000 --> 500 {product}
bool EliminateAutoBox false --> true {C2 diagnostic}
Internal Java implementation and could not be configured, the range is from -128 to 127. You can check
Javadocs
or simply take a look at sources:UPD. I was wrong (thx to Marco Topolnik). All of the above is related to older Java implementations. For Java 7 implementation could be achieved with system property:
or JVM setting:
UPD. 2
java.math.BigInteger
has hardcoded cache for values -16 <= x <= 16. From sources: