The Structure of the Java Virtual Machine Java虚拟机结构 虚拟机内存模型
小结:
1、实现一台Java虚拟机,只需正确读取class文件中的每一条字节码指令且能正确执行这些指令所蕴含的操作。
2、设计者决定:运行时数据区的内存如何布局,选择哪种垃圾收集算法,是否对虚拟机字节码指令进行一些内部优化操作(如何用即时编译器把字节码编译为机器码)
3、请注意避免混淆Stack、Heap和Java(VM)Stack、Java Heap的概念。Java虚拟机的实现本质上是由其他语言所编写的应用程序,Java语言程序里分配在Java Stack中的数据,从实现虚拟机的程序角度上看则可能分配在Heap中。
4、
gc的对象存在与虚拟机堆内存中。
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.
Chapter 2. The Structure of the Java Virtual Machine https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-2.html#jvms-2.4
2.5. Run-Time Data Areas
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.
2.5.1. 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虚拟机线程只会执行一个方法的代码,这个正在被线程执行的方法称为该线程的当前方法。
如果该方法不是native 的,则pc寄存器就保存Java虚拟机正在执行的字节码指令的地址,否则,则pc寄存器的值是undefined。
pc寄存器的容量至少应当能保存一个returnAddress类型的数据或者一个域平台相关的本地指针的值。
2.5.2. 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.
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虚拟机栈,这个栈与线程同时创建,用于存储栈帧。
其作用与传统语言(例如C语言)中的栈非常类似,用于存储局部变量与一些尚未算好的结果。
其可能发生的异常情况:
如果线程请求分配的栈容量超过Java虚拟机栈允许的最大容量,Java虚拟机将会抛出一个StackOverflowError异常。
如果Java虚拟机栈可以动态扩展,并且在尝试扩展时无法申请到足够的内存,或者在创建新的线程时没有足够的内存去创建对应的虚拟机栈,那么Java虚拟机将会抛出一个OutOfMemoryError异常。
2.5.3. 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堆在虚拟机启动的时候就被创建,它存储了被自动内存管理系统(automatic storage management system,也就是常说的garbage collector 垃圾收集器)所管理的各种对象,这些受管理的对象无需也无法显示地销毁。
Java堆的容量可以是固定的,也可以随着程序执行的需求动态扩展并在不需要过多空间时自动收缩。
其可能发生的异常情况:
如果实际所需的堆超过了自动内存管理系统能提供的最大容量,那么Java虚拟机将会抛出一个OutOfMemoryError。
2.5.4. 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 used in class and interface initialization and in instance initialization (§2.9).
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虚拟机中,方法区是可供各个线程共享的运行时内存区域。
方法区与传统语言中的编译代码存储区(storage area for compiled code),或者操作系统进程的正文段(text segment)的作用非常类似,它存储了每一个类的结构信息,例如,运行时常量池(runtime constant pool)、字段和方法数据、构造函数和普通方法的字节码内容,还包括一些在类、实例、接口初始化时用到的特殊方法。
其可能发生的异常情况:
如果方法区的内存空间不能满足内存分配请求,那么Java虚拟机将会抛出一个OutOfMemoryError。
2.5.5. 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 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.
运行时常量池是class文件中每一个类或接口的常量池表的运行时表示形式,它包括了若干哪种不同给的常量,从编译期可知的数值字面量到必须在运行期解析后才能获得的方法或字段引用。
运行时常量池类似于传统语言中饭的符号表,不过它存储数据的范围比通常意义上的符号表更为广泛。
其可能发生的异常情况:
当创建类或接口时,如果构造运行时常量池所需要的内存空间超过了方法区所能提供的最大值,那么Java虚拟机将会抛出一个OutOfMemoryError。
2.5.6. 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 stack)来支持native方法(指使用Java以为的其他语言编写的方法)的执行。
当Java虚拟机使用其他语言(例如C语言),来实现指令集解释器时,也可以使用本地方法栈。
如果Java虚拟机不支持native方法或者本身不依赖传统栈,那么可以不提供本地方法栈;如果支持本地方法栈,那么这个栈一般会在线程创建的时候按线程分配。
其可能发生的异常情况:
如果线程请求分配的栈容量超过Java虚拟机栈允许的最大容量,Java虚拟机将会抛出一个StackOverflowError异常。
如果本地方法栈可以动态扩展,并且在尝试扩展时无法申请到足够的内存,或者在创建新的线程时没有足够的内存去创建对应的本地方法栈,那么Java虚拟机将会抛出一个OutOfMemoryError异常。
The Structure of the Java Virtual Machine Java虚拟机结构 虚拟机内存模型的更多相关文章
- 60年代进程 80年代线程 IPC How the Java Virtual Machine (JVM) Works
小结: 1. To facilitate communication between processes, most operating systems support Inter Process C ...
- What is Java virtual machine?
Java Virtual Machine (JVM) is a specification that provides runtime environment in which java bytec ...
- JVM Specification 9th Edition (3) Chapter 2. The Structure of the Java Virtual Machine
Chapter 2. The Structure of the Java Virtual Machine 内容列表 2.1. The class File Format (class文件的格式) 2. ...
- JVM Specification 9th Edition (4) Chapter 3. Compiling for the Java Virtual Machine
Chapter 3. Compiling for the Java Virtual Machine 内容列表 3.1. Format of Examples 3.2. Use of Constants ...
- fail to create java virtual machine..
今天打开zend stdio 的时候 出现的错误 fail to create java virtual machine... 然后找度娘了,,都说改xxxxx, 我打开360 ,把内存清理了一遍 ...
- Failed to create the Java Virtual Machine.问题的解决
运行Eclipse,出现了"Failed to create the Java Virtual Machine."错误: 解决的办法是在Eclipse的解压目录下找到eclipse ...
- eclipse failed to create the java virtual machine 问题图文解析
eclipse failed to create the java virtual machine 问题图文解析 分类: java常用软件异常2010-10-02 23:45 73200人阅读 评论( ...
- [转载]Eclipse提示No java virtual machine
第一次运行Eclipse,经常会提示下面的问题:... No java virtual machine was found after searching the follwing location ...
- SQL Developer 4.0 启动报错“unable to create an instance of the java virtual machine located at path”
安装了Oracle之后,第一件事情就是想想怎么去连接,进而操作.SQL Developer是官方提供的强大工具,个人看来也是第一选择. 目前官网提供的最新版是4.0.1.14.48,下载下来之后,就跃 ...
随机推荐
- Spark 底层网络模块
文章正文 对于分布式系统来说,网络是最基本的一环,其设计的好坏直接影响到整个分布式系统的稳定性及可用性.为此,Spark专门独立出基础网络模块spark-network,为上层RPC.Shuffle数 ...
- 使用vw做移动端页面的适配
Flexible到今天也有几年的历史了,解救了很多同学针对于H5页面布局的适配问题.而这套方案也相对而言是一个较为成熟的方案.简单的回忆一下,当初为了能让页面更好的适配各种不同的终端,通过Hack手段 ...
- GDB 调试器使用手冊
使用GDB: 本文描写叙述GDB,GNU的原代码调试器. (这是4.12版1994年一月.GDB版本号4.16) * 文件夹: * 摘要: GDB的摘要 * 实例: 一个使用实例 * 入门: 进入和退 ...
- 【Java】Comparable和Comparator接口的区别
Java提供了只包含一个compareTo()方法的Comparable接口.这个方法可以个给两个对象排序.具体来说,它返回负数,0,正数来表明已经存在的对象小于,等于,大于输入对象. Java提供了 ...
- 【iCore4 双核心板_ARM】例程十五:USB_CDC实验——高速数据传输
实验方法: 1.安装USB CDC驱动,驱动安装方法参考例程包中安装方法文档. 2.将跳线冒跳至USB_OTG,通过Micro USB 线将iCore4 USB-OTG接口与电脑相连. 3.打开上位机 ...
- Java知多少(33)多态对象的类型转换
这里所说的对象类型转换,是指存在继承关系的对象,不是任意类型的对象.当对不存在继承关系的对象进行强制类型转换时,java 运行时将抛出 java.lang.ClassCastException 异常. ...
- 安装psycopg2时出错:Error: pg_config executable not found.
红帽系: 安装postgresql-devel Debian系: 安装libpq-dev
- centos7 centos中apache运行php需要连接mysql一直连不上,telnet访问mysql出错Connection closed by foreign host
执行命令: getsebool -a|grep httpd 发现 httpd_can_network_connect off 解决: setsebool httpd_can_network_conn ...
- nginx 配置信息
主配置文件: cat /etc/nginx/nginx.conf# For more information on configuration, see:# * Official English Do ...
- 理解AppDomain
在传统的 Win32的程序中,进程是独立的运行空间, 在一些大型系统中, 通常都是将系统中的核心功能分解出来用独立的进程来处理,一方面是为了能获得更高的系统性能.吞吐量 .另一方面是为了能隔离功能之间 ...