Run-Time Data Areas 运行时数据区

Run-Time Data Areas 运行时数据区

原文地址:https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.5

关键点

  • PC寄存器:线程私有,指向当前线程正在执行的指令的地址
    • 唯一一个在Java虚拟机规范中没有规定任何OOM情况的区域
  • 栈:线程私有,存储一页一页的栈帧,帧中有局部变量等内容。栈在方法调用和返回时会对应的压栈或弹栈。
  • 堆:线程共享,是对象被创建的地方,也是GC工作的场所
  • 方法区:线程共享,存储类的信息,如运行时常量池,字段,方法等代码
  • 原生方法栈:线程私有,用于支持原生方法的执行
  • 运行时常量池:方法区中的一部分,它是对类文件中常量池表的运行时表达,存有多种常量。

全文翻译

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退出时被销毁。其他数据区域则是线程私有的。这些线程私有的数据区在一个线程创建时被创建,在线程销毁时被销毁。

The pc Register (PC寄存器)

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.

JVM可以支持很多线程并发执行。每个JVM线程都有自己的PC(程序计数器)寄存器。任何时刻,每个JVM线程都在执行一个方法的代码,即当前线程的当前方法。如果这个不是Native的,那么PC寄存器就会记录当前正在执行的JVM指令的地址。如果是Native的,那么PC寄存器的值就不会被定义。它可字节宽度足够装载一个返回地址值或者一个在特点平台上的原生指针

Java Virtual Machine Stack( 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.

每个JVM线程都有一个私有的JVM栈。这个栈是和线程在同时创建的,并存储着栈帧。我们可以将它和传统语言(如C)中的栈做类比:它会保存本地变量和中间结果,并在方法调用和返回中发挥一定作用。由于JVM栈从不被直接操作,除非压入和弹出帧,因此帧有可能在堆中被分配。JVM栈的内存空间不必是连续的。

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

在《Java虚拟机规范》第一版中,JVM栈被称为了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.

此规范允许JVM栈可以是固定大小,也可以根据计算依据需要来动态增加或压缩空间。如果JVM栈是固定大小的,那么这些栈在被创建时,其大小可能各自不同。

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.

一种JVM的实现可能会为开发者或者用户提供对JVM栈初始大小的控制。同样的,对于动态增减型的JVM栈而言,则可能会提供对栈空间上下限的控制。

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.

下面的两种异常情况都和JVM栈有关

  • 如果线程中的一个计算需要比允许值更大的JVM栈空间,那么JVM就会抛出栈溢出异常
  • 当JVM栈可以动态伸缩时,如果需要增加栈空间而缺乏足够的可用内存空间时,或者在为一个新的线程创建初始的JVM栈时,也发生了可用内存不够的情况,那么JVM就会抛出内存溢出异常

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.

JVM中有一个堆区域,这个堆为所有JVM线程所共享。堆是一种运行时数据区,是所有类实例和数字对象分配内存空间的地方。

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.

堆在虚拟机启动时被创建。堆存储的对象由一种自动存储管理系统(也就是大家所说的“垃圾清理”)进行回收处理。对象从未被显示的回收。JVM假设没有特别类型的自动存储管理系统。堆也许是固定大小或者也许是通过计算根据需要进行扩展,可能还会在大堆空间变得没有必要时进行压缩。堆内存不必是连续的

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.

一种JVM的实现可能为开发者和用户提供对初始堆大小的控制,同时,如果堆是可自动伸缩的,那么就可能提供对堆大小上下限的控制

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.

下面的异常情况和堆有关:

  • 如果一个计算需要比自动存储管理系统所能提供的可用空间还要大,那么JVM就会抛出内存溢出异常

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.

JVM有一个方法区,这个区域由所有的JVM线程所共享。在传统语言中,有一个区域是用于存储编译的代码。而在操作系统进程中,有一种段结构名为“文本”。JVM的方法区就可以和上面两种事物做类比。它存储了每个类的结构,例如运行时常量池,字段和方法数据以及方法和构造器的代码,其中包含了特殊方法(这些特殊方法用于类、实例和接口的初始化)。

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 version of the Java Virtual Machine 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.

方法区在JVM启动时被创建。尽管这个方法区从理论上讲是堆的一部分,但是在JVM的简单实现中也许会选择既不对其进行垃圾清理,也不对其进行空间压缩。本版本的JVM规范并不对方法区的位置以及对编译代码的管理策略做出要求。方法区可能为固定大小,也可能根据需求通过计算来动态增长,并在一个大方法区变得没有必要时对其进行压缩。方法区的内存空间不必是连续的。

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.

一种JVM的实现可能为开发者和用户提供对方法区初始大小的控制,同时,在方法区可动态变化大小时,对其空间上下限提供控制。

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.

下面的这个异常与方法区有关:

  • 如果内存中的方法区无法满足分配请求的大小,JVM就会抛出OOM异常

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.

每个运行时常量池都在JVM的方法区中被分配。对于创建的时间,由于每个类和接口都有一个运行时常量池,那么当一个类和接口被构造出时,常量池就会被创建出来。

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.

下面的这个异常就和一个类或接口对运行时常量池的创建有关:

  • 当创建一个类和接口时,如果运行时常量池的创建需要更多的内存,而JVM方法区却无法提供足够的空间时,JVM就会抛出OOM异常

See §5 for information about the construction of the run-time constant pool.

关于运行时常量池的更多信息,请参阅第5章节

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.

在一种虚拟机的实现中可能会使用一种常规的栈,俗称“C栈”。它是用于支持原生方法的(所谓Native方法,指的是Java语言以外编写的方法)。对于JVM指令集解释器的实现会使用另一种语言,如C。在这种场合下原生方法栈也可能被使用。有些JVM的实现并不能支持加载原生方法,也有些无需依靠传统的栈。对于这些JVM的实现方式而言,就无需再添加对原生方法栈的支持了。如果添加了对此的支持,依照惯例,在每个线程创建出来时,就会配套分配出一个对应的原生方法栈。

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.

一种JVM的实现可能会为开发者和用户提供对原生方法栈初始大小的控制,同时,对于可变大小类型的栈,则可能提供对栈空间上下限的控制。

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.

下面的两种异常与原生方法栈相关:

  • 如果一个线程中的计算需要比允许值更大的原生方法栈空间,那么JVM就会抛出栈溢出异常
  • 如果原生方法栈可以动态伸缩,但是当增长的需求超出内存可用空间时,或者,当没有足够的内存空间支持为新线程初始化原生方法栈时,JVM就会抛出内存溢出异常。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值