클래스 Integer
에는 Integer
값을 캐시하는 캐시가 있습니다. 따라서 메서드 valueOf
을 사용하거나 inboxing을 사용하면 새 값이 인스턴스화되지 않고 캐시에서 가져옵니다.정수 캐시의 크기는 얼마입니까?
기본 캐시 크기는 127
이지만 VM 설정으로 인해 확장 될 수 있음을 알고 있습니다. 내 질문입니다 : 얼마나 큰이 설정에서 캐시 크기의 기본값입니다 및이 값을 조작 할 수 있습니까? 이 값은 사용하는 VM (32 비트 또는 64 비트)에 따라 다릅니 까?
이제 레거시 코드를 조정 중이며 int에서 Integer로 변환해야 할 수도 있습니다.
명확한 설명 : 나는 자바 소스
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);
}
에서 발견 한 다음 코드는 그래서 캐시를 구성 할 수 있습니다 생각합니다. Javadoc 가입일
자바 소스 코드입니다. VM 설정은이 설정과 아무 관련이 없습니다. –
그것은 -128에서 +127까지입니다. vm에서 구성을 허용하지 않는 한 변경 될 수 있다고 생각하지 않습니다. – vikingsteve
시스템 속성'java.lang.Integer.IntegerCache.high'를 사용하여 다른 한계를 설정할 수 있습니다. 그것은 모두 소스 코드에 있습니다. –