Java Integer的缓存策略

发布时间:2025-09-01 01:47:58 作者:益华网络 来源:undefined 浏览量(0) 点赞(0)
摘要:Java5为Integer的操作引入了一个新的特性,用来节省内存和提高性能。整型对象在内部实现中通过使用相同的对象引用实现了缓存和重用。上面的规则默认适用于整数区间 -128 到 +127(这个整数区间可以通过启动应用的虚拟机参数修改:-XX:AutoBoxCacheMax)。这种Integer缓存策

Java5为Integer的操作引入了一个新的特性,用来节省内存和提高性能。整型对象在内部实现中通过使用相同的对象引用实现了缓存和重用。

上面的规则默认适用于整数区间 -128 到 +127(这个整数区间可以通过启动应用的虚拟机参数修改:-XX:AutoBoxCacheMax)。这种Integer缓存策略仅在自动装箱(autoboxing)的时候有用,使用构造器创建的Integer对象不能被缓存。Java 编译器把原始类型自动转换为封装类的过程称为自动装箱(autoboxing),这相当于调用 valueOf 方法。 publicstaticInteger valueOf(int i){if(i >=IntegerCache.low && i <=IntegerCache.high)returnIntegerCache.cache[i +(-IntegerCache.low)];returnnewInteger(i);}

首先看代码:

publicclassTestInteger{publicstaticvoid main(String[] args){int i =128;Integer i2 =128;Integer i3 =newInteger(128);//Integer会自动拆箱为int,所以为trueSystem.out.println(i == i2);System.out.println(i == i3);System.out.println("**************");Integer i5 =127;//java在编译的时候,被翻译成-> Integer i5 = Integer.valueOf(127);Integer i6 =127;System.out.println(i5 == i6);//trueInteger i9 =128;Integer i10 =128;System.out.println(i9 == i10);//falseInteger ii5 =newInteger(127);System.out.println(i5 == ii5);//falseInteger i7 =newInteger(128);Integer i8 =newInteger(123);System.out.println(i7 == i8);//false}}

首先,7行和8行输出结果都为true,因为Integer和int比都会自动拆箱(jdk1.5以上)。

12行的结果为true,而15行则为false。java在编译Integer i5 = 127的时候,被翻译成-> Integer i5 = Integer.valueOf(127);所以关键就是看valueOf()函数了。只要看看valueOf()函数的源码就会明白了。JDK源码的 valueOf函数式这样的: publicstaticInteger valueOf(int i){assertIntegerCache.high >=127;if(i >=IntegerCache.low && i <=IntegerCache.high)returnIntegerCache.cache[i +(-IntegerCache.low)];returnnewInteger(i);}

看一下源码大家都会明白,对于-128到127之间的数,会进行缓存,Integer i5 = 127时,会将127进行缓存,下次再写Integer i6 = 127时,就会直接从缓存中取,就不会new了。所以12行的结果为true,而15行为false。

对于17行和20行,因为对象不一样,所以为false。

对于以上的情况总结如下: 无论如何,Integer与new Integer不会相等。不会经历拆箱过程,i3的引用指向堆,而i4指向专门存放他的内存(常量池),他们的内存地址不一样,所以为false两个都是非new出来的Integer,如果数在-128到127之间,则是true,否则为false。java在编译Integer i2 = 128的时候,被翻译成-> Integer i2 = Integer.valueOf(128);而valueOf()函数会对-128到127之间的数进行缓存两个都是new出来的,都为falseint和Integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比 AutoBoxCacheMax参数// IntegerCache,Integer类的内部类,注意它的属性都是定义为static finalprivatestaticclassIntegerCache{//缓存的下界,-128,不可变staticfinalint low =-128;//缓存上界,暂为nullstaticfinalint high;//缓存的整型数组staticfinalInteger cache[];static{// 缓存上界,可以通过JVM参数来配置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);//最大的数组值是Integer.MAX_VALUE h =Math.min(i,Integer.MAX_VALUE -(-low));} high = h; cache =newInteger[(high - low)+1];int j = low;for(int k =0; k < cache.length; k++) cache[k]=newInteger(j++);}privateIntegerCache(){}}

-XX:AutoBoxCacheMax这个参数是设置Integer缓存上限的参数,在VM初始化期间java.lang.Integer.IntegerCache.high属性可以被设置和保存在私有的系统属性sun.misc.VM class中。理论上讲,当系统需要频繁使用Integer时,或者说堆内存中存在大量的Integer对象时,可以考虑提高Integer缓存上限,避免JVM重复创造对象,提高内存的使用率,减少GC的频率,从而提高系统的性能。

理论归理论,这个参数能否提高系统系统关键还是要看堆中Integer对象到底有多少、以及Integer的创建的方式。如果堆中的Integer对象很少,重新设置这个参数并不会提高系统的性能。即使堆中存在大量的Integer对象,也要看Integer对象时如何产生的。 大部分Integer对象通过Integer.valueOf()产生。说明代码里存在大量的拆箱与装箱操作。这时候设置这个参数会系统性能有所提高。大部分Integer对象通过反射,new产生。这时候Integer对象的产生大部分不会走valueOf()方法,所以设置这个参数也是无济于事。 JDK中其他类似的缓存

Integer的缓存上限可以通过Java虚拟机参数修改,Byte、Short、Long、Character的缓存则没法修改。

Byte

privatestaticclassByteCache{privateByteCache(){}staticfinalByte cache[]=newByte[-(-128)+127+1];static{for(int i =0; i < cache.length; i++) cache[i]=newByte((byte)(i -128));}}publicstaticByte valueOf(byte b){finalint offset =128;returnByteCache.cache[(int)b + offset];}

Short

privatestaticclassShortCache{privateShortCache(){}staticfinalShort cache[]=newShort[-(-128)+127+1];static{for(int i =0; i < cache.length; i++) cache[i]=newShort((short)(i -128));}}publicstaticShort valueOf(short s){finalint offset =128;int sAsInt = s;if(sAsInt >=-128&& sAsInt <=127){// must cachereturnShortCache.cache[sAsInt + offset];}returnnewShort(s);}

Long

privatestaticclassLongCache{privateLongCache(){}staticfinalLong cache[]=newLong[-(-128)+127+1];static{for(int i =0; i < cache.length; i++) cache[i]=newLong(i -128);}}publicstaticLong valueOf(long l){finalint offset =128;if(l >=-128&& l <=127){// will cachereturnLongCache.cache[(int)l + offset];}returnnewLong(l);}

Character

privatestaticclassCharacterCache{privateCharacterCache(){}staticfinalCharacter cache[]=newCharacter[127+1];static{for(int i =0; i < cache.length; i++) cache[i]=newCharacter((char)i);}}publicstaticCharacter valueOf(char c){if(c <=127){// must cachereturnCharacterCache.cache[(int)c];}returnnewCharacter(c);}

示例:

publicclassAllCacheDemo{/** * 演示JDK内部缓存 */publicstaticvoid main(String[] args){Integer a =28;Integer b =28; println(a == b);Byte c =25;Byte d =25; println(c==d);Short p=12;Short q=12; println(p==q);Long x=127L;Long y=127L; println(x==y);Character m=M;Character n=M; println(m==n);}publicstaticvoid println(Object o){System.out.println(o);}}

作者:刘晓;花名:愚谷。

点击 阅读更多 查看更多详情

二维码

扫一扫,关注我们

声明:本文由【益华网络】编辑上传发布,转载此文章须经作者同意,并请附上出处【益华网络】及本页链接。如内容、图片有任何版权问题,请联系我们进行处理。

感兴趣吗?

欢迎联系我们,我们愿意为您解答任何有关网站疑难问题!

您身边的【网站建设专家】

搜索千万次不如咨询1次

主营项目:网站建设,手机网站,响应式网站,SEO优化,小程序开发,公众号系统,软件开发等

立即咨询 15368564009
在线客服
嘿,我来帮您!