yeskery

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

基于OpenJDK 8

一直以为Java Object.hashCode()的结果就是通过对象的内存地址做相关运算得到的,但是无意在网上看到有相应的意见争论,故抽时间从源码层面验证了剖析了hashCode的默认计算方法。

先说结论:OpenJDK8 默认hashCode的计算方法是通过和当前线程有关的一个随机数+三个确定值,运用Marsaglia’s xorshift scheme随机数算法得到的一个随机数。和对象内存地址无关。

下面通过查找和分析OpenJDK8源码实现来一步步分析。

查找java.lang.Object.hashCode()源码

  1. public native int hashCode();

导出Object的JNI头文件

切换到Object.class文件所在目录,执行 javah -jni java.lang.Object,得到java_lang_Object.h文件,文件内容如下:

  1. /* DO NOT EDIT THIS FILE - it is machine generated */
  2. #include <jni.h>
  3. /* Header for class java_lang_Object */
  4. #ifndef _Included_java_lang_Object
  5. #define _Included_java_lang_Object
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /*
  10. * Class: java_lang_Object
  11. * Method: registerNatives
  12. * Signature: ()V
  13. */
  14. JNIEXPORT void JNICALL Java_java_lang_Object_registerNatives
  15. (JNIEnv *, jclass);
  16. /*
  17. * Class: java_lang_Object
  18. * Method: getClass
  19. * Signature: ()Ljava/lang/Class;
  20. */
  21. JNIEXPORT jclass JNICALL Java_java_lang_Object_getClass
  22. (JNIEnv *, jobject);
  23. /*
  24. * Class: java_lang_Object
  25. * Method: hashCode
  26. * Signature: ()I
  27. */
  28. JNIEXPORT jint JNICALL Java_java_lang_Object_hashCode
  29. (JNIEnv *, jobject);
  30. /*
  31. * Class: java_lang_Object
  32. * Method: clone
  33. * Signature: ()Ljava/lang/Object;
  34. */
  35. JNIEXPORT jobject JNICALL Java_java_lang_Object_clone
  36. (JNIEnv *, jobject);
  37. /*
  38. * Class: java_lang_Object
  39. * Method: notify
  40. * Signature: ()V
  41. */
  42. JNIEXPORT void JNICALL Java_java_lang_Object_notify
  43. (JNIEnv *, jobject);
  44. /*
  45. * Class: java_lang_Object
  46. * Method: notifyAll
  47. * Signature: ()V
  48. */
  49. JNIEXPORT void JNICALL Java_java_lang_Object_notifyAll
  50. (JNIEnv *, jobject);
  51. /*
  52. * Class: java_lang_Object
  53. * Method: wait
  54. * Signature: (J)V
  55. */
  56. JNIEXPORT void JNICALL Java_java_lang_Object_wait
  57. (JNIEnv *, jobject, jlong);
  58. #ifdef __cplusplus
  59. }
  60. #endif
  61. #endif

查看Object的native方法实现

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

  1. static JNINativeMethod methods[] = {
  2. {"hashCode", "()I", (void *)&JVM_IHashCode},//hashcode的方法指针JVM_IHashCode
  3. {"wait", "(J)V", (void *)&JVM_MonitorWait},
  4. {"notify", "()V", (void *)&JVM_MonitorNotify},
  5. {"notifyAll", "()V", (void *)&JVM_MonitorNotifyAll},
  6. {"clone", "()Ljava/lang/Object;", (void *)&JVM_Clone},
  7. };

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

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

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

ObjectSynchronizer::fashHashCode方法的实现

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

  1. intptr_t ObjectSynchronizer::FastHashCode (Thread * Self, oop obj) {
  2. if (UseBiasedLocking) {
  3. // NOTE: many places throughout the JVM do not expect a safepoint
  4. // to be taken here, in particular most operations on perm gen
  5. // objects. However, we only ever bias Java instances and all of
  6. // the call sites of identity_hash that might revoke biases have
  7. // been checked to make sure they can handle a safepoint. The
  8. // added check of the bias pattern is to avoid useless calls to
  9. // thread-local storage.
  10. if (obj->mark()->has_bias_pattern()) {
  11. // Box and unbox the raw reference just in case we cause a STW safepoint.
  12. Handle hobj (Self, obj) ;
  13. // Relaxing assertion for bug 6320749.
  14. assert (Universe::verify_in_progress() ||
  15. !SafepointSynchronize::is_at_safepoint(),
  16. "biases should not be seen by VM thread here");
  17. BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());
  18. obj = hobj() ;
  19. assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
  20. }
  21. }
  22. // hashCode() is a heap mutator ...
  23. // Relaxing assertion for bug 6320749.
  24. assert (Universe::verify_in_progress() ||
  25. !SafepointSynchronize::is_at_safepoint(), "invariant") ;
  26. assert (Universe::verify_in_progress() ||
  27. Self->is_Java_thread() , "invariant") ;
  28. assert (Universe::verify_in_progress() ||
  29. ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant") ;
  30. ObjectMonitor* monitor = NULL;
  31. markOop temp, test;
  32. intptr_t hash;
  33. markOop mark = ReadStableMark (obj);
  34. // object should remain ineligible for biased locking
  35. assert (!mark->has_bias_pattern(), "invariant") ;
  36. if (mark->is_neutral()) {
  37. hash = mark->hash(); // this is a normal header
  38. if (hash) { // if it has hash, just return it
  39. return hash;
  40. }
  41. hash = get_next_hash(Self, obj); // allocate a new hash code
  42. temp = mark->copy_set_hash(hash); // merge the hash code into header
  43. // use (machine word version) atomic operation to install the hash
  44. test = (markOop) Atomic::cmpxchg_ptr(temp, obj->mark_addr(), mark);
  45. if (test == mark) {
  46. return hash;
  47. }
  48. // If atomic operation failed, we must inflate the header
  49. // into heavy weight monitor. We could add more code here
  50. // for fast path, but it does not worth the complexity.
  51. } else if (mark->has_monitor()) {
  52. monitor = mark->monitor();
  53. temp = monitor->header();
  54. assert (temp->is_neutral(), "invariant") ;
  55. hash = temp->hash();
  56. if (hash) {
  57. return hash;
  58. }
  59. // Skip to the following code to reduce code size
  60. } else if (Self->is_lock_owned((address)mark->locker())) {
  61. temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned
  62. assert (temp->is_neutral(), "invariant") ;
  63. hash = temp->hash(); // by current thread, check if the displaced
  64. if (hash) { // header contains hash code
  65. return hash;
  66. }
  67. // WARNING:
  68. // The displaced header is strictly immutable.
  69. // It can NOT be changed in ANY cases. So we have
  70. // to inflate the header into heavyweight monitor
  71. // even the current thread owns the lock. The reason
  72. // is the BasicLock (stack slot) will be asynchronously
  73. // read by other threads during the inflate() function.
  74. // Any change to stack may not propagate to other threads
  75. // correctly.
  76. }
  77. // Inflate the monitor to set hash code
  78. monitor = ObjectSynchronizer::inflate(Self, obj);
  79. // Load displaced header and check it has hash code
  80. mark = monitor->header();
  81. assert (mark->is_neutral(), "invariant") ;
  82. hash = mark->hash();
  83. if (hash == 0) {
  84. hash = get_next_hash(Self, obj);
  85. temp = mark->copy_set_hash(hash); // merge hash code into header
  86. assert (temp->is_neutral(), "invariant") ;
  87. test = (markOop) Atomic::cmpxchg_ptr(temp, monitor, mark);
  88. if (test != mark) {
  89. // The only update to the header in the monitor (outside GC)
  90. // is install the hash code. If someone add new usage of
  91. // displaced header, please update this code
  92. hash = test->hash();
  93. assert (test->is_neutral(), "invariant") ;
  94. assert (hash != 0, "Trivial unexpected object/monitor header usage.");
  95. }
  96. }
  97. // We finally get the hash
  98. return hash;
  99. }

该方法中

  1. // Load displaced header and check it has hash code
  2. mark = monitor->header();
  3. assert (mark->is_neutral(), "invariant") ;
  4. hash = mark->hash();
  5. if (hash == 0) {
  6. hash = get_next_hash(Self, obj);
  7. ...
  8. }

对hash值真正进行了计算,查看get_next_hash方法源码http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/87ee5ee27509/src/share/vm/runtime/synchronizer.cpp#l555

  1. static inline intptr_t get_next_hash(Thread * Self, oop obj) {
  2. intptr_t value = 0 ;
  3. if (hashCode == 0) {
  4. // This form uses an unguarded global Park-Miller RNG,
  5. // so it's possible for two threads to race and generate the same RNG.
  6. // On MP system we'll have lots of RW access to a global, so the
  7. // mechanism induces lots of coherency traffic.
  8. value = os::random() ;
  9. } else
  10. if (hashCode == 1) {
  11. // This variation has the property of being stable (idempotent)
  12. // between STW operations. This can be useful in some of the 1-0
  13. // synchronization schemes.
  14. intptr_t addrBits = cast_from_oop<intptr_t>(obj) >> 3 ;
  15. value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ;
  16. } else
  17. if (hashCode == 2) {
  18. value = 1 ; // for sensitivity testing
  19. } else
  20. if (hashCode == 3) {
  21. value = ++GVars.hcSequence ;
  22. } else
  23. if (hashCode == 4) {
  24. value = cast_from_oop<intptr_t>(obj) ;
  25. } else {
  26. // Marsaglia's xor-shift scheme with thread-specific state
  27. // This is probably the best overall implementation -- we'll
  28. // likely make this the default in future releases.
  29. unsigned t = Self->_hashStateX ;
  30. t ^= (t << 11) ;
  31. Self->_hashStateX = Self->_hashStateY ;
  32. Self->_hashStateY = Self->_hashStateZ ;
  33. Self->_hashStateZ = Self->_hashStateW ;
  34. unsigned v = Self->_hashStateW ;
  35. v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ;
  36. Self->_hashStateW = v ;
  37. value = v ;
  38. }
  39. value &= markOopDesc::hash_mask;
  40. if (value == 0) value = 0xBAD ;
  41. assert (value != markOopDesc::no_hash, "invariant") ;
  42. TEVENT (hashCode: GENERATE) ;
  43. return value;
  44. }

对于OpenJDK8版本,其默认配置http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/87ee5ee27509/src/share/vm/runtime/globals.hpp#l1127 为:

  1. product(intx, hashCode, 5, \
  2. "(Unstable) select hashCode generation algorithm") \

其对应的hashCode计算方案为:

  1. // Marsaglia's xor-shift scheme with thread-specific state
  2. // This is probably the best overall implementation -- we'll
  3. // likely make this the default in future releases.
  4. unsigned t = Self->_hashStateX ;
  5. t ^= (t << 11) ;
  6. Self->_hashStateX = Self->_hashStateY ;
  7. Self->_hashStateY = Self->_hashStateZ ;
  8. Self->_hashStateZ = Self->_hashStateW ;
  9. unsigned v = Self->_hashStateW ;
  10. v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ;
  11. Self->_hashStateW = v ;
  12. value = v ;

其中Thread->_hashStateX, Thread->_hashStateY, Thread->_hashStateZ, Thread->_hashStateW在http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/87ee5ee27509/src/share/vm/runtime/thread.cpp#I263 有定义:

  1. // thread-specific hashCode stream generator state - Marsaglia shift-xor form
  2. _hashStateX = os::random() ;
  3. _hashStateY = 842502087 ;
  4. _hashStateZ = 0x8767 ; // (int)(3579807591LL & 0xffff) ;
  5. _hashStateW = 273326509 ;

所以,JDK8 的默认hashCode的计算方法是通过和当前线程有关的一个随机数+三个确定值,运用Marsaglia’s xorshift scheme随机数算法得到的一个随机数。对xorshift算法有兴趣可以参考原论文:https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf
xorshift是由George Marsaglia发现的一类伪随机数生成器,其通过移位和与或计算,能够在计算机上以极快的速度生成伪随机数序列。其算法的基本实现如下:

  1. unsigned long xor128(){
  2. static unsigned long x=123456789,y=362436069,z=521288629,w=88675123;
  3. unsigned long t;
  4. t=(xˆ(x<<11));x=y;y=z;z=w; return( w=(wˆ(w>>19))ˆ(tˆ(t>>8)) );

这就和上面计算hashCode的OpenJDK代码对应了起来。

其他几类hashCode计算方案:

  1. if (hashCode == 0) {
  2. // This form uses an unguarded global Park-Miller RNG,
  3. // so it's possible for two threads to race and generate the same RNG.
  4. // On MP system we'll have lots of RW access to a global, so the
  5. // mechanism induces lots of coherency traffic.
  6. value = os::random() ;
  7. }
  • hashCode == 1
    此类方案将对象的内存地址,做移位运算后与一个随机数进行异或得到结果
  1. if (hashCode == 1) {
  2. // This variation has the property of being stable (idempotent)
  3. // between STW operations. This can be useful in some of the 1-0
  4. // synchronization schemes.
  5. intptr_t addrBits = cast_from_oop<intptr_t>(obj) >> 3 ;
  6. value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ;
  7. }
  • hashCode == 2
    此类方案返回固定的1
  1. if (hashCode == 2) {
  2. value = 1 ; // for sensitivity testing
  3. }
  • hashCode == 3
    此类方案返回一个自增序列的当前值
  1. if (hashCode == 3) {
  2. value = ++GVars.hcSequence ;
  3. }
  • hashCode == 4
    此类方案返回当前对象的内存地址
  1. if (hashCode == 4) {
  2. value = cast_from_oop<intptr_t>(obj) ;
  3. }

可以通过在JVM启动参数中添加-XX:hashCode=4,改变默认的hashCode计算方式。

参考资料:

  1. https://srvaroa.github.io/jvm/java/openjdk/biased-locking/2017/01/30/hashCode.html
  2. https://en.wikipedia.org/wiki/Xorshift
  3. http://www.cnblogs.com/mengyou0304/p/4763220.html
  4. http://stackoverflow.com/questions/2427631/how-is-hashcode-calculated-in-java
  5. http://hllvm.group.iteye.com/group/topic/39183

本文转载自:https://www.jianshu.com/p/be943b4958f4

评论

Post comment Click Refresh verification code

Tip

The feature is not yet available