Java object memory layout and object header

1 definition

insert image description here

2 Detailed introduction

insert image description here
insert image description here

2.1 Object header

insert image description here
insert image description here
[Object mark]
Mark Word
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
[Class meta information]
also known as type pointer
insert image description here

2.1 Instance data

2.1 Alignment padding

3 JOL view memory layout

public class ObjectLayout {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(VM.current().details());
        System.out.println(VM.current().objectAlignment());
    }
}

# Running 64-bit HotSpot VM.
# Using compressed oop with 0-bit shift.
# Using compressed klass with 3-bit shift.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]

8
public class ObjectLayout {
    
    
    public static void main(String[] args) {
    
    
        Object o = new Object();
        System.out.println(ClassLayout.parseInstance(o).toPrintable());
    }
}

java.lang.Object 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)                           e5 01 00 20 (11100101 00000001 00000000 00100000) (536871397)
     12     4        (loss due to the next object alignment)
Instance size: 16 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

insert image description here

Guess you like

Origin blog.csdn.net/kaikai_sk/article/details/131610836