JDK1.8 The Java® Virtual Machine Specification《2.The Structure of the Java Virtual Mach》(4)

JDK1.8 The Java® Virtual Machine Specification《2.The Structure of the Java Virtual Mach》(4)_第1张图片 JDK1.8 The Java® Virtual Machine Specification《2.The Structure of the Java Virtual Mach》(4)_第2张图片

2.5 Run-Time Data Areas

2.5 运行时方法区

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.

java虚拟机定义了许多运行时数据区,他们在运行程序的时候被使用。一些数据区域在虚拟机启动的时候被创建,在虚拟机退出的时候被销毁,其他的数据区域会依据每一个线程来创建。每一个线程数据区的诞生是一句线程的创建,同样的线程数据区的销毁也是伴随着线程的销毁。

2.5.1. The pc Register

2.5.1 PCR 程序计数器

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虚拟机支持多线程运行,每一个java虚拟机线程都有自己的pc计数器。在其中的某一时刻每一个虚拟机线程会执行一个方法的代码,并且这个线程的名字会跟当前正自执行的方法相同。如果这个方法不是本地方法(native),pc计数器将会拥有正在执行的java虚拟机指令地址。如果正在被线程执行的是个本地方法,PC计数器将会拥有一个未定义的值(undefine)。在特定平台上java虚拟机的PC计数器拥有足够大的空间容纳一个返回地址(returnAddress )或者本地方法指针

2.5.2. Java Virtual Machine Stacks

2.5.2 java虚拟机栈

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.

In the First Edition of The Java® Virtual Machine Specification, the Java Virtual Machine stack was known as the Java stack.

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.

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.

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虚拟机栈存储的是栈帧(frames )。一个java虚拟机栈与我们平时常见的栈很相似,例如C语言中的栈,它存储了本地变量和部分的结果,并且参与了方法的调用和返回。由于虚拟机栈只有入栈和出栈(push and pop)两种操作,其他的操作从来都不会被作用在虚拟机栈上,栈帧是可以在堆分配的,并且虚拟机栈并不要求是连续的。

在最初的java虚拟机规范中java虚拟机栈也被乘坐java栈。

这样的规范允许java虚拟机栈要么可以指定大小,要么动态的扩大并且按照计算机的评估结果去缩小。如果java虚拟机栈的长度是一个固定大小的值,每一个java虚拟机栈的大小都可以在创建的时候指定大小。

java虚拟机实现了用程序去控制或者由用户去指定一个栈的初始化大小,最大值和最小值,以至于在虚拟机栈动态扩大或缩小的时候可以不会越界。

下面是关于虚拟机栈的一些异常条件:

  • 如果虚拟机的一个线程通过计算需要一个更大的虚拟机栈,但是这个长度超过了允许的栈长度,这个时候虚拟机会抛出一个栈溢出的异常(StackOverflowError
  • 如果java虚拟机栈可以动态的,并且它扩展时期望得到的内存大小超过所能提供的大小(由于内存不足)或者新线程创建的时候没有足够的内存去创建一个初始虚拟机栈,这个时候虚拟机会抛出一个内存溢出的异常(OutOfMemoryError

 

2.5.3. Heap

2.5.3 堆

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虚拟机启动的时候堆就已经被创建了,堆中存储的对象会被自动存储管理系统回收,我们称该系统为垃圾收集器(GC- garbage collector)。对象永远不会被显式的释放掉(不像C语言那样)。虚拟机并会假设自己没有特定类型的垃圾收集器,实现者可以自行制定一个垃圾收集器的管理技术。堆可以是固定大小,也可以按需扩大或缩小,并且堆的内存空间并不要求是连续的。

java虚拟机堆实现了程序控制或者用户控制初始化大小,与此同时也可以指定一个堆的最大最小值来供堆动态扩大或者缩小。

下面阐述了一些与堆相关的异常

  • 如果虚拟机需要更多的空间去存储对象但是可用的空间不足,则会导致虚拟机抛出内存溢出错误(OutOfMemoryError

2.5.4. Method Area

2.5.4 方法区

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虚拟机还拥有一个被所有线程所共享的方法区,方法区类似于其他常见语言的编译后代码存储区或操作系统的文本段。它存储了每个类文件的结构,例如运行时常量池,字段和方法的数据,一些方法和构造函数的代码,还有一些类初始化或者接口初始化时包含的特殊方法。

方法区在虚拟机启动的时候创建,虽然方法区逻辑上属于堆的一部分,但是简单实现的方法区并不会选择GC或者压缩。规范中不会要求方法区数据的存储位置或者用一些策略去管理编译后的代码。方法区可以是固定大小的或者是按照需求动态扩大或者收缩。而且并不要求其内存连续性。

java虚拟机可由程序或者用户自行指定其固定大小或者最大最小值,以方便在区域动态扩容的时候对其进行约束。

下面的异常与方法区相关

如果内存没有足够的区域供方法区分配,虚拟机会抛出内存溢出的异常(OutOfMemoryError

2.5.5. Run-Time Constant Pool

2.5.5 运行时常量池

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 run-time 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.

运行时常量池是一个针对单一类或者单一接口类文件中常量池表的运行时的表现形式。它包含了许多的类型的常量,编译时获取的数字或者字面常量必须在运行时与方法和字段进行关联,运行时常量池供方法使用,它类似于传统编程语言的符号表,虽然他比典型的符号表拥有更宽范的数据

每一个运行时常量池都会被分配在jvm的方法区中,它在虚拟机创建类或者接口的时候创建。

下述异常与运行时常量池关联

创建类或者接口时如果运行时常量池得不到足够的空间,就会抛出内存溢出的异常(OutOfMemoryError

 

2.5.6. Native Method Stacks

2.5.6 本地方法栈

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方法 即用不同语言编写的一些方法)提供服务。本地方法栈也会像c语言一样,实现一个基于java虚拟机指令集的解释器。如果不去加载本地方法或者不去依赖传统的栈,那么这个虚拟机就不需要去提供本地方法栈,如果提供,本地方法栈需要依据每个线程而创建。

规范中约束了本地方法栈既可以是固定大小也可以动态扩展,如果本地方法栈需要一个固定的大小,那么在创建的时候可以有选择性的指出每一个栈的大小。

栈可以实现程序员或用户指定的初始化大小,也可以自己划定一个范围来约束栈的大小。

下面异常与本地方法栈相关:

  • 如果一个线程需要的本地方法栈大小超过了允许的大小,那么会抛出堆栈溢出(StackOverflowError
  • 如果没有足够的内存供本地方法栈进行动态的扩展,那么会抛出内存溢出(OutOfMemoryError

你可能感兴趣的:(研读java虚拟机规范)