JVM: Structure of the JVM

Chapter 2 reading notes: types and runtime data areas, and a bit about threads. Opcodes will come soon 😄! Source: chapter 2 of JVM specification.

Data types

There are two kinds of types (and values stored in variables)

  • primitive types (for primitive values)
  • reference types (reference values)

Primitive types: have special opciodes (iadd, ladd or fadd or dadd for integer, long and float and double, respectively)

Explicit support for objets: dynamically allocated class instance or an array Reference to an object has its own type: reference (kind of a pointer); objects are always passed and operated on using this type.

JVM types

Primitive types and values

  • Primitive:
    • Integral:
      • byte, short, int, long - signed 8, 16, 32, 64 bits two’s-complement integers whose value is zero
      • char - 16-bit unsigned integers; Unicode code points encoded in UTF-16
    • Floating point:
      • float - 32-bit IEEE 754 binary32 format
      • double - 64-bit IEEE 754 binary64 format
    • Boolean type:
      • values: true and false (default)
    • returnAddress type
      • values: pointers to opcodes; used for arguments to jsr, ret, and jsr_w instructions
      • can’t be changed at runtime
      • not available as Java type

Reference types and values

  • class types -> class instances
  • array types -> arrrays
  • interface types -> arrays or class instances that implement interface Such values can be null (default value for reference type).

Run-Time Data Areas

  • can be per-program and per-thread

PC register

  • keeps the address of the VJM instruction currently being executed (if the method is not native; in such case value of pc is undefined)


  • each thread has its own stack
  • holds local variables, partial results (in frames, which can be heap-allocated; memory for the stack does not need to be contiguous)
  • can be fixed sized or expand/contract dynamically (size or min/max can be defined by the programmer)
  • JVM throws StackOverflowError if the computation requires larger stack than is permitted
  • JVM throws OutOfMemoryError if - in case of dynamic expansion - expansion is attempted but insufficient memory can be made available (or there is not enough memory to create a stack for new thread)


  • shared by all threads
  • stores objects reclaimed by garbage collector
  • can be fixed or dynamic by size (size or min/max can be defined)
  • if computation requires more heap that can be made available by GC, throws OutOfMemoryError
JVM heap and stacks

Method Area

  • stores per-class structures
    • run-time constant pool
    • field and method data
    • code for methods and constructors
    • logically part of the heap

Run-Time Constant Pool

  • per-class or per-interface representation of constant_pool table in class file.
  • allocated from method area
  • numeric literals known in compile time, field references resolved at run-time
  • similar to symbol table in classic languages

Native Method Stacks

  • needed if JVM uses conventional stacks or if it implements native methods that rely on conventional stakcs
  • allocated per thread, with size that may be configured (fixed of dynamically expanded/contracted), throwing SO or OOM


  • created when a method is invoked
  • destroyed when invocation completes (norm. or abr.)
  • allocated on per-thread stack; each has
    • array of local variables (size determined at compile time)
    • operand stack (as above)
    • reference to runtime constant pool of the class of the current method
  • only one (called current frame) is active - the one for the executing method called current method
  • on return, controls goes back to previous frame which becomes current, and the frame from which control returned is discarded

Local Variables

  • single local variable (lv) can hold a value of types: boolean, byte, char, short, int, float, reference, returnAddress.
  • types long and double require two local variables per value (only accessed by for read by lesser index; higher index can be stored into)
  • accessed by 0-indexing
  • on instance method invocation, 0-th variable is always reference to this

Operand Stacks

  • empty at start
  • max depth determined at compile time and supplied with current method code
  • depth determination: long and double count as 2, other values as 1
  • there are instructions for loading constants or variables; others for taking, operating and putting back on the operand stack
  • class file verification process ensures that instructions operate on proper data (i.e. with proper types)

Dynamic Linking

  • reference to constant pool in frame: needed to support dynamic linking of method code
  • class file contains symbolic references to methods and variales
  • dynammic linking translates those to appropriate offsets in storage structures which are valid on runtime


  • normal: no exception thrown (by JVM or throw); may return value
  • abrupt: JVM throws exception which is not handled in the method or athrow instructin in executed and also not handled; never returns value to invoker

Representation of Objects

This is not “mandated” in any way. There’s a note that in Oracle impl a reference to an instance is a pointer to handle, which is a pair of pointers: one to table with methods of the object and Class instance pointer, and the other to heap memory with object data.

Floating Pont Arithmetic

  • JVM uses a subset of operations defined in IEEE 754 Standard with diffefences specified in the spec
  • rounding policy: a function used to map a real mumner to a floating point value in a given format
    • round to nearest - all fp instructions except conversion to integer and remainder
      • if two nearest representable values are equally near, choose value with least significant bit being zero
    • round toward zero - conversion from fp to integer by d2i, d2l, f2i and f2l and fp-remainder (drem, frem)
      • inexact results rounded to nearest representable value which is not greater in magnitude than infinitely precise result (in case of conversion to integer: equivalent to truncation where fractional bits are discarded)

Special methods

Instance Initialization Methods

  • defined in a class (not interface)
  • has special name <init>
  • is void Constrains:
  • declaration: access_flags and code array
  • use: invoked only by invoke_special

Class Initialization Methods

  • has name <clinit>
  • is void
  • if class file version equal or greater than 51.0: method has set ACC_STATIC flag and takes no arguments

Signature Polimorfic Methods

  • declared in java.lang.invoke.MethodHandle or java.lang.invoke.VarHandle
  • has single formal param Object[]
  • has ACC_VARARGS and ACC_NATIVE flags set

JVM treats such method specially in invokevirtual in order to invoke method handle or access variable referenced by var handle.


  • represented by instance of class Throwable or one of subclasses
  • occur synchronously (result of an action by the thread) or asynchronously (at any point in program execution)
  • three reasons:
    1. athrow instrunction executed
    2. abnormal exection condition detected (synchronously) by JVM after an instrunction that
    • specifies exception as possible result (instrunction has operation that violates Java semantics, like indexing oob, or there is an error in loading/linking)
    • causes limit on a resource to be exceeded (memory)
    1. An async exception occured
    • stop method of Thread or ThreadGroup
    • internal error of JVM impl

Speculative execution results of code after throwing point should be hidden from user-visible state of the program. Exceptions are therefore precise: control transfer causes all actions before throw to happen before the exception.

Process of exception throwing

  • each method may have zero or more exception handlers
  • exception handler:
    • range of offsets into VM code implementing the method for which the handler is active (try….catch in java)
    • type of the exception which the handler is able to handle
    • location of the handling code
  • when e. is throw, searches for matching handler for e. in current method; then (if found) branches to the handling code; if not found, the method completes abruptly, the frame is dropped and matching handlers are searched in previous method up the stack (until handler is found or thread is terminated)
  • search order is guaranteed to be the order as specified in class file’s exception handler table

Ten wpis jest częścią serii jvm.

Wszystkie wpisy w tej serii: