Java
JVM

[Java] Runtime Data Areas of JVM

Runtime Data Areas

62708f0fa456f6c70661446194ae4fd2.png

  • PC (Program Counter) register
    PC register has the address of a JVM instruction being executed now.

  • JVM stack
    It is a stack that saves the struct (Stack Frame).

    Stack frame

    12e67f793e0b88cc6fe46d8085e8c326.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.
- Local variable array: It has an index starting from 0. 0 is the reference of a class instance where the method belongs. From 1, the parameters sent to the method are saved.
- Operand stack: Each method exchanges data between the Operand stack and the local variable array, and pushes or pops other method invoke results. The necessary size of the Operand stack space can be determined during compiling. Therefore, the size of the Operand stack can also be determined during compiling.

  • Native method stack
    A stack for native code written in a language other than Java. In other words, it is a stack used to execute C/C++ codes invoked through JNI (Java Native Interface).

  • Method area
    The method area is shared by all threads, created when the JVM starts. It stores runtime constant pool, field and method information, static variable, and method bytecode for each of the classes and interfaces read by the JVM.

  • Runtime constant pool
    An area that corresponds to the constant_pool table in the class file format. This area is included in the method area. As well as the constant of each class and interface, it contains all references for methods and fields. In short, when a method or field is referred to, the JVM searches the actual address of the method or field on the memory by using the runtime constant pool.

  • Heap
    A space that stores instances or objects, and is a target of garbage collection.

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 …
}
  • Byte number
    the number in front of the code (0, 1, 4...)

  • OpCode
    the bytecode instruction (aload_0, getfield...)
    getfield need the 2-byte Operand, the next instruction of getfield on the first byte is written on the fourth byte by skipping two bytes.

  • Index of class constant pool
    (#15, #23)

  • Locals
    the length of local variable array
    Slot 0: this (the reference for the current class instance)
    Slot 1: userName (method parameter)

  • Stack
    the size of the Operand stack

  • Args_size
    the size of the argument (this, userName)

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.