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()返回的是對象內存地址?

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