I found cache mechanism is improved in jdk 1.6 or above jdk versions.
In jdk 1.5 the cache array in Integer is a fixed one, see
static final Integer cache[] = new Integer[-(-128) + 127 + 1];
In jdk 1.6 or above version, an method named getAndRemoveCacheProperties
and an IntegerCache.high
property have been added to Integer class,
like,
// value of java.lang.Integer.IntegerCache.high property (obtained during VM init)
private static String integerCacheHighPropValue;
static void getAndRemoveCacheProperties() {
if (!sun.misc.VM.isBooted()) {
Properties props = System.getProperties();
integerCacheHighPropValue =
(String)props.remove("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null)
System.setProperties(props); // remove from system props
}
}
With this change, it is allowed to configure a highest value for cache and to use a new cache range. (-128 <= cachedValue <= highestValue
).
*Here are my questions:*
Q#1 Why the cache range is using [-128 ~ 127] in jdk 1.5 or default cache of jdk 1.6 or above version? Is it just to support bytes
and char '\u0000'~ 'u007f'
?
Q#2 What is the advantage to specify a high value for cache range in jdk 1.6 or above version? What kind of appilication or sceen is suitable for us to do so?
Please help me out with these questions. Thank you very much in advance.
Below is the Source code for IntegerCache and valueOf(int i) in Integer class. It just for referance.
jdk 1.5
private static class IntegerCache {
private IntegerCache(){}
static final Integer cache[] = new Integer[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}
}
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);
}
jdk 1.6
private static class IntegerCache {
static final int high;
static final Integer cache[];
static {
final int low = -128;
// high value may be configured by property
int h = 127;
if (integerCacheHighPropValue != null) {
// Use Long.decode here to avoid invoking methods that
// require Integer's autoboxing cache to be initialized
int i = Long.decode(integerCacheHighPropValue).intValue();
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) {
if(i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}
Java Integer Cache Implementation: In Java 5, a new feature was introduced to save the memory and improve performance for Integer type objects handling. Integer objects are cached internally and reused via the same referenced objects. This is applicable for Integer values in the range between –128 to +127.
The Java Object Cache provides caching for expensive or frequently used Java objects when the application servers use a Java program to supply their content. Cached Java objects may contain generated pages or may provide support objects within the program to assist in creating new content.
There are four types of caching are as follows: In-memory Caching. Database Caching. Web server Caching.
Memory objects are Java objects that the Java Object Cache manages. Memory objects are stored in the Java virtual machine (JVM) heap space as Java objects. Memory objects can hold HTML pages, the results of a database query, or any information that can be stored as a Java object.
Q#1 Why the cache range is using [-128 ~ 127] in jdk 1.5
Because that's the way they designed JDK 1.5.
or default cache of jdk 1.6 or above version?
Because that's the way they designed JDK 1.6.
Is it just to support bytes and char '\u0000'~ 'u007f' ?
In JDK 1.5, yes, in JDK 1.6+, no.
Q#2 What is the advantage to specify a high value for cache range in jdk 1.6 or above version?
So that values in a wider range get cached.
What kind of appilication or sceen is suitable for us to do so?
An application that frequently uses values in a wider range.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With