1, the introduction

  • Java object headers are important; Synchronize, GC, HashCode, biasedLock, and ObjectMonitor all operate on object headers.

  • In fact, the entire Java can be said to be built on oop- Klass system, I believe from various technical blogs, books you know quite a lot. But basically all are under 32 bit virtual machine, may zhou Zhiming study notes too much.

  • This article is not going to talk about oop-Klass architecture (limited capabilities), but will only talk about common object headers for 64-bit virtual machines, jdk1.8.0_221, JVM parameters: -xx: -usecompressedoops.

2. Know OOP

  • One of the cornerstones of OO-klass, let’s start with OOP (actually oopDesc *)
typedef class oopDesc*                            oop;

class oopDesc{
    friend class VMStructs;
 private:
  volatile markOop  _mark;
  union _metadata {
    Klass*      _klass;
    narrowKlass _compressed_klass;
  } _metadata;
}
Copy the code

2.1 oop inheritance system

  • From jdK8, from the source can be seen xxOop is xxxOopDesc* alias.
typedef class oopDesc*                            oop;
typedef class   instanceOopDesc*            instanceOop;
typedef class   arrayOopDesc*                    arrayOop;
typedef class     objArrayOopDesc*            objArrayOop;
typedef class     typeArrayOopDesc*            typeArrayOop;
Copy the code
  • Mainly instanceOopDesc and arrayOopDesc. All inherit from oopDesc without extended field attributes, which will not be described below. The markword and klass attributes of instanceOopDesc and arrayOopDesc are derived from oopDesc.

2.2 、InstanceOopDesc

  • Normal Java objects are instanceOopDesc, whether of wrapper type or primitive type.
  • 🌰 came.

  • See that objects of normal type are also arranged with an Object header.

2.3, arrayOopDesc

  • ObjArrayOopDesc and typeArrayOopDesc inherit from arrayOopDesc, where the array type of the wrapper type is objArrayOopDesc and the basic type array is typeArrayOopDesc.

  • The oop properties mentioned above, arrayOopDesc is a bit special in that it not only has markword, klass, but also a Length property to record the length of an array. ArrayOopDesc extends no properties from arrayOopDesc. ArrayOopDesc extends no properties from arrayOopDesc. The length attribute is part of the object header. It does not exist as a separate attribute, but as a value stored in a fixed location after the object headers Markword and Klass.
  • As shown below, on a 64-bit VM, length appears at offset=16. It is by no means accidental that 3 and 4 are equal to the length of the array.

  • Note: The object header has nothing to do with the instance data. In fact, arrayOopDesc’s length belongs to the object header, not to the data.

2.4, markword

  • Markword is part of OOP, where HashCode, GC, lightweight/heavyweight/biased locks, generational age are all based on Markword. Markword starts at object header offset=0 and takes 8 bytes on a 64-bit virtual machine.

  • This is a picture you’ve seen many times, and from the name you can see that I’m right. So the question is, many people can see at a glance that your article above 8 fields 64 bit seems to see except one 1, you have no lock symbol, I can understand, hashcode you also eat? Do you write any number of objects hashcode is zero?
  • At first I was also surprised at my bone surprise (feel can brain mining, hand calculate MD5), just kidding… Java is so easy to hash, should I switch to PHP? You’re the one who doesn’t read enough. 🌰 came.

  • If you look at the picture carefully, you will understand. Note that markword=00000008807e2501, where unused25 bits, hashcode is equal to integers1.hashcode(), and both flag bits are ok.

  • Let’s look at a bias towards the lock state (flag bit 101)

3, summary

  • Java object header OOP is mainly introduced, including instance object header and array object header.
  • The markword in the object head combined with demo for a simple description, to throw off a brick to introduce jade. As for the markword changes that favor the upgrade process of locks, lightweight locks and heavyweight locks, there may be more details in the future. However, all are in the Java object header, the reader can be combined with the lock upgrade flowchart to verify their own.