[Java] Runtime Data Areas of JVM

Runtime Data Areas 62708f0fa456f6c70661446194ae4fd2.png

Each stack frame has the reference for local variable array, Operand stack, and runtime constant pool of a class where the method being executed belongs. The size of local variable array and Operand stack is determined while compiling. Therefore, the size of stack frame is fixed according to the method.

Java Bytecode

public void add(java.lang.String);
  Code:
   Stack=2, Locals=2, Args_size=2
   0:   aload_0
   1:   getfield        #15; //Field admin:Lcom/nhn/user/UserAdmin;
   4:   aload_1
   5:   invokevirtual   #23; //Method com/nhn/user/UserAdmin.addUser:(Ljava/lang/String;)Lcom/nhn/user/User;
   8:   pop
   9:   return  LineNumberTable:
   line 14: 0
   line 15: 9  LocalVariableTable:
   Start  Length  Slot  Name   Signature
   0      10      0    this       Lcom/nhn/service/UserService;
   0      10      1    userName       Ljava/lang/String; // … Omitted - Other method information …
}

example1 java code

public class SimpleClass {

    public int simpleField = 100;

}

byte code

public SimpleClass();
  Signature: ()V
  flags: ACC_PUBLIC
  Code:
    Stack=2, Locals=1, Args_size=1
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: aload_0
       5: bipush        100
       7: putfield      #2                  // Field simpleField:I
      10: return

constant pool

   #1 = Methodref          #4.#16         //  java/lang/Object."<init>":()V
   #2 = Fieldref           #3.#17         //  SimpleClass.simpleField:I
   #3 = Class              #13            //  SimpleClass
   #4 = Class              #19            //  java/lang/Object
   #5 = Utf8               simpleField
   #6 = Utf8               I
   #7 = Utf8               <init>
   #8 = Utf8               ()V
   #9 = Utf8               Code
  #10 = Utf8               LineNumberTable
  #11 = Utf8               LocalVariableTable
  #12 = Utf8               this
  #13 = Utf8               SimpleClass
  #14 = Utf8               SourceFile
  #15 = Utf8               SimpleClass.java
  #16 = NameAndType        #7:#8          //  "<init>":()V
  #17 = NameAndType        #5:#6          //  simpleField:I
  #18 = Utf8               LSimpleClass;
  #19 = Utf8               java/lang/Object

Locals=1, Args_size=1 -> this

  1. aload_0 the first local variable points to this -> load the this reference onto the operand stack

  2. invokespecial #1 invoking the superclass constructor -> corresponding to the index 1 of constant pool -> Methodref #4.#16 -> Object class init

default constructor execute initialization code for class variables (field)

  1. aload_0 the first local variable points to this -> load the this reference onto the operand stack

  2. bipush 100 add a byte as an integer (100) to the operand stack (Stack=2)

  3. putfield #2 reference a field in the index 2 of runtime constant pool -> Fieldref #3.#17 -> the field called simpleField -> pop 100 and this on the operand stack -> 100 to set the simpleField to this object that contains the field

java_class_variable_creation_byte_code.png

example2 java code

public class TestClass {
    public static void main(String[] args)  {
        Object foo = null;
        Object bar = null;
    }
}

byte code

public static void main(java.lang.String[]);
  Code:
   Stack=1, Locals=3, Args_size=1
   0:   aconst_null
   1:   astore_1
   2:   aconst_null
   3:   astore_2
   4:   return

LineNumberTable: 
line 5: 0
line 6: 2
line 7: 4

LocalVariableTable: 
Start  Length  Slot  Name   Signature
0      5      0    args       [Ljava/lang/String;
2      3      1    foo       Ljava/lang/Object;
4      1      2    bar       Ljava/lang/Object;

Args_size=1 -> args Locals=3 -> args, foo, bar

  1. aconst_null push the null onto the operand stack (Stack=1)

  2. astore_1 pop the reference from the operand stack -> store it in the index 1 of local variable correspond to foo

  3. aconst_null push the null onto the operand stack (Stack=1)

  4. astore_2 pop the reference from the operand stack -> store it in the index 2 of local variable correspond to bar

ref.

Recommended Posts

[Java] Runtime Data Areas of JVM
[Java] Overview of Java
[Java] Classification memo of compilation error and run-time error
Expired collection of java
[Java] Significance of serialVersionUID
NIO.2 review of java
Review of java Shilber
[Java] Data type ①-Basic type
java --Unification of comments
History of Java annotation
java (merits of polymorphism)
[Java] Main data types
NIO review of java
[Java] Three features of Java
Summary of Java support 2018
Java basic data types
Java to fly data from Android to ROS of Jetson Nano
[Java] To know the type information of type parameters at runtime
About an instance of java
[Java] Mirage-Basic usage of SQL
Java learning memo (data type)
[Java] Beginner's understanding of Servlet-②
[Java] Practice of exception handling [Exception]
[Java11] Stream Summary -Advantages of Stream-
Basics of character operation (java)
Java programming (variables and data)
[Java] Creation of original annotation
4th day of java learning
Java end of month plusMonths
Importing Excel data in Java 2
[Java] Summary of regular expressions
[Java] Summary of operators (operator)
[Java] Implementation of Faistel Network
[Java] Comparator of Collection class
Java addition excel data validation
Summary of Java language basics
[Java] Summary of for statements
Summary of Java Math class
Java for beginners, data hiding
Enumeration of all combinations Java
java (inheritance of is-a principle)
Implementation of gzip in java
Advantages and disadvantages of Java
Importing Excel data in Java 3
Benefits of Java static method
[Java] Summary of control syntax
Implementation of tri-tree in Java
List data structure [Java / Scala]
Summary of java error processing
Java Learning 1 (learning various data types)
[Java] Summary of design patterns
[Java] Summary of mathematical operations
[Java] Generate Data URI from byte string of file contents [Kotlin]