Detailed explanation of memory layout in java objects--Java virtual machine

Memory layout in java object

In the virtual machine, the storage of an object in the heap memory can be divided into three parts: object header (Header), instance object (Instance Data) and alignment padding (Padding). Let's understand one of these three parts respectively. What are they and what they do.

1. Object head

The object header is divided into two parts. Mark Word and Klass Word (don't doubt, Klass, not class). They have different proportions in different JVMs. The default JVM below is 32-bit HotSpot virtual machine .

Overview 32-bit virtual machine 64-bit virtual machine
Mark Word Store object runtime data 32bit 64bit
Class Word Type pointer, pointing to the type metadata of the object 32bit 64bit
Array Lenght (exclusive to array objects) Data length 32bit 64bit

1 Mark Word

MarkWord is mainly used to store the runtime data of the object itself: hash code (HashCode), GC generation age (age), lock information, etc. There are a lot of runtime data that the object needs to store, and if it is stored, it will exceed 32 Taking into account the space efficiency of the virtual machine, MarkWord is designed as a dynamically defined data structure . That is, MarkWord of an object will store different data according to different states. The following are several situations.

|-------------------------------------------------------------|--------------------|
|                  Mark Word (32 bits)                        |       State        |
|-------------------------------------------------------------|--------------------|
| identity_hashcode:25 | age:4 | biased_lock:1(0)| lock:2(01) |       Normal       |
|-------------------------------------------------------------|--------------------|
|  thread:23 | epoch:2 | age:4 | biased_lock:1(1) | lock:2(01)|       Biased       |
|-------------------------------------------------------------|--------------------|
|               ptr_to_lock_record:30          | lock:2(00)   | Lightweight Locked |
|-------------------------------------------------------------|--------------------|
|               ptr_to_heavyweight_monitor:30  | lock:2(10)   | Heavyweight Locked |
|-------------------------------------------------------------|--------------------|
|                                              | lock:2(11)   |    Marked for GC   |
|-------------------------------------------------------------|--------------------|
        后面的()中的内容代表在该特定状况下的二级制值,比如lock在Normal状况下占两位,值为(01)

The data is explained below.

  • identity_hashcode: The HashCode of the object. In fact, if an object does not call the hashCode() method, then the value of the object's identity_hashcode is 0.
  • age: GC generation age of the java object.
  • biased_lock: Whether the object opens the biased lock. 1 represents the open biased lock.
  • lcok: The status flag of the lock. The table shows that when biased_lock is 0 and lock is 01, the object is unlocked. When lock is 00, it represents a heavyweight lock. 10 is a lightweight lock. 11-bit GC flag
  • ptr_to_lock_record: The lock_record of the thread that points to the lock of the object
  • ptr_to_heavyweight_monitor: Monitor pointer to the object.

1.2Klass Pointer

Type pointer, an object can know which class instance the object is through the pointer.

1.3Array Length

Record the length of the array. But if the length of the object of an array is uncertain, then the size of the array cannot be inferred from the information in the metadata

2. Instance Data

The instance data is the truly valid information stored in the object, which is the various attribute values ​​set by the programmer.

3. Padding

This part of the content does not have to exist, nor has any special meaning, but because the HotSpot virtual machine stipulates that the starting address of the object must be 8 bytes (32 bits). The object header of the object must be an integer multiple of 8 bytes. So if If the instance part is not an integer multiple, a placeholder must be added to meet the requirements.

Guess you like

Origin blog.csdn.net/qq_44823898/article/details/109785206