为什么JVM允许设置“高”? IntegerCache的值,但不是“低”? [英] Why does the JVM allow to set the "high" value for the IntegerCache, but not the "low"?
问题描述
我们都知道Java的整数
(以及其他一些类型)的缓存数量范围 [ - 128,127]
被认为是常用。
We all know that Java has a cache for Integer
(and some other types) for number in the range [-128, 127]
which are considered to be "commonly used".
缓存的设计如下:
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) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
我知道我可以通过向JVM提供参数来扩展 high
值:
I know that I can extend the high
value by giving a parameter to the JVM :
java -Djava.lang.Integer.IntegerCache.high=xxxx Aclass.class
我不明白为什么我们不允许覆盖 low
值?
What I don't understand is why aren't we allowed to override the low
value ?
注意我并没有试图找到一种解决方法,而是理解为什么不允许出于某些不明原因。
Note that I was not trying to find a workaround, but instead understand why it is not allowed for some obscure reasons.
推荐答案
发现已经有一个未解决的 RFP 就此而言。
Found out that there is already an unresolved RFP on this.
Joe Darcy就此问题发表评论:
Joe Darcy commented on the issue :
可以想象,缓存更大范围的
负数也会有所帮助,但是然而,没有迫切需要
这样做。
It is conceivable that it would be helpful to cache a greater range of negative number as well, but as of yet there has been no pressing need to do so.
请注意,RFP提供了一个糟糕的解决方法,如果有人有兴趣在处理之前使用它:
Note that the RFP offer a poor workaround if ever someone was interested in using this before it is handled :
除了实现你的实现你的缓存大小之外,没有办法增加
在转到java.lang.Integer类之前,首先拥有自己的缓存和
。
这显然不太好,因为它需要调用其他类
,而不仅仅是默认使用Integer.valueOf(int i)调用。
这篇关于为什么JVM允许设置“高”? IntegerCache的值,但不是“低”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!