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

Java中如何打印对象内存地址?

先看一个简单的程序,一般我们打印对象,大部分是下面的情况,可能会重写下toString()方法,这个另说

Frolan frolan = new Frolan();
System.out.println(frolan);// 输出结果
com.test.admin.entity.Frolan@2b80d80f

这个结果其实是调用了Object.toString打印出来的,就是类路径名+@+hashCode的16进制数

public String toString() {return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

这里的hashCode()是一个native方法,就是我们要的地址值?

我们通过源码来分析一波

static inline intptr_t get_next_hash(Thread * Self, oop obj) {intptr_t value = 0 ;if (hashCode == 0) {// This form uses an unguarded global Park-Miller RNG,// so it's possible for two threads to race and generate the same RNG.// On MP system we'll have lots of RW access to a global, so the// mechanism induces lots of coherency traffic.value = os::random() ;} elseif (hashCode == 1) {// This variation has the property of being stable (idempotent)// between STW operations.  This can be useful in some of the 1-0// synchronization schemes.intptr_t addrBits = cast_from_oop<intptr_t>(obj) >> 3 ;value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ;} elseif (hashCode == 2) {value = 1 ;            // for sensitivity testing} elseif (hashCode == 3) {value = ++GVars.hcSequence ;} elseif (hashCode == 4) {value = cast_from_oop<intptr_t>(obj) ;} else {// Marsaglia's xor-shift scheme with thread-specific state// This is probably the best overall implementation -- we'll// likely make this the default in future releases.unsigned t = Self->_hashStateX ;t ^= (t << 11) ;Self->_hashStateX = Self->_hashStateY ;Self->_hashStateY = Self->_hashStateZ ;Self->_hashStateZ = Self->_hashStateW ;unsigned v = Self->_hashStateW ;v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ;Self->_hashStateW = v ;value = v ;}value &= markOopDesc::hash_mask;if (value == 0) value = 0xBAD ;assert (value != markOopDesc::no_hash, "invariant") ;TEVENT (hashCode: GENERATE) ;return value;
}

大概意思就是,会根据不同的hashCode返回不同的结果
hashCode=0,返回随机数
hashCode=1,将oop的地址做位运算、异或运算得到的结果
hashCode=2,固定值1
hashCode=3,返回递增序列当前值
hashCode=4,oop的地址
hashCode=其他值,简单理解为移位寄存器,线程安全

我们设置JVM启动参数来模拟一下

// -XX:hashCode=2
for (int i = 0; i < 3; i++) {Frolan frolan = new Frolan();System.out.println(frolan.hashCode());
}// 输出结果
1
1
1
// -XX:hashCode=3
for (int i = 0; i < 3; i++) {Frolan frolan = new Frolan();System.out.println(frolan.hashCode());
}// 输出结果
714
715
716

我们模拟了hashCode=2和3的情况,其它情况没那么好验证,感兴趣的话,后续大家一起交流下~

那么我们不设置启动参数,默认值是什么?

java -XX:+PrintFlagsFinal -version | grep hashCodeintx hashCode                     = 5                     {product}
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

可以看到默认值是5,所以我们不设置的情况下,打印的并不是对象的内存地址

网上很多说,hashCode=4,这里就是对象的内存地址,这个是错的,这里拿到的只是oop的地址,System.identityHashCode()方法拿到的也是这个地址

如果想要获取真正的对象地址,可以使用Java 对象布局 ( JOL ) 工具

引入依赖

<dependency><groupId>org.openjdk.jol</groupId><artifactId>jol-core</artifactId><version>0.9</version>
</dependency>
Frolan frolan = new Frolan();
System.out.println(VM.current().addressOf(frolan));// 输出结果
31867940040
http://www.lryc.cn/news/6159.html

相关文章:

  • CF1707E Replace
  • 【Hello Linux】Linux工具介绍 (make/makefile git)
  • 享元模式flyweight
  • Pulsar
  • 项目介绍 + 定长内存池设计及实现
  • Linux--线程安全的单例模式--自旋锁--0211
  • 图文解说S参数(进阶篇)
  • Sentinel源码阅读
  • 2023年浙江食品安全管理员考试真题题库及答案
  • Webstorm 代码没有提示,uniapp 标签报错
  • MySQL-Innodb引擎事务原理
  • Linux操作系统学习(了解环境变量)
  • 数据分析思维(六)|循环/闭环思维
  • C++:类和对象(下)
  • ASP.NET Core MVC 项目 AOP之IResultFilter和IAsyncResultFilter
  • jstack排查cpu占用高[复习]
  • 网络安全-Pyhton环境搭建
  • SpringBoot Mybatis 分页实战
  • 计算机断层扫描结肠镜和全自动骨密度仪在一次检查中的可行性
  • Java多级缓存是为了解决什么的?
  • MongoDB--》索引的了解及具体操作
  • Python open()函数详解:打开指定文件
  • CentOS Stream 9尝鲜安装教程
  • Ambire AdEx 2023 年路线图
  • 两种特征提取方法与深度学习方法对比的小型金属物体分类分析研究
  • 传奇私服搭建网站的几种方法
  • i.MX8MP平台开发分享(clock篇)- 各类clock的注册
  • java ssm计算机系统在线考试平台idea
  • C语言(字符串函数)
  • Maxwell工作流程详解