Analysis of HotSpot Implementation of Java Object Header

In the HotSpot virtual machine, the layout of objects in memory is divided into three areas: object header, instance data and alignment padding.

object header

The object header consists of two parts: Mark Word and type pointer.

Mark Word

Mark Word is used to store the runtime data of the object itself, such as HashCode, GC generation age, lock status flags, locks held by threads, biased thread IDs, biased timestamps, etc. The seat length is the same.

type pointer

The type pointer points to the class metadata of the object, and the virtual machine uses this pointer to determine which class the object is an instance of.

markOop implementation

HotSpot implements Mark Word through the markOop type, and the specific implementation is located in the markOop.hppfile.
Since the object needs to store a lot of runtime data, considering the memory usage of the virtual machine, markOop is designed as a non-fixed data structure to store as much data as possible in a very small space, and reuse its own storage according to the state of the object. space, the markOop implementation of the 32-bit virtual machine is as follows:


hash:  save the hash code of the object
age:  save the generation age of the object
biased_lock:  biased lock flag
lock:  lock status flag
JavaThread*:  save the thread ID holding the biased lock
epoch:  save the biased timestamp

Different lock flags in markOop represent different lock states:


Different lock states store different data:


MarkOop provides a large number of methods for viewing the status of the current object header and updating the data of the object header, which provides the basis for the implementation of synchronized locks.

Source: http://www.jianshu.com/p/9c19eb0ea4d8

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324132690&siteId=291194637