Monday 4 August 2014

Java Virtual Machine Internals

This article explains the internal architecture of the Java Virtual Machine (JVM). 














Method area is part of non-heap memory. It stores per-class structures, code for methods and constructors. Per-class structure means runtime constants and static fields.

Type Information:


  • Fully qualified type’s  name.
  •  Fully qualified direct super  class name.
  •  Whether class or an  interface
  •  type's modifiers 
  •  list of the fully qualified  names of any direct super  interfaces

   

Constant Pool:   


  • Ordered set of constants

        - string
        - integer
        - floating point 
        - final variables

  •  symbolic references to

        - types
        - fields 
        - Methods


Field Information:


  •   field’s name
  •   field’s type
  •   field’s modifiers (subset )

        - public
        - private
        - protected
        - static
        - final
        - volatile
        - transient



Method Information:


  • Method’s name
  • Method’s return type
  • Number and type of  parameters
  • Modifiers (subset)

        - public
        - private
        - protected
        - static
        - final
        - synchronized
        - native
        - abstract


Class Variables:


  • ordered set of class  variables 

    - static variables



Reference to class loader:

Reference to class loader is used for dynamic linking.
  instance java.lang.Class is created every type for
the following info.
     - getName();
     - getSuperClass();
     - isInterface();                                      
     - getInterfaces();
     - getClassLoader();


Method Table:

Used for quick ref. to
    method.

  Contains name and index
   in symbol ref. array


Heap Memory:

Objects and arrays are allocated in this area.
Two different threads of the same application, however, could trample on each other's heap data.


Memory Generations:


HotSpot VM’s garbage collector uses generational garbage collection. It separates the JVM’s memory into and they are called young generation and old generation.

Young Generation

Young generation memory consists of two parts, Eden space and survivor space. Shortlived objects will be available in Eden space. Every object starts its life from Eden space. When GC happens, if an object is still alive and it will be moved to survivor space and other dereferenced objects will be removed.

Old Generation – Tenured and PermGen

Old generation memory has two parts, tenured generation and permanent generation (PermGen). PermGen is a popular term. We used to error like PermGen space not sufficient.

GC moves live objects from survivor space to tenured generation. The permanent generation contains meta data of the virtual machine, class and method objects.


Lock on Objects:

object is associated with a lock (or mutex) to coordinate multi-threaded access to the object.
 Only one thread at a time can "own" an object's lock.
 Once a thread owns a lock, it can request the same lock again multiple times, but then has to release the lock the same number of times before it is made available to other threads


Array Allocation on Heap:

The name of an array's class has one open square bracket for each dimension plus a letter or string representing the array's type. 
The class name for an array of ints is "[I“.
The class name for  three-dimensional array of bytes is "[[[B". 
 The class name for a two-dimensional array of Objects is "[[Ljava.lang.Object". 


Java Stack:

Java stack stores a thread's state in discrete frames. 
 Each frame contains
        - local variables Area.
        - operand stack 
        - frame data



Local variable  Area:

organized as a zero-based array of cells.
 Variables are accessed through their indices.
 Values of type int, float, reference, and return Address occupy one cell. 
 Values of type byte, short, and char also occupy one cell.
 Values of type long and double occupy two consecutive cells in the array. 


Operand Stack:

operand stack is also organized as an array of cells.
local variables are accessed via array indices, the operand stack is accessed by pushing and popping values. 
instructions take their operands from 
        - operand stack
        - immediately following the opcode   
        - constant pool


Frame data:

Frame data is needed to support 
          - constant pool resolution
          - normal method return
          - exception dispatch
          - debugging.


Responsibilities of Memory Mgr:

Chop the chunk. 
Allocate Requested number of bytes.
 Provide necessary information to garbage
    collector.
 Collect the bytes returned by garbage
    collector.
 Defragment the chunks.

Optimal size of chunk:

For Method Area MM              10K
For Stack                             16K
For Heap                  1K, 10K, 32K






No comments:

Post a Comment