JAVA object header structure details

This is the 10th day of my participation in the August More Text Challenge

📚 preface

Have you ever thought about Java objects in addition to some of our custom properties, there will be some things we do not understand, such as synchronized lock as an object, then how to record the lock state? Let’s take a look at the magic of objects!

🥠 Object memory layout

In addition to our custom attributes, in the HotSpot virtual machine, objects can be divided into three areas in memory: object header, instance data, and alignment fill. These three areas make up a complete object!

  • Object headers: See article below for an explanation
  • Instance data: Stores the attribute data of a class, including the attribute information of its parent class
  • The vm requires that the start address of the object be a multiple of 8 bytes. Padding data does not have to exist, just for byte alignment

🍬 object head

The object header stores a lot of internal Java information about the object, such as hash code, age of the object, object lock, lock status flag, bias lock (thread) ID, bias time, etc. Java object headers generally occupy two machine codes

PS: in a 32-bit virtual machine, one machine code is 4 bytes, which is 32bit, and in a 64-bit virtual machine, one machine code is 8 bytes, which is 64bit!

If the object is an array, three machine codes are required, because the JVM can determine the size of a Java object from its metadata information, but not from the array metadata, so a section is used to record the length of the array. The header of the HotSpot virtual machine object contains two parts of information, the first part is the Mark Word, the second part is the class Pointer, and if it is an array object, then the array length!

🍭 Mark Word

Mark Word is used to store the runtime data of the object itself, such as hash codes, GC generation ages, lock status flags, locks held by threads, biased thread ids, biased timestamps, and so on. , the length of the data is 32 Bits and 64 Bits respectively on 32-bit and 64-bit VMS (regardless of the scenario where the compression pointer is enabled). It is officially called a Mark Word. Objects need to store a lot of runtime data, in fact, has exceeded the limit of 32, 64 Bitmap structure can record, but the object header information is independent of the data of the object itself additional storage costs, considering the space efficiency of the VIRTUAL machine, Mark Word is designed as a flexible data structure to store as much information as possible in a very small space. It reuses its storage space according to the state of the object. For example, if the object is not locked in the 32-bit HotSpot VIRTUAL machine, 25Bits of the 32 Bits of Mark Word space are used to store the object HashCode, 4Bits are used to store the age of the object, 2Bits are used to store the lock flag bit, and 1Bit is fixed to 0. The contents of objects stored in other states (lightweight lock, heavyweight lock, GC mark, biased) are shown in the following table.

32-BIT VMS

64-bit VM

🍓 Practices

Next, use JOL to analyze runtime object header state analysis:

Introduce the jolMaven dependency below

< the dependency > < groupId > org. Its. Jol < / groupId > < artifactId > jol - core < / artifactId > < version > 0.16 < / version > <scope>provided</scope> </dependency>Copy the code

Output object status information

System.out.println( ClassLayout.parseInstance(object).toPrintable() );
Copy the code


🥚 summary:

Today’s object memory parsing is over here, if you want to understand more deeply, you can go to read next week’s “Deep Understanding of Java Virtual Machine” by Teacher Zhiming, this book on the JVM of a parsing want to be thorough!