Java 对象头中你可能不知道的事

写在前头

本文将通过 jol 工具包和 OpenJDK 源码来说明对象头中 hashCode 的设置。

为了不浪费大家的时间,先说结论:

  1. 对象创建完毕后,对象头中的 hashCode 为 0。
  2. 只有对象调用了从 Object 继承下来的 hashCode 方法,HotSpot 才会把对象 hashCode 写入对象头,否则不会写入。

验证

一、jol 输出验证

jol 全称 Java Object Layout,可以打印对象在堆里的真实布局。

可以直接去 maven 仓库搜索,得到的 maven 依赖如下:

<!-- https://mvnrepository.com/artifact/org.openjdk.jol/jol-core -->
<dependency>
    <groupId>org.openjdk.jol</groupId>
    <artifactId>jol-core</artifactId>
    <version>0.10</version>
</dependency>

我机器的 JVM 是 64 位的 HotSpot。

1.1 创建一个对象后,我们先直接输出其对象布局

验证代码:

import org.openjdk.jol.info.ClassLayout;

class Lear {
    boolean state;
}

public class App {
    public static void main( String[] args ) {
        Lear lear = new Lear();
        System.out.println(ClassLayout.parseInstance(lear));
    }
}

输出:
输出
最后一列(VALUE),就是对象在内存中真实的样子。给出了 16 进制、二进制 和 十进制 三种打印输出。

其中,前 12 个字节就是对象头。12 个字节中前 8 个是 mark word,后 4 个是对象元数据指针。对象的 hashCode 就放在 mark word 中。

再来看一下对象头 mark word 中各个 bit 的含义:
mark word

我们的对象现在处于无锁状态,所以对应的 hashCode 为 mark word 中间的 31 个比特。

从输出中可以看出,现在对象的 hashCode 为 0。(我的机器按小端模式存储数据。)

1.2 调用从 Object 继承下来的 hashCode 方法

验证代码:

import org.openjdk.jol.info.ClassLayout;

class Lear {
    boolean state;
}

public class App {
    public static void main( String[] args ) {
        Lear lear = new Lear();
        System.out.println("对象 hashCode:"+lear.hashCode());
        System.out.println(ClassLayout.parseInstance(lear).toPrintable());
    }
}

输出:
输出
由于我的机器按小端模式存储数据,所以对象的 mark word 应该读作,
16 进制:0x00 00 00 01 4a e5 a5 01
二进制:0b00000000 00000000 00000000 0[0000001 01001010 11100101 10100101] 00000001

取出中间 31 位的 hashCode:0b0000001 01001010 11100101 10100101
对应的十进制就为:21685669

1.3 调用自己重写的 hashCode 方法

验证代码:

import org.openjdk.jol.info.ClassLayout;

class Lear {
    boolean state;

    @Override
    public int hashCode() {
        return 255;
    }
}

public class App {
    public static void main( String[] args ) {
        Lear lear = new Lear();
        System.out.println("对象 hashCode:"+lear.hashCode());
        System.out.println(ClassLayout.parseInstance(lear).toPrintable());
    }
}

输出:
输出
可以看到对象的 hashCode 并未写入对象头。

二、OpenJDK 源码验证

OpenJDK 源码链接:http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/3462d04401ba/src/share/native/java/lang/Object.c ,查看 Object.c 文件,可以看到 hashCode 的方法被注册成由 JVM_IHashCode 方法指针来处理。

static JNINativeMethod methods[] = {  
    {"hashCode",    "()I",                    (void *)&JVM_IHashCode},
    {"wait",        "(J)V",                   (void *)&JVM_MonitorWait},  
    {"notify",      "()V",                    (void *)&JVM_MonitorNotify},  
    {"notifyAll",   "()V",                    (void *)&JVM_MonitorNotifyAll},  
    {"clone",       "()Ljava/lang/Object;",   (void *)&JVM_Clone},  
};  

而 JVM_IHashCode 方法指针在 openjdk\hotspot\src\share\vm\prims\jvm.cpp 中定义为:

JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))  
  JVMWrapper("JVM_IHashCode");  
  // as implemented in the classic virtual machine; return 0 if object is NULL  
  return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ;  
JVM_END 

从而得知,真正计算获得 hashCode 的值是 ObjectSynchronizer::FastHashCode。

ObjectSynchronizer::FastHashCode 方法的实现

在 openjdk\hotspot\src\share\vm\runtime\synchronizer.cpp 找到其实现方法。

intptr_t ObjectSynchronizer::FastHashCode (Thread * Self, oop obj) {
  if (UseBiasedLocking) {
    // NOTE: many places throughout the JVM do not expect a safepoint
    // to be taken here, in particular most operations on perm gen
    // objects. However, we only ever bias Java instances and all of
    // the call sites of identity_hash that might revoke biases have
    // been checked to make sure they can handle a safepoint. The
    // added check of the bias pattern is to avoid useless calls to
    // thread-local storage.
    if (obj->mark()->has_bias_pattern()) {
      // Box and unbox the raw reference just in case we cause a STW safepoint.
      Handle hobj (Self, obj) ;
      // Relaxing assertion for bug 6320749.
      assert (Universe::verify_in_progress() ||
              !SafepointSynchronize::is_at_safepoint(),
             "biases should not be seen by VM thread here");
      BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());
      obj = hobj() ;
      assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
    }
  }

  // hashCode() is a heap mutator ...
  // Relaxing assertion for bug 6320749.
  assert (Universe::verify_in_progress() ||
          !SafepointSynchronize::is_at_safepoint(), "invariant") ;
  assert (Universe::verify_in_progress() ||
          Self->is_Java_thread() , "invariant") ;
  assert (Universe::verify_in_progress() ||
         ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant") ;

  ObjectMonitor* monitor = NULL;
  markOop temp, test;
  intptr_t hash;
  markOop mark = ReadStableMark (obj);

  // object should remain ineligible for biased locking
  assert (!mark->has_bias_pattern(), "invariant") ;

  if (mark->is_neutral()) {
    hash = mark->hash();              // this is a normal header
    if (hash) {                       // if it has hash, just return it
      return hash;
    }
    hash = get_next_hash(Self, obj);  // allocate a new hash code
    temp = mark->copy_set_hash(hash); // merge the hash code into header
    // use (machine word version) atomic operation to install the hash
    test = (markOop) Atomic::cmpxchg_ptr(temp, obj->mark_addr(), mark);
    if (test == mark) {
      return hash;
    }
    // If atomic operation failed, we must inflate the header
    // into heavy weight monitor. We could add more code here
    // for fast path, but it does not worth the complexity.
  } else if (mark->has_monitor()) {
    monitor = mark->monitor();
    temp = monitor->header();
    assert (temp->is_neutral(), "invariant") ;
    hash = temp->hash();
    if (hash) {
      return hash;
    }
    // Skip to the following code to reduce code size
  } else if (Self->is_lock_owned((address)mark->locker())) {
    temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned
    assert (temp->is_neutral(), "invariant") ;
    hash = temp->hash();              // by current thread, check if the displaced
    if (hash) {                       // header contains hash code
      return hash;
    }
    // WARNING:
    //   The displaced header is strictly immutable.
    // It can NOT be changed in ANY cases. So we have
    // to inflate the header into heavyweight monitor
    // even the current thread owns the lock. The reason
    // is the BasicLock (stack slot) will be asynchronously
    // read by other threads during the inflate() function.
    // Any change to stack may not propagate to other threads
    // correctly.
  }

  // Inflate the monitor to set hash code
  monitor = ObjectSynchronizer::inflate(Self, obj);
  // Load displaced header and check it has hash code
  mark = monitor->header();
  assert (mark->is_neutral(), "invariant") ;
  hash = mark->hash();
  if (hash == 0) {
    hash = get_next_hash(Self, obj);
    temp = mark->copy_set_hash(hash); // merge hash code into header
    assert (temp->is_neutral(), "invariant") ;
    test = (markOop) Atomic::cmpxchg_ptr(temp, monitor, mark);
    if (test != mark) {
      // The only update to the header in the monitor (outside GC)
      // is install the hash code. If someone add new usage of
      // displaced header, please update this code
      hash = test->hash();
      assert (test->is_neutral(), "invariant") ;
      assert (hash != 0, "Trivial unexpected object/monitor header usage.");
    }
  }
  // We finally get the hash
  return hash;
}

该方法表明,如果对象头中的哈希值非零,就直接返回对象头中设置的哈希值,如果为零,则调用 get_next_hash 方法计算哈希值,并将哈希值写入对象头。

参考:Java Object.hashCode()返回的是对象内存地址?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章