内容目录
前言
之前遇到一个问题,想打印下对象的内存地址,找了很多博客,发现很多其实说的都不太对,所以有了这篇文章,和大家分享下如何正确查看
一、引入
先看一个简单的程序,一般我们打印对象,大部分是下面的情况,可能会重写下 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()); }
toString 中的 hashCode () 是一个 native 方法,就是我们要的地址值?我们往下分析看看
二、分析
我们通过 hotspot 源码来看看,如下:
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() ; } else if (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 ; } else if (hashCode == 2) { value = 1 ; // for sensitivity testing } else if (hashCode == 3) { value = ++GVars.hcSequence ; } else if (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 启动参数来模拟一下
1、设置 - XX:hashCode=2,可以看到输出结果是固定值 1
// -XX:hashCode=2 for (int i = 0; i < 3; i++) { Frolan frolan = new Frolan(); System.out.println(frolan.hashCode()); } // 输出结果 1 1 1
2、设置 - XX:hashCode=3,可以看到输出结果是递增的值
// -XX:hashCode=3 for (int i = 0; i < 3; i++) { Frolan frolan = new Frolan(); System.out.println(frolan.hashCode()); } // 输出结果 714 715 716
3、默认值
前面主要模拟了 hashCode=2 和 3 的情况,其它情况没那么好验证,感兴趣的话,大家测试交流下~
如果我们不设置启动参数,默认值是什么?
通过命令可以看到,默认值是 5,所以在我们不设置的情况下,打印的并不是对象的内存地址
java -XX:+PrintFlagsFinal -version | grep hashCode intx 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)
另一种说法,hashCode=4,就是对象的内存地址,这个也是错的,它拿的其实是 oop 的地址,System.identityHashCode () 方法拿到的也是这个地址
四、正确获取
如果想要获取真正的对象地址,可以使用 Java 对象布局 (JOL) 工具
1、引入依赖
<dependency> <groupId>org.openjdk.jol</groupId> <artifactId>jol-core</artifactId> <version>0.9</version> </dependency>
2、具体代码
Frolan frolan = new Frolan(); System.out.println(VM.current().addressOf(frolan)); // 输出结果 31867940040