深入理解JVM虚拟机-读书笔记【三】运行时数据区域

版权声明: https://blog.csdn.net/jiangxuexuanshuang/article/details/88375728

具体可参考官方《java虚拟机标准》:https://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf

1、运行时数据区划分

2、程序计数器(Program Counter 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, namely the current method (§2.6) for that thread. If that method is not
native, the pc register contains the address of the Java Virtual Machine instruction
currently being executed. If the method currently being executed by the thread is
native, the value of the Java Virtual Machine's pc register is undefined. The Java
Virtual Machine's pc register is wide enough to hold a returnAddress or a native
pointer on the specific platform
Java虚拟机可以同时支持多个执行线程(JLS)。每个Java虚拟机线程都有自己的PC(程序计数器)寄存器。
在任何时候(一个确定的时间,一个处理器),每个Java虚拟机线程都只执行单个代码块,即该线程的当前方法。如果那个方法不是在本地,PC寄存器包含Java虚拟机指令的地址。如果线程当前正在执行的方法是本地的,Java虚拟机的PC寄存器的值是未定义的。JAVA虚拟机的PC寄存器足够大,可以容纳返回地址或本机特定平台上的指针

程序的存储区域较小,存储的是执行程序的计数值(说明当前执行的是哪一行代码),程序根据此计数器的值来确定分支、循环、跳转、线程处理、异常恢复等的执行顺序。

可以类比:每个代码都有一个数字来指向其地址,而计数器就是记录执行代码的这个数字,知道代码执行的位置。

此内存区域是唯一一个在 Java 虚拟机规范中没有规定任何 OutOfMemoryError 情况的区域。

2、Java虚拟机栈(Java Virtual Machine Stacks)

Each Java Virtual Machine thread has a private Java Virtual Machine stack, created
at the same time as the thread. A Java Virtual Machine stack stores frames (§2.6).
A Java Virtual Machine stack is analogous to the stack of a conventional language
such as C: it holds local variables and partial results, and plays a part in method
invocation and return. Because the Java Virtual Machine stack is never manipulated
directly except to push and pop frames, frames may be heap allocated. The memory
for a Java Virtual Machine stack does not need to be contiguous.
每个Java虚拟机线程都有一个私有Java虚拟机栈,与线程同时创建(生命周期与线程相同)。
Java虚拟机栈存储帧。
Java虚拟机栈类似于传统语言的堆栈,例如C/C++语言。
它保存局部变量和局部结果,并在方法中起调用和返回的作用。
因为Java虚拟机栈从不被直接操作,除了推送和弹出帧外,帧可以被堆分配。
对于Java虚拟机栈的存储是不需要连续的。

Java虚拟机栈存储的是当前线程执行过程中产生的局部变量,当运行到一行需要局部变量时push入栈,当局部变量无需继续使用时则pop出栈。

In the First Edition of The Java® Virtual Machine Specification, the Java Virtual Machine
stack was known as the Java stack.
在Java版虚拟机规范的第一版中,Java虚拟机栈被称为Java堆栈。

堆和栈是比较笼统的分法,这种比较流行的方法是大多数程序关注的内存分配方式。

This specification permits Java Virtual Machine stacks either to be of a fixed size
or to dynamically expand and contract as required by the computation. If the Java
Virtual Machine stacks are of a fixed size, the size of each Java Virtual Machine
stack may be chosen independently when that stack is created.
此规范允许Java虚拟机栈具有固定大小。或者根据计算需要动态地扩展和收缩。
如果是Java虚拟机栈是固定大小的,每个Java虚拟机的大小创建堆栈时,可以独立选择堆栈。
A Java Virtual Machine implementation may provide the programmer or the user control
over the initial size of Java Virtual Machine stacks, as well as, in the case of dynamically
expanding or contracting Java Virtual Machine stacks, control over the maximum and
minimum sizes.
一个Java虚拟机实现可以提供程序员或用户控件,在Java虚拟机栈的初始大小上,
以及在动态情况下扩展或收缩Java虚拟机栈,控制最大和最小尺寸。
The following exceptional conditions are associated with Java Virtual Machine
stacks:
• If the computation in a thread requires a larger Java Virtual Machine stack than
is permitted, the Java Virtual Machine throws a StackOverflowError.
• If Java Virtual Machine stacks can be dynamically expanded, and expansion is
attempted but insufficient memory can be made available to effect the expansion,
or if insufficient memory can be made available to create the initial Java Virtual Machine stack for a new thread, the Java Virtual Machine throws an
OutOfMemoryError.
以下异常条件与Java虚拟机堆栈相关:
如果线程中的计算需要超过了Java虚拟机栈允许的大小,Java虚拟机抛出StackOverflowError。
如果Java虚拟机栈可以动态扩展,并且扩展时已尝试但内存不足,无法进行扩展,
或者如果内存不足,无法为新线程创建初始Java虚拟机堆栈,则Java虚拟机抛出一个内存不足错误(OutOfMemoryError)。

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). Native method stacks may
also be used by the implementation of an interpreter for the Java Virtual Machine's
instruction set in a language such as C. Java Virtual Machine implementations
that cannot load native methods and that do not themselves rely on conventional
stacks need not supply native method stacks. If supplied, native method stacks are
typically allocated per thread when each thread is created.

This specification permits native method stacks either to be of a fixed size or to
dynamically expand and contract as required by the computation. If the native
method stacks are of a fixed size, the size of each native method stack may be
chosen independently when that stack is created.

A Java Virtual Machine implementation may provide the programmer or the user control
over the initial size of the native method stacks, as well as, in the case of varying-size native method stacks, control over the maximum and minimum method stack sizes.

The following exceptional conditions are associated with native method stacks:
• 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
一个Java虚拟机的实现可以使用传统的堆栈,通俗地称为“C堆栈”,以支持native(本地)方法(在除了Java编程语言之外的语言。
本地方法栈可以也可用于Java虚拟机解释器实现的语言中的指令集,例如C。
Java虚拟机实现可以不用加载本地方法,并且本身不依赖于常规方法栈不需要提供本地方法栈。
如果提供,则本地方法堆栈为通常在创建每个线程时为每个线程分配。
本规范允许本地方法堆栈具有固定大小或根据计算需要动态扩展和收缩。
如果是本地方法堆栈的大小是固定的,每个本机方法堆栈的大小可以是创建栈时独立选择。
Java虚拟机实现可以提供程序或用户控制。
对于本地方法堆栈的初始大小,以及在大小不同的本机方法堆栈的情况下,对最大和最小方法堆栈大小进行控制。
以下异常条件与本地方法堆栈关联:
•如果线程中的计算需要比当前更大的本机方法堆栈允许,Java虚拟机抛出StackOverflowError。
•如果可以动态扩展本机方法堆栈和本机方法堆栈尝试扩展,但内存不足。或者内存不足,无法为新线程创建初始本地方法栈,Java虚拟机抛出OutOfMemoryError

总的来说,本地方法栈不是虚拟机必须实现的。它为虚拟机执行本地方法分配相应的栈,与虚拟机栈类似;不同的是虚拟机是虚拟机内部的栈,本地方法是执行本地方法创建的栈。它的实现比较自由,如Sun HotSpot虚拟机将将本地方法栈与虚拟机合二为一统一分配。

4、Java堆(Heap)

The Java Virtual Machine has a heap that is shared among all Java Virtual Machine
threads. The heap is the run-time data area from which memory for all class
instances and arrays is allocated.
The heap is created on virtual machine start-up. Heap storage for objects is
reclaimed by an automatic storage management system (known as a garbage
collector); objects are never explicitly deallocated. The Java Virtual Machine
assumes no particular type of automatic storage management system, and the
storage management technique may be chosen according to the implementor's
system requirements. The heap may be of a fixed size or may be expanded as
required by the computation and may be contracted if a larger heap becomes
unnecessary. The memory for the heap does not need to be contiguous.
A Java Virtual Machine implementation may provide the programmer or the user control
over the initial size of the heap, as well as, if the heap can be dynamically expanded or
contracted, control over the maximum and minimum heap size.
The following exceptional condition is associated with the heap:
• If a computation requires more heap than can be made available by the
automatic storage management system, the Java Virtual Machine throws an
OutOfMemoryError.
Java虚拟机具有在所有Java虚拟机线程之间共享的堆。
堆是所有运行数据存储区域,分配对象实例的创建和数组的创建。

堆是在虚拟机启动时创建的。对象的堆存储由自动存储管理系统回收(称为垃圾收集器);
对象永远不会显式释放。Java虚拟机假设没有特定类型的自动存储管理系统,
并且存储管理技术可以根据实施者的系统要求进行实现。
堆的大小可以是固定的,也可以为计算所需进行扩展,一块大的不必要的堆会被回收,堆的内存不需要是连续的。

Java虚拟机实现可以提供程序或用户控制初始化的堆大小。
如果堆可以动态扩展或回收,需要控制最大和最小堆大小。

以下异常条件与堆有关:
•如果计算需要的堆超过自动存储管理系统可用的大小,Java虚拟机抛出一个内存不足错误(OutOfMemoryError)。

堆用来存储系统运行过程中创建的对象和数组。堆的内存不需要物理连续的,只需要逻辑连续。

5、方法区(Method Area)

The Java Virtual Machine has a method area that is shared among all Java Virtual
Machine threads. The method area is analogous to the storage area for compiled
code of a conventional language or analogous to the "text" segment in an operating
system process. It stores per-class structures such as the run-time constant pool,
field and method data, and the code for methods and constructors, including
the special methods (§2.9) used in class and instance initialization and interface
initialization.
The method area is created on virtual machine start-up. Although the method area
is logically part of the heap, simple implementations may choose not to either
garbage collect or compact it. This specification does not mandate the location of
the method area or the policies used to manage compiled code. The method area
may be of a fixed size or may be expanded as required by the computation and may
be contracted if a larger method area becomes unnecessary. The memory for the
method area does not need to be contiguous.
A Java Virtual Machine implementation may provide the programmer or the user control
over the initial size of the method area, as well as, in the case of a varying-size method area,
control over the maximum and minimum method area size.
The following exceptional condition is associated with the method area:
• If memory in the method area cannot be made available to satisfy an allocation
request, the Java Virtual Machine throws an OutOfMemoryError
Java虚拟机有一块方法区域在所有的虚拟机线程池之间共享。
方法区域传统语言编译代码存储区或者操作系统进程的正文段很相似。
它存储了每个类的结构,像运行时常量池,字段、方法数据,方法、构造函数的代码,
包括类、对象初始化、接口初始化的特殊方法。

方法区在虚拟机启动时创建,虽然方法区是堆的逻辑组成部分,
但是简单的实现可以选择不进行垃圾回收集与压缩(回收)。
此规范也不现在实现方法区的内存位置或者编译代码的管理策略。
方法区的大小可以是固定大小或者根据计算和扩展、大的不需要的方法区进行回收。
方法区内存不需要是连续的。

Java虚拟机实现可以提供程序或用户控制初始化的堆大小。
如果堆可以动态扩展或回收,需要控制最大和最小方法区大小。

以下异常条件与方法区有关:
•如果方法区的内存空间不能满足内存分配需求,
Java虚拟机抛出一个内存不足错误(OutOfMemoryError)。

6、运行时常量池(Run-Time Constant Pool)

A run-time constant pool is a per-class or per-interface run-time representation
of the constant_pool table in a class 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.
Each run-time constant pool is allocated from the Java Virtual Machine's method
area (§2.5.4). The run-time constant pool for a class or interface is constructed
when the class or interface is created (§5.3) by the Java Virtual Machine.
The following exceptional condition is associated with the construction of the runtime
constant pool for a class or interface:
• When creating a class or interface, if the construction of the run-time constant
pool requires more memory than can be made available in the method area of the
Java Virtual Machine, the Java Virtual Machine throws an OutOfMemoryError.
See §5 (Loading, Linking, and Initializing) for information about the construction of the
run-time constant pool.
运行时常量池是每个类或者每个接口的常量池表在class文件运行时的表现形式。
它包含了多种常量,从编译器期已知的到在运行时必须被解析的方法和字段引用。
运行时常量池类似于传统语言的符号表,但是它比符号表的范围大很多。

每一个运行时常量池在java虚拟机的方法区中分配,在类或者接接口在虚拟机中加载时
创建对应的类或接口的运行时常量池。

以下异常条件与构造类或接口的运行时常量池相关:

•创建类或接口时,如果运行时常量的构造池需要的内存超过了Java虚拟机方法区可用的大小,
Java虚拟机抛出OutOfMemoryError错误.

在class文件运行时会初始化相应的常量池用于存储对象中的常量。

后记:翻译理解完发现有Java虚拟机规范中文版,可以直接看中文版,不过可以看原版还是看原版,少了一层转化

猜你喜欢

转载自blog.csdn.net/jiangxuexuanshuang/article/details/88375728
今日推荐