Java concurrent programming-JAVA object header (including 32-bit virtual machine and 64-bit virtual machine)

Why learn Java object headers

Learning the Java object header is mainly to understand the underlying principles of synchronized, the synchronized lock upgrade process, and concurrent Java programming.
Insert picture description here

JAVA object header

Due to the object-oriented idea of ​​Java, a large number of storage objects are required in the JVM. In order to realize some additional functions during storage, some mark fields need to be added to the objects to enhance the function of the object. These mark fields constitute the object header.

In the HotSpot virtual machine, the layout of objects stored in memory can be divided into 3 areas: object header (Header), instance data (Instance Data) and alignment padding (Padding).
That is to say JAVA object = object header + instance data + object filling.

Among them, the object header is composed of two parts, one part is used to store its own runtime data, called Mark Word, and the other part is the type pointer, and the object points to its class metadata pointer.

Object header = Mark Word + type pointer
(when pointer compression is not turned on)
In a 32-bit system, Mark Word = 4 bytes = 32 bits, object header = 8 bytes = 64 bits;
In a 64-bit system, Mark Word = 8 bytes = 64 bits, object header = 16 bytes = 128bits;

bytes are bytes, and bits are bits. Therefore, in the 32-bit JVM virtual machine system, the Mark Word part occupies 4 bytes, and the Klass Word part also occupies 4 bytes, so the object header size is 8 bytes. In the 64-bit JVM virtual machine system, the Mark Word part occupies 8 bytes, and the Klass Word part also occupies 8 bytes, so the object header size is 16 bytes.

32-bit virtual machine object header

Object header of common object of 32-bit virtual machine

|-----------------------------------------------------------|
|                    Object Header (64 bits)                |
|---------------------------------|-------------------------|
|             Mark Word (32 bits) | Klass Word (32 bits)    |
|---------------------------------|-------------------------|

Object header of 32-bit virtual machine array object

|---------------------------------------------------------------------------------|
|                                  Object Header (96 bits)                        |
|--------------------------------|-----------------------|------------------------|
|       Mark Word(32bits)        | Klass Word(32bits)    | array length(32bits)   |
|--------------------------------|-----------------------|------------------------|

The 32-bit virtual machine object header details are as follows

|-----------------------------------------------------------------------------------------------------------------|
|                                             Object Header(64bits)                                               |
|-----------------------------------------------------------------------------------------------------------------|
|                       Mark Word(32bits)                           |  Klass Word(32bits)    |      State         |
|-----------------------------------------------------------------------------------------------------------------|
|     hashcode:25                      | age:4 | biased_lock:0 | 01 | OOP to metadata object |      Nomal         |
|-----------------------------------------------------------------------------------------------------------------|
|     thread:23              | epoch:2 | age:4 | biased_lock:1 | 01 | OOP to metadata object |      Biased        |
|-----------------------------------------------------------------------------------------------------------------|
|     ptr_to_lock_record:30                                    | 00 | OOP to metadata object | Lightweight Locked |
|-----------------------------------------------------------------------------------------------------------------|
|     ptr_to_heavyweight_monitor:30                            | 10 | OOP to metadata object | Heavyweight Locked |
|-----------------------------------------------------------------------------------------------------------------|
|                                                              | 11 | OOP to metadata object |    Marked for GC   |
|-----------------------------------------------------------------------------------------------------------------|

64-bit virtual machine object header

|-----------------------------------------------------------------------------------------------------------------|
|                                             Object Header(128bits)                                              |
|-----------------------------------------------------------------------------------------------------------------|
|                                   Mark Word(64bits)               |  Klass Word(64bits)    |      State         |
|-----------------------------------------------------------------------------------------------------------------|
|    unused:25|identity_hashcode:31|unused:1|age:4|biase_lock:0| 01 | OOP to metadata object |      Nomal         |
|-----------------------------------------------------------------------------------------------------------------|
|    thread:54|      epoch:2       |unused:1|age:4|biase_lock:1| 01 | OOP to metadata object |      Biased        |
|-----------------------------------------------------------------------------------------------------------------|
|                        ptr_to_lock_record:62                 | 00 | OOP to metadata object | Lightweight Locked |
|-----------------------------------------------------------------------------------------------------------------|
|                       ptr_to_heavyweight_monitor:62          | 10 | OOP to metadata object | Heavyweight Locked |
|-----------------------------------------------------------------------------------------------------------------|
|                                                              | 11 | OOP to metadata object |    Marked for GC   |
|-----------------------------------------------------------------------------------------------------------------|

lock : 2-bit lock status flag bit. Since we want to use as few binary bits as possible to represent as much information as possible, the lock flag is set. The value of the mark is different, the meaning of the whole mark word is different.

biased_lock lock status
0 01 no lock
1 01 Bias lock
0 00 Lightweight lock
0 10 Heavyweight lock
0 11 GC mark

biased_lock : Whether the object enables the biased lock flag, which only occupies 1 binary bit. When it is 1, it means that the object has a biased lock, and when it is 0, it means that the object has no biased lock.
age : 4-digit Java object age. In the GC, if the object is copied once in the Survivor area, the age increases by 1. When the subject reaches the set threshold, it will be promoted to the old age. By default, the age threshold for parallel GC is 15, and the age threshold for concurrent GC is 6. Since age has only 4 bits, the maximum value is 15, which is why the -XX:MaxTenuringThresholdmaximum value of the option is 15.
identity_hashcode : 25-bit object identification hash code, using lazy loading technology. Call the method System.identityHashCode()calculation and write the result to the object header. When the object is locked, the value will be moved to the monitor.
thread : ID of the thread holding the bias lock.
epoch : bias timestamp.
ptr_to_lock_record : Pointer to the lock record in the stack.
ptr_to_heavyweight_monitor : Pointer to monitor monitor.

How to view object header

To view the object header, you need to use the JOL tool.

<dependency>
     <groupId>org.openjdk.jol</groupId>
     <artifactId>jol-core</artifactId>
     <version>0.10</version>
</dependency>

import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.vm.VM;
import static java.lang.System.out;

public class TestBiased {
    
    
    public static void main(String[] args) {
    
    
        Dog d= new Dog();
        //打印JVM的详细信息
        out.println(VM.current().details());
        //打印对应的对象头信息
        out.println(ClassLayout.parseInstance(a).toPrintable());
    }
}

class Dog {
    
    
}

The print is roughly as follows.

21:46:08.204 c.TestBiased [main] - # Running 64-bit HotSpot VM.
# Objects are 8 bytes aligned.
# Field sizes by type: 8, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 8, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

21:46:08.215 TestBiased [main] - cn.itcast.test.Dog object internals:
 OFFSET  SIZE   TYPE DESCRIPTION                               VALUE
      0     4        (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4        (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4        (object header)                           50 c8 e1 3f (01010000 11001000 11100001 00111111) (1071761488)
     12     4        (object header)                           71 01 00 00 (01110001 00000001 00000000 00000000) (369)
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

If the printing is different, you can add the VM options parameter -XX:-UseCompressedOopsto turn off pointer compression.

To understand the number of VALUE, you have to understand little endian storage

reference

memory-efficient-java
in - depth understanding of Java object header mark word
Java object header detailed explanation of
java school recruitment interview
Java object structure and lock implementation principle and MarkWord detailed explanation to
kill the interviewer 1-synchronized underlying principle (from Java object header to instant compilation optimization)
Explore the Java object header-big-endian storage and little-endian storage

Guess you like

Origin blog.csdn.net/e891377/article/details/108905401
Recommended