JVM object memory layout and object access positioning (6)

Memory layout

Objects in the heap can be divided into three parts: object header, instance data, alignment and padding

1. Object Head

The HotSpot virtual machine object header contains two types of information. One type is used to store the runtime data of the object itself , such as hash code, GC generation age, lock status flags, and locks held by threads. The length of this part occupies 32 bits and 64 bits in 32-bit and 64-bit virtual machines, respectively, and is officially called **"Mark Word"**.
In the 32-bit space, 25 bits are used to store the hash code, 4 bits are used to store the age of the object generation, 2 bits are used to store the lock flag, and 1 bit is fixed to 0.
The other part : the type pointer, a pointer to its type metadata, and the pointer is used to determine which class instance the object is.

2. Example part

The useful information of the storage object, such as the content of various types of fields defined in the code, whether it is inherited by the parent class or defined by the subclass.

3. Align and fill

This part of the content does not necessarily exist and has no special meaning. The placeholder must be an integer multiple of 8 bytes.

Visit location

The Java program manipulates the specific content on the heap through the reference data on the stack. The reference type is a reference to an object. There are two mainstream access methods for objects

1. Handle

The Java heap divides a piece of memory to use as a handle pool. The reference stores the address of the object handle, and the handle contains the specific address information of the object instance data and type data.

2. Direct pointer

The address stored in the reference is the object address

The advantage of using handle access : When the object is moved, only the instance data of the handle is changed, and the reference itself does not need to be modified.
The advantages of using direct pointer access : faster speed, saving time and overhead, HotSpot virtual machine mainly uses this method.

Guess you like

Origin blog.csdn.net/weixin_43663421/article/details/109270081