[FYI] 关于Integer的自动缓存大小

For your information.

最近又看到有人提到java.lang.Integer的自动装箱的缓存大小问题。这里想提一下这个值在VM启动时是可配置的。下面例子使用32位Windows上的Sun JDK 1.6.0 update 18。

在Java语言规范第三版,5.1.7 Boxing Conversion中,
[quote="The Java Language Specification, 3rd Edition"]If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.[/quote]
这就是为什么符合规范的Java实现必须保证Integer的缓存至少要覆盖[-128, 127]的范围。

使用Oracle/Sun JDK 6,在server模式下,使用-XX:AutoBoxCacheMax=NNN参数即可将Integer的自动缓存区间设置为[-128,NNN]。注意区间的下界固定在-128不可配置。
在client模式下该参数无效。这个参数是server模式专有的,在[url=http://hg.openjdk.java.net/jdk6/jdk6/hotspot/file/d2f6f71320bd/src/share/vm/opto/c2_globals.hpp]c2_globals.hpp[/url]中声明,默认值是128;不过这个默认值在默认条件下不起作用,要手动设置它的值或者是开启-XX:+AggressiveOpts参数才起作用。

在设置了-XX:+AggressiveOpts启动参数后,AutoBoxCacheMax的默认值会被修改为20000并且生效。参考[url=http://hg.openjdk.java.net/jdk6/jdk6/hotspot/file/d2f6f71320bd/src/share/vm/runtime/arguments.cpp]arguments.cpp[/url]:
// Aggressive optimization flags  -XX:+AggressiveOpts
void Arguments::set_aggressive_opts_flags() {
#ifdef COMPILER2
if (AggressiveOpts || !FLAG_IS_DEFAULT(AutoBoxCacheMax)) {
if (FLAG_IS_DEFAULT(EliminateAutoBox)) {
FLAG_SET_DEFAULT(EliminateAutoBox, true);
}
if (FLAG_IS_DEFAULT(AutoBoxCacheMax)) {
FLAG_SET_DEFAULT(AutoBoxCacheMax, 20000);
}

// Feed the cache size setting into the JDK
char buffer[1024];
sprintf(buffer, "java.lang.Integer.IntegerCache.high=" INTX_FORMAT, AutoBoxCacheMax);
add_property(buffer);
}
// ...
#endif
}


测试代码:
// run with:
// java -server -XX:AutoBoxCacheMax=1000 TestAutoBoxCache

public class TestAutoBoxCache {
public static void main(String[] args) {
Integer a = 1000;
Integer b = 1000;
System.out.println(a == b);

Integer c = 1001;
Integer d = 1001;
System.out.println(c == d);

Integer e = 20000;
Integer f = 20000;
System.out.println(e == f);
}
}


在命令行上测试:
D:\>javac TestAutoBoxCache.java

D:\>java TestAutoBoxCache
false
false
false

D:\>java -server TestAutoBoxCache
false
false
false

D:\>java -Djava.lang.Integer.IntegerCache.high=1000 TestAutoBoxCache
true
false
false

D:\>java -server -Djava.lang.Integer.IntegerCache.high=1000 TestAutoBoxCache
true
false
false

D:\>java -Djava.lang.Integer.IntegerCache.high=1001 TestAutoBoxCache
true
true
false

D:\>java -server -Djava.lang.Integer.IntegerCache.high=1001 TestAutoBoxCache
true
true
false

D:\>java -XX:AutoBoxCacheMax=1000 TestAutoBoxCache
Unrecognized VM option 'AutoBoxCacheMax=1000'
Could not create the Java virtual machine.

D:\>java -server -XX:AutoBoxCacheMax=1000 TestAutoBoxCache
true
false
false

D:\>java -server -XX:AutoBoxCacheMax=1001 TestAutoBoxCache
true
true
false

D:\>java -server -XX:+AggressiveOpts TestAutoBoxCache
true
true
true

中间报[color=red]Unrecognized VM option 'AutoBoxCacheMax=1000'[/color]错误是因为这个参数只能在HotSpot Server VM上使用,在HotSpot Client VM上不支持。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值