当前位置: 首页 > news >正文

Integer中缓存池讲解

文章目录

  • 一、简介
  • 二、实现原理
  • 三、修改缓存范围

一、简介

Integer缓存池是一种优化技术,用于提高整数对象的重用和性能。在Java中,对于整数值在 -128 到 127 之间的整数对象,会被放入缓存池中,以便重复使用。这是因为在这个范围内的整数值被频繁使用,因此重用这些对象可以节省内存和提高性能。当使用自动装箱机制创建整数对象时,如果对象的值在缓存池范围内,会直接返回缓存池中的对象,而不是创建新的对象。这个特性可以通过调用Integer.valueOf(int)方法来实现。

根据通过设置JVM-XX:AutoBoxCacheMax=可以来修改缓存的最大值,最小值改不了

二、实现原理

底层实现的原理是int 在自动装箱的时候会调用IntegervalueOf进而用到了 IntegerCache。

    public static Integer valueOf(String s) throws NumberFormatException {return Integer.valueOf(parseInt(s, 10));}

没有太多复杂的步骤,只需要判断给定的值是否在指定范围内,如果是的话,则从 IntegerCache 中获取已经预先初始化好的缓存值。

这些缓存值在静态块中被初始化。

/*** Cache to support the object identity semantics of autoboxing for values between* -128 and 127 (inclusive) as required by JLS.** The cache is initialized on first usage.  The size of the cache* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.* During VM initialization, java.lang.Integer.IntegerCache.high property* may be set and saved in the private system properties in the* sun.misc.VM class.*/private static class IntegerCache {static final int low = -128;static final int high;static final Integer cache[];static {// high value may be configured by propertyint 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_VALUEh = 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;}private IntegerCache() {}}

这里还有一个有趣的面试题,即在 Integer 类中,数值在 127 以内的两个 Integer 对象会被认为是相等的,而超过 127 的数值则不相等。这是因为在 Java 中,对于数值在 -128 到 127 之间的整型对象,会使用 IntegerCache 中预先初始化的对象。因此,当数值在该范围内时,它们实际上是同一个对象,因此相等性比较会返回 true。这种行为不仅适用于 Integer 类,也适用于 Long 类。

    public static Long valueOf(long l) {final int offset = 128;if (l >= -128 && l <= 127) { // will cachereturn LongCache.cache[(int)l + offset];}return new Long(l);}

对于小数类型的 Float 和 Double,不会有像整数类型那样的缓存机制。因为小数类型的数值范围非常广泛,无法事先预先缓存所有可能的数值。因此,对于 Float 和 Double 类型的对象,相等性比较会根据它们的实际数值来判断,而不是基于对象的引用。所以,即使两个 Float 或两个 Double 对象的数值相同,它们也不会被认为是相等的。

三、修改缓存范围

IntegerCache

通过注释可知缓存值可以修改

IntegerCache

验证:默认情况下

integer

添加JVM参数 -XX:AutoBoxCacheMax=500

AutoBoxCacheMax

再次执行:

integer
发现i1和i2相等返回true,说明参数生效了。

http://www.lryc.cn/news/125119.html

相关文章:

  • PHP Smarty模板如何与MVC框架集成?
  • spring cloud alibaba 应用无法注册到sentinel dashboard
  • 如何在vue3中加入markdown语法
  • R语言的物种气候生态位动态量化与分布特征模拟实践技术
  • 大数据Flink(六十一):Flink流处理程序流程和项目准备
  • C语言快速回顾(一)
  • Element Plus报错:ResizeObserver loop completed with undelivered notifications.
  • scope穿透(二)
  • 2023+HuggingGPT: Solving AI Tasks with ChatGPT and itsFriends in Hugging Face
  • Ant Design Mobile是什么?
  • 深入理解设计模式-行为型之模板(和回调区别联系)
  • LabVIEW控制通用工作台
  • 什么是事务,并发带来的事务问题以及事务隔离级别(图文详解)
  • 【MySQL】MySQL数据库的delete from table和truncate table之间的区别
  • 强制Edge或Chrome使用独立显卡【WIN10】
  • easyx图形库基础:3实现弹球小游戏
  • vue基础知识三:v-show和v-if有什么区别?使用场景分别是什么?
  • Python Opencv实践 - 图像旋转
  • 第五章 Opencv图像处理框架实战 5-10 文档扫描OCR识别
  • CentOS 7 源码制作openssh 9.4p1 rpm包 —— 筑梦之路
  • OpenCV图像处理——轮廓检测
  • 【论文阅读】基于深度学习的时序预测——Non-stationary Transformers
  • 开发者如何使用讯飞星火认知大模型API?
  • linux 系统中vi 编辑器和库的制作和使用
  • 麒麟arm架构 编译安装qt5.14.2
  • 【springmvc系】利用RequestBodyAdviceAdapter做接口鉴权
  • ROS学习笔记(三)---好用的终端Terminator
  • NFT Insider#102:The Sandbox重新上线LAND桥接服务,YGG加入Base生态
  • Webpack 的 sass-loader 在生产模式下最小化 CSS 问题
  • pytest自动化测试框架tep环境变量、fixtures、用例三者之间的关系