当前位置:网站首页>JVM memory and garbage collection-3-object instantiation and memory layout

JVM memory and garbage collection-3-object instantiation and memory layout

2022-07-08 01:58:00 xcrj

Object instantiation

Mind mapping

Example

Code

package xcrj;

/* *  Object instantiation process  * */
public class VMObjCreation {
    
    public static void main(String[] args) {
    
        Object obj = new Object();
    }
}

command

#  compile 
javac -d D:\workspace\idea\JVMDemo\blog\target\classes\ -encoding UTF-8 VMObjCreation.java
#  Decompile 
javap -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ -v xcrj.VMObjCreation

result - Decompile
 Insert picture description here
Introduce

  • new #2#2 Symbol reference ; Judge whether the type is loaded , In the method area Class Object instances ;new Open up storage space in the heap ;new Member properties are initialized by default
  • dup:duplicate OS Top 1 individual byte, Re press into stack ;OS Store object references (int value ),OS The object reference in refers to the object entity in the heap
  • invokespecial #1: Call the object constructor <init>
  • astore_1: take OS The object reference at the top of the stack (int value ) Put it in LV Medium slot 1 The location of

Object memory layout

Object content

  • Runtime metadata
  • Type a pointer
  • The instance data ( Parent instance data + Own instance data )
  • Alignment filling

Mind mapping

Example

Code

package xcrj;

public class VMObjLayout {
    
    private int age = 20;
    private String name;
    private Xcrj xcrj;

    {
    
        name = "xcrj";
    }

    public VMObjLayout() {
    
        xcrj = new Xcrj();
    }
}

class Xcrj {
    }

command

#  compile 
javac -d D:\workspace\idea\JVMDemo\blog\target\classes\ -encoding UTF-8 VMObjLayout.java
#  Decompile 
javap -classpath D:\workspace\idea\JVMDemo\blog\target\classes\ -v xcrj.VMObjLayout

result - Decompile
 Insert picture description here
Memory layout
 Insert picture description here

  • jdk7=+ String constant table and static variables are put in the heap
原网站

版权声明
本文为[xcrj]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130541456730.html