java 的数据区_Java8-jvm运行时数据区(权威解析)

一 说明

网上很多关于jvm运行时数据区的文章描述都是错误的,书上的内容也好多都是过时;基于此情况,作者今天有时间特地去官网查阅资料进行解析,希望能帮到那么被误导的同学们,如果相关的译文有不正确之处请指出,谢谢!!!!

二 程序计数器(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, 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(程序计数器)寄存器,在任何时候,每一个java虚拟机线程执行的方法都是当前线程单一的方法。如果方法不是本地的方法,程序计数器就会包含java虚拟机的指令地址,并且执行。如果是本地方法,程序计数器就是未定义的。当在特定的平台上,jvm的pc只要足够的宽,就会持有一个returnAddress 和 native pointer。

三 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.

每一个jvm线程都有一个jvm栈,当线程被创建的时候随之创建,jvm栈存储的是 `frames`(帧,不是本文的重点不会详细描述,其作用时存储数据和部分的返回值,并且能够执行动态链接,拿到方法的返回值,和调度异常)。jvm栈其实跟c语言的栈类似。它持有本地变量和部分的结果集,参与方法的调用和返回。由于java虚拟机栈除了对栈的push和pop并不会直接的操作frames,frames有可能是heap(堆)所分配。因此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虚拟机栈式固定的大小,那么在java虚拟机栈被建立的时候,每一个java虚拟机栈都是可选独立的大小。

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

如果线程计算的java虚拟机栈要大小超过了java虚拟机所允许的大小,就会抛出 `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虚拟机栈,Jvm就会抛出 `OutOfMemoryError` 异常。

四 堆(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.

java虚拟机的堆是被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.

在java虚拟机启动的时候堆也随着创建。堆存储的是对象,这些对象是被动态存储管理系统所回收(广泛的被称为垃圾收集器)。对象是不会显示的被释放。由于存储技术(垃圾回收器)是由实现者的系统所要求的,java虚拟机是不会认定没有特别类型的垃圾回收器。堆的大小可能是固定的,也可能是扩展的,如果堆的大小比较大,并且不是必须的,那么堆可以是收缩变小。堆的内存不是连续的。

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虚拟机就会抛出 `OutOfMemoryError` 异常;

五 方法区(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 方法区同样是被所有线程共享的。方法区类似于常规语言编译代码存储的区域,或者类似于操作系统进程的文本块。

方法区存储的是每个类的结构比如运行时常量池,字段,方法数据,和构造器或者方法的代码,包括特殊的方法( `` 方法 本文不会详细介绍,有兴趣的朋友可以查阅相关资料或者我的“图解类的加载机制一文”),这些方法的作用是接口和类的初始化。

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.

方法区也是随着jvm启动的时候创建,尽管方法区是堆逻辑区中的一部分,但是可以选择不进行垃圾回收或者压缩。java虚拟机规范没有规定要求方法区具体的配置或者管理代码编译的策略(可能根据不同版本的jdk,那么其方法区的位置等也是不同的)。

If memory in the method area cannot be made available to satisfy an allocation request, the Java Virtual Machine throws an OutOfMemoryError.

如果方法区的内存大小无法满足分配的需求,java虚拟机就会抛出 `OutOfMemoryError` 异常。

六 运行时常量池(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.

运行时常量池是每个类或者接口在运行时类文件中所代表的 `constant_pool table` (常量池表);其包含多种常量,范围从编译时已知的数字文字到必须在运行时解析的方法和字段引用。运行时常量池的功能类似于常规编程语言的 `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.

每一个运行时常量池都是来自java虚拟机方法区所分配,当java虚拟机创建类或者接口的时候,会对应的创建类或者接口的运行时常量池。

七 本地方法栈(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.

java虚拟机的实现可能会使用C语言的堆栈用来支持本地方法栈(意指java的本地方法栈并不是Java语言所编写的);其对应实现的解释器用来解析java的指令集也不是java语言所编写的,比如c语言。java虚拟机的实现无需加载本地方法或者本身,其依赖于本地方法栈;如果提供本地方法栈,当线程被创建的时候会为其每一个线程分配独立的本地方法栈。

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.

java虚拟机规范允许本地方法栈固定大小或者动态扩展和收缩。如果本地方法栈是固定大小,当栈建立的时候可以独立分配大小。

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

如果线程计算的本地方法栈要大小超过了java虚拟机所允许的大小,就会抛出 `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虚拟机栈或者没有足够的内存初始化新线程的Java虚拟机栈,Jvm就会抛出 `OutOfMemoryError` 异常。

八 启动顺序和存储对象总结

九 线程共享性总结

十内存连续性总结

十一 异常和内存连续性总结

十二 关于永久代和元空间

作者在官网没有找到相关信息,但根据jdk自带的调优工具显示了javaSE 1.8 确实永久代已经从方法区中移除,查阅了相关资料表名:元空间取代了永久代,并且是在堆空间上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值