整数缓存有多大? [英] How large is the Integer cache?
问题描述
Class Integer
具有缓存,缓存 Integer
值。因此,如果我使用方法 valueOf
或收件箱,新值将不会被实例化,而是从缓存中获取。
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.
我知道默认缓存大小是 127
,但由于VM设置可以扩展。我的问题是:在这些设置中缓存大小的默认值有多大,我可以操纵这个值吗?这个值取决于我使用的是哪个VM(32或64位)?
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)?
我现在正在调优遗留代码,可能需要从int转换到整数。
I'm now on tuning of a legacy code, and probably will need the conversion from int to Integer.
澄清:以下我在Java源代码中找到的代码
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.
推荐答案
内部Java实现且无法配置,范围从-128到127 。您可以查看 Javadocs
或只是查看来源:
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:
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。我错误(对Marco Topolnik来说)。以上所有内容都与较旧的Java实现有关。对于 Java 7 ,可以使用系统属性实现:
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:
-Djava.lang.Integer.IntegerCache.high=<size>
或JVM设置:
-XX:AutoBoxCacheMax=<size>
UPD。 2 java.math.BigInteger
具有值 -16< = x< = 16 的硬编码缓存。来自:
UPD. 2 java.math.BigInteger
has hardcoded cache for values -16 <= x <= 16. From sources:
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);
}
这篇关于整数缓存有多大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!