Chapter 2. The Structure of the Java Virtual Machine (oracle.com) 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.
JVM 只有一个方法区,且是被所有 JVM 线程共享的,方法区的生命周期是与 JVM 互相绑定的。方法区拥有以下特点:
A run-time constant pool is a per-class or per-interface run-time representation of the
constant_pool
table in aclass
file (§4.4). It contains several kinds of constants, ranging from numeric literals known at compile-time to method and field references that must be resolved at run-time. The run-time constant pool serves a function similar to that of a symbol table for a conventional programming language, although it contains a wider range of data than a typical symbol table.
JVM 只有一个堆,且是被所有 JVM 线程共享的,类和数组等主要在堆中进行内存分配,堆的生命周期同样是与 JVM 互相绑定的。堆拥有以下特点:
以下异常情况与 Java 虚拟机堆栈相关联:
StackOverflowError
异常,如:无中断递归、无返回栈帧压入等OutOfMemoryError
异常通过 javap
指令得到反编译的字节码文件中一个方法的流程代表一个栈帧,一个栈帧中存放了 jvm 的指令
public static int calc(int, int);
Code:
0: iconst_3 // 将int类型常量3压入操作数栈
1: istore_0 // 将int类型值存入局部变量0
2: iload_0 // 从局部变量0中装载int类型值到操作数栈
3: iload_1 // 从局部变量1中装载int类型值到操作数栈
4: iadd // 执行int类型的加法
5: istore_2 // 将int类型值存入局部变量2
6: iload_2 // 从局部变量2中装载int类型值
7: ireturn // 从方法中返回int类型的数据
一个 JVM 进程中有多个线程正在执行,而线程中的内容是否能够拥有执行权,是根据 CPU 调度来的。
加入线程 A 正在执行到某个地方,突然失去了 CPU 的执行权,切换到了线程 B,然后当线程 A 再获得 CPU 执行权的时候,就需要通过线程中维护的一个变量来记录线程执行到哪个位置。程序计数器具有如下特点:
如果当前线程执行的方法是 Native 雷晓宁的,这些方法就会在本地方法栈中执行,主要通过 C/C++ 来执行。