Integer缓存原理与JVM调参应用

本文介绍了Java中Integer的缓存原理,包括包装类型的拆箱与装箱操作,Integer缓存如何在valueOf()方法中避免创建新对象。重点讨论了-XX:AutoBoxCacheMax参数的作用,它用于设置Integer缓存上限,以优化性能。内容涵盖何时调整此参数有效,以及与JIT编译器的关系。此外,还提及了通过jmap和jhat等工具监控堆中对象分布的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本数据类型的拆箱与装箱

jvm把一个包装类型自动转成基本数据类型叫做拆箱,反之叫做装箱。例如:


Integer integer = 2 ;//装箱 实际运行代码为Integer integer = Integer.valueOf(2);
int i = integer ;//装箱 实际运行代码为int i = integer.intValue();
</java>

Integer缓存原理

程序在执行装箱操作时,实际执行的代码为Integer.valueOf(number);我们查看这个方法的源码,发现number在一定范围内不创建新的对象,而是直接拿缓存。源码如下:

     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

low为固定值-128,high可以配置,默认值为127。最高位可以用 AutoBoxCacheMax=XXX来配置。在加载IntegerCache类时,完成对IntegerCache.high的初始化。

 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;
        }

事实上,不仅仅Integer有缓存机制,其他包装类型也有缓存操作也有缓存,只不过只有Integer可以配置上限。

AutoBoxCacheMax参数

-XX:AutoBoxCacheMax这个参数是设置Integer缓存上限的参数。理论上讲,当系统需要频繁使用Integer时,或者说堆内存中存在大量的Integer对象时,可以考虑提高Integer缓存上限,避免JVM重复创造对象,提高内存的使用率,减少GC的频率,从而提高系统的性能。
理论归理论,这个参数能否提高系统系统关键还是要看堆中Integer对象到底有多少、以及Integer的创建的方式。如果堆中的Integer对象很少,重新设置这个参数并不会提高系统的性能。即使堆中存在大量的Integer对象,也要看Integer对象时如何产生的。
1. 大部分Integer对象通过Integer.valueOf()产生。说明代码里存在大量的拆箱与装箱操作。这时候设置这个参数会系统性能有所提高。
2. 大部分Integer对象通过反射,new产生。这时候Integer对象的产生大部分不会走valueOf()方法,所以设置这个参数也是无济于事。

如何查看堆中的对象分布,需要了解一下JVM的监控工具jmap与jhat,以及对象查询语言(OQL),这部分内容在下一篇博客单独讲

## AutoBoxCacheMax与JIT ##

有时候JIT会根据AutoBoxCacheMax的大小来决定是否内联Integer的构造函数。直接给传送门吧,我之前在知乎提问与回答。
https://www.zhihu.com/question/58735131/answer/158460810

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值