JVM oop memory model

1. The oop model

1. Non-array object InstaceOopDesc

2. Array object arrayOopDesc

        2.1 Basic data type array typeArrayOopDesc

        2.2 Reference type array objArrayOopDesc

 3、MarkOopDesc

        Store lock information, generational age, etc.

Second, the memory structure of the object

 The object memory structure is divided into three parts

Object Header (64-bit OS)

mark world occupies 8B

kclass pointer type pointer 

        Enable pointer compression (default) 4B

        Turn off pointer compression 8B

Array length 4B

        The object is not an array, occupying 0B

        The object is an array, occupying 4B

instance data instance Data

4 categories and 8 types

boolean 1B

char 2B 0-65535

byte 1B -128~127 (-2 to the 7th power to 2 to the 7th power-1)

short 2B -32768~32767 (-2 to the 15th power to 2 to the 15th power-1)

int 4B -2147483648~2147483647 (-2 to the 31st power of 2 to the 31st power of 2-1)

long 8B -9223372036854774808~9223372036854774807 (-263 to 263-1)

float 4B 3.402823e+38~1.401298e-45

double 8B 1.797693e+308~4.9000000e-324
reference type
        enable pointer compression 4B

        Turn off pointer compression 8B

Align Filled Area

 The number of filling digits is guaranteed to be an integer multiple of 8

3. How to calculate the object size

public class A {
    // 普通对象
    int a = 1;
    int b = 2;
    public static void main(String[] args) {
        A a = new A();
    }
}

object without instance data

Enable pointer compression

16B = 8B + 4B + 0B + 0B + 4B
turn off pointer compression
16B =8B +8B +0+0+0

Ordinary object (array object, there will be padding at both ends when pointer compression is turned off)

Enable pointer compression

24 =8B + 4B + 0B + 4*2 +4B
turn off pointer compression
24 =8+8+0+4*2+ 0

Fourth, the implementation principle of pointer compression

Why should you turn on pointer compression to save memory and
improve addressing efficiency?

The realization principle of pointer compression
Two sentences
and two pieces of information:
1. All objects in java are 8-byte aligned

2. The last three digits are 0. When storing, the last three digits of 0 are erased. When using, the last three digits are filled with 0.

Pointer Compression C++ Code Structure

Guess you like

Origin blog.csdn.net/qq_16803227/article/details/131481477