19 August 2013

JVM internals: Runtime data areas

When a Java virtual machine runs a program, it needs memory to store many things, including bytecodes and other information it extracts from loaded class files, objects the program instantiates, parameters to methods, return values, local variables, and intermediate results of computations. The Java virtual machine organizes the memory it needs to execute a program into several runtime data areas.

Overview of all data areas:


Let's talk about each one in detail. 

Data areas individual for each thread:
  1. The pc Register - program counter register. Each JVM thread has its own pc register. At any time JVM thread executing method and if that method is not native, the pc register contains the address (in Method Area) of next instruction to execute, but is method is native then pc register's value is undefined. 
  2. Stack. JVM stack stores the state of each Java (not native) method invocations for the thread. State of method invocation placed in stack frame (or simply frame), so then thread invokes method, new frame created and pushed to stack, and then method completes frame poped and discarded from virtual machine. State of frame contains local variables, parameters of method, return result, intermediate calculations also know as operand stack (as JVM is stack-based VM, it has no registers to store intermediate calculations) and reference to current class constant pool. The sizes of local variable array and the operand stack are determined at compile-time. Only one frame is active at any point of thread execution and this frame is called current frame and it's method known as current method. It should be mention, the memory for JVM stack doesn't have to be contiguous.
  3. Native Method Stack. Native method stack also called as "C stack". Native method stack is used for native method invocation and also may be used by the implementation of a JVM bytecode interpreter. 

Interaction between Java stack and Native Method Stack:


Data areas shared by all threads:
  1. Heap. 
  2. Method Area. Method area is used to store per-class structures such as runtime constant pool, field and method data, code for methods and constructors (including special methods used in class or object initialization). Method area is created on virtual machine start up and it is logical part of heap. JVM provide control over minimum and maximum size of method area or just initial size.
  3. Runtime Constant Pool. Runtime constant pool is per-class or per-interface runtime representation of the constant_pool table in a class file. It contains several kinds of constants ranging from literal constants known at compile time to method and field references resolved at runtime. Runtime constant pool is constructed when class or interface created in virtual machine.
    In class file constant pool is represented as array of structures like:
                

      constant_info {
                u1 tag;
                u1 info[];
    }

    Tag is 1 byte value which is vary from 1 to 18 and represents one of constant type.

    ·         CONSTANT_Utf8  (1)
    u1 tag – constant type (1)
    u2 length – number of bytes in byte array
    u1 bytes[length] – contains bytes of string (byte value > 0 i. e. in range (0; 0x0F)) (strings                                      is encode in modified UTF-8)

    ·         CONSTANT_Class  (7)
    tag - constant type (7)
    name_index -  index of CONSTANT_Utf8 entry in constant_pool

    ·         CONSTANT_FieldRef (9)

    ·         CONSTANT_MethodRef (10)

    ·         CONSTANT_InterfaceMethodRef (11)
    u1 tag – constant type
    u2 class _index – index of CONSTANT_Class entry in constant pool
    u2 name_index – index of CONSTANT_NameAndType entry in constant pool

    ·         CONSTANT_String (8)
    u1 tag – constant type (8)
    u2 string_index – index of CONSTANT_Utf8 entry in constant pool to which the String object is to be initialized

    ·         CONSTANT_Integer (3)

    ·         CONSTANT_Float (4)
    u1 tag – constant type
    u4 bytes – value of constant in bytes in big-endian order (high order first)

    ·         CONSTANT_Long (5)

    ·         CONSTANT_Double (6)
    u1 – constant type
    u4 hight_bytes
    u4 low_bytes
    VALUE = ((long) high_bytes << 32) + low_bytes

    ·         CONSTANT_NameAndType (12)
    u1 tag – constant type
    u2 name_index – method or field name
    u2 descriptor_index – method or field descriptor
    Field descriptor –
    §  B – byte
    §  C – char
    §  D – double
    §  F – float
    §  I – integer
    §  J – long
    §  L ClassName ; - reference
    §  S – short
    §  Z – Boolean
    §  [ - array dimension (one bracket per dimension)
    Method descriptor – (FieldDescriptor*)ReturnDecriptor  (return descriptor may be V – void or FieldDescriptor)

    ·         CONSTANT_MethodHandle (15)
    u1 tag – constant type
    u1 reference_kind – value denotes the kind of method handle, which characterizes its bytecode behavior and lies in range 1 to 9
    1.       REF_getField + CONSTANT_Field
    2.       REF_getStatic + CONSTANT_Field
    3.       REF_putField + CONSTANT_Field
    4.       REF_putStatic + CONSTANT_Field
    5.       REF_invokeVirtual + CONSTANT_MethodRef (all methods excluding and )
    6.       REF_invokeStatic + CONSTANT_MethodRef (all methods excluding and )
    7.       REF_invokeSpecial + CONSTANT_MethodRef (all methods excluding and )
    8.       REF_newInvokeSpecial + CONSTANT_MethodRef (only )
    9.       REF_invokeInterface + CONSTANT_InterfaceMethodRef (all methods excluding and )
    u2 reference_index – value of CONSTANT_Field, _MethodRef, _InterfaceMethodRef

    ·         CONSTANT_MethodType (16)
    u1 tag – constant type
    u2 descriptor_index – valid index of CONSTANT_Utf8 entry in constant_pool which representing a method descriptor

    ·         CONSTANT_InvokeDynamic (18)
    u1 tag – constant type
    u2 bootstrap_method_attr_index – valid index of bootstrap_methods array of the bootstrap method table of current class file
    u2 name_and_type_index – valid index of CONSTANT_NameAndType entry in constant_pool representing method name and method descriptor


No comments:

Post a Comment