The article role

  • Simplified architecture for the JVM
  • System understanding of the VIRTUAL machine memory area contains what parts, roughly what is done
  • Understand the interaction between stack, heap, and method areas

Simplified architecture for the JVM

Class loaders have been given a separate article on class loading, wiring, and initialization, and the following focuses on the runtime data area

Runtime data area

Include: program counter (PC register),Java virtual machine stack, local method stack, method area, runtime constant pool,Java heap, etc

Program counter (PC register)

An abstract simulation of a physical PC register

  1. Each thread has a PC register, which is thread-private and stores the address to the next instruction. Can be understood as a line number indicator of the current bytecode, or as a program flow indicator
  2. When the thread is created, the corresponding PC register is created
  3. When executing the local method, the VALUE of the PC register isundefined
  4. Is a small memory space, the only one not specified in the JVM specificationOutOfMemoryErrorMemory region of

Example:

The program counter (PC register) is stored roughly as the bytecode red number column in the figure below

Java virtual machine stack

  • A stack consists of a series of frames (hence the Java stack) and is thread-private
  • Frames are used to hold local variables of a method, operand stack (Java has no registers and all arguments pass using the operand stack), constant pool Pointers, dynamic links, method return values, etc
  • Each method call creates a frame (the frame is combined with the method call) and pushes the stack. When exiting the method, changing the top pointer destroys the contents of the stack frame
  • The local variable table stores various basic data types and reference types known at compile time. Each slot holds 32 bits of data, with longs and doubles in two slots
  • Stack advantage: access faster than the heap, second only to registers
  • Disadvantages of the stack: the size of the data in the stack, the lifetime is determined at compile time, lack of flexibility

Method local variable

It is used to store method parameters and local variables defined in the method body

Example:

You can see that there are seven variables, including three parameter variables and four local variables. Long and double occupy two slots each

The operand stack

It is mainly used to store the intermediate results of the calculation process and serve as temporary storage space for variables in the calculation process

Dynamic link

Each stack frame contains an internal reference to the method that the stack frame belongs to in the runtime constant pool

The purpose of dynamic linking is to convert these symbolic references into direct references to the calling method

The return address

Used to hold the value of the program counter (PC register) that called the method

The Java heap

  • Used to store objects and arrays created by the application system, all threads share the Java heap
  • The GC mainly manages the heap space. For generational GC, the heap is also generational (generational just caters to the garbage collection algorithm, not necessarily generational)
  • Advantages of heap: dynamic allocation of memory size during runtime, automatic garbage collection
  • Disadvantages of heap: Relatively slow efficiency

Methods area

  • The method area is shared by threads and is usually used to hold loaded class information, constants, static variables, just-in-time compiled code, etc
  • It is usually associated with meta-spaces (Java8 used to be permanent), but it depends on the JVM implementation and version
  • The JVM specification describes the method area as a logical part of the heap, but it has an alias called non-heap, presumably to distinguish it from the Java heap

Run-time constant pool

The Constant Pool Table in the Class file is used to store various literals and symbolic references generated at compile time in the runtime Constant Pool in the method area after the Class is loaded

  • Is a constant pool table for each Class or interface in a Class file. It is identified at runtime, usually including: Class version, field, method, interface, etc
  • Assign in the method area
  • Typically, after classes and interfaces are loaded into the JVM, the corresponding runtime constant pool is created

Local method stack

The stack used in the JVM to support native method execution is the native method stack

The interaction between stack, heap, and method areas

This is a direct reference to the figure in understanding the Java Virtual Machine