Skip to main content
Deep Dive into JVM (Java Virtual Machine) (2)

Deep Dive into JVM (Java Virtual Machine) (2)

gunyoung.Park
Author
gunyoung.Park
Always curious, always exploring new tech
Deep Dive into JVM - This article is part of a series.
Part 2: This Article

JVM Components
#

JVM_process


1. Class Loader
#

JVM’s Class Loader loads *.class files (bytecode files converted by javac) into Runtime Data Areas to run the program.

Class Loader loading occurs at runtime, when a class is first accessed. This enables Lazy Loading Singleton implementation.

  • Class Loading is Thread-safe.

2. Execution Engine
#

Executes the bytecode loaded into Runtime Data Areas by the Class Loader. It converts bytecode to machine code and executes it instruction by instruction, composed of 1-byte OpCode and operands.

Key Components
#

  • Interpreter
  • Compiler (Just-in-Time)

3. Garbage Collector
#

Responsible for removing unreferenced objects in the Heap area.

Before Java, programmers managed all program memory. In Java, the JVM manages program memory through a process called garbage collection.

Garbage collection continuously finds and removes unused memory in Java programs.

4. Runtime Data Areas
#

JVM’s memory area allocated from the OS. It holds data needed to run Java applications.

Runtime Data Areas are divided into 5 areas as follows.

Shared Areas

  • Method and Heap areas are shared by all Threads

Thread-specific Areas

  • Stack, PC Register, Native Method areas exist per Thread

JVM_process

(1) Method Area
#

Created when the JVM starts, it stores runtime constant pools, field and method code, static variables, method bytecode, etc. for each class and interface read by the JVM.

It’s a Non-Heap area stored in the Permanent area. This is a factor to consider when specifying the PermSize (Permanent Generation size) JVM option.

1-1 Type Information
#

  • Whether it’s an Interface
  • Type name including package name
  • Type access modifiers
  • Associated Interface list

1-2 Runtime Constant Pool
#

  • Stores all references to Type, Field, and Method
  • JVM finds and references memory addresses through the Runtime Constant Pool

1-3 Field Information
#

  • Field type
  • Field access modifiers

1-4 Method Information
#

  • Stores metadata for all Methods including Constructors
  • Stores Method name, parameter count and type, return type, access modifiers, bytecode, local variable section size, etc.

1-5 Class Variable
#

  • Stores variables declared with the static keyword
  • Actual instances of non-primitive static variables are stored in Heap memory

(2) Heap Area
#

Space for storing objects created with the new operator.

Objects become targets for GC (Garbage Collector) when no referencing variables or fields exist.

(3) Stack Area
#

Stored as separate Frames per Thread, with the following elements stored.

3-1 Local Variable Area
#

  • Stores temporary data occurring during Method execution, such as local variables, parameters, and method call addresses
  • Stored in 4-byte units: 4-byte primitives like int, float occupy 1 cell; 8-byte primitives like double occupy 2 cells
  • bool generally occupies 1 cell

3-2 Operand Stack
#

  • The Method’s workspace
  • Indicates which commands to execute with which operands

3-3 Frame Data
#

  • Includes Constant Pool Resolution, Method Return, Exception Dispatch, etc.
  • Also holds referenced Exception tables
  • When an Exception occurs, JVM references this table to determine how to handle the Exception

(4) PC Register
#

Created when a Thread starts, one exists per thread.

Holds the address of the currently executing portion of the Thread, recording which part should be executed with which instruction.

The OS references the PC (Program Counter) Register to know which instruction the Thread should execute next during CPU scheduling.

(5) Native Method Stack
#

Area for executing programs written in actual executable machine code rather than bytecode generated from Java program compilation.

  • Space for code written in languages other than Java
  • Converted to bytecode and stored through the Java Native Interface
  • Area where the kernel grabs the stack and independently executes the program, like a general program

JVM Execution Order
#

  1. Memory Allocation

    • When a program runs, JVM receives memory needed to execute the program from the OS
    • JVM divides this memory into multiple areas for use
  2. Compilation

    • Java Compiler (javac) compiles *.java files and converts them to *.class Java bytecode
  3. Class Loading

    • Compiled *.class files are loaded into JVM memory through the Class Loader
  4. Bytecode Interpretation

    • Loaded *.class files are interpreted into machine code through the Execution Engine
  5. Execution and Management

    • Interpreted bytecode is placed in memory areas and actually executed
    • During execution, JVM performs memory management tasks such as thread synchronization and garbage collector as needed
Deep Dive into JVM - This article is part of a series.
Part 2: This Article

Related