JVM内存结构之一 程序计数器、虚拟机栈


jvms14 :Chapter 2.

  1. Data Types
  2. Run-Time Data Areas
    • The PC Register
    • Java Virtual Mechine Stacks
    • Native Method Stacks
    • Heap
      • Method Area
        • Run-Time Constant Pool

This chapter gives an overview of the Java Virtual Machine architecture.

一、Data Types

Like the Java programming language, the Java Virtual Machine operates on two kinds of types: primitive types and reference types.

二、Run-Time Data Areas

The Java Virtual Machine defines various run-time data areas that are used during execution of a program. Some of these data areas are created on Java Virtual Machine start-up and are destroyed only when the Java Virtual Machine exits. Other data areas are per thread. Per-thread data areas are created when a thread is created and destroyed when the thread exits.

2.1 The PC Register

The Java Virtual Machine can support many threads of execution at once (JLS §17). Each Java Virtual Machine thread has its own pc (program counter) register. At any point, each Java Virtual Machine thread is executing the code of a single method.

[1] 程序计数器可以看做是当前线程所执行的 字节码的 行号指示器。字节码解释器工作时就是通过改变这个计数器的值来选取下一个需要执行的字节码指令。

2.2 Java Virtual Machine Stacks

Each Java Virtual Machine thread has a private Java Virtual Machine stack, created at the same time as the thread.

The memory for a Java Virtual Machine stack does not need to be contiguous.


[1] 虚拟机栈描述的是JAVA方法执行的内存模型:每个方法被执行的时候,JAVA虚拟机都会同步创建一个 栈帧(#2.4)用于存储局部变量表、操作数栈、动态链接、方法出口等信息。

每个方法被调用到执行完成,都对应着一个栈帧在 JAVA虚拟机栈中 入栈、出栈的过程。


2.3 Native Method Stacks

An implementation of the Java Virtual Machine may use conventional stacks, colloquially called "C stacks," to support native methods (methods written in a language other than the Java programming language).

If the computation in a thread requires a larger native method stack than is permitted, the Java Virtual Machine throws a StackOverflowError.

If native method stacks can be dynamically expanded and native method stack expansion is attempted but insufficient memory can be made available, or if insufficient memory can be made available to create the initial native method stack for a new thread, the Java Virtual Machine throws an OutOfMemoryError.

2.4 Frames(栈帧)

A frame is used to store data and partial results, as well as to perform dynamic linking, return values for methods, and dispatch exceptions.

A new frame is created each time a method is invoked.A frame is destroyed when its method invocation completes, whether that completion is normal or abrupt (it throws an uncaught exception). Frames are allocated from the Java Virtual Machine stack (§2.5.2) of the thread creating the frame. Each frame has its own array of local variables (§2.6.1), its own operand stack (§2.6.2), and a reference to the run-time constant pool (§2.5.5) of the class of the current method.
Only one frame, the frame for the executing method, is active at any point in a given thread of control. This frame is referred to as the current frame, and its method is known as the current method.
Note that a frame created by a thread is local to that thread and cannot be referenced by any other thread.

下一章先看类加载机制,Loading, Linking, and Initializing,然后再看垃圾回收。