`
qiuqiao2000
  • 浏览: 29854 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

JVM

    博客分类:
  • Java
阅读更多

摘自《Java面向对象编程》

Java虚拟机提供了程序运行时环境,运行时环境中最重要的一个资源是运行时数据区。运行时数据区是操作系统为Java虚拟机进程分配的内存区域,主要包括堆区、方法区和Java栈区等。

l         方法区中存放类的类型信息,类型信息包括静态变量和方法信息等,方法信息中包含类的所有方法的字节码。

l         堆区中存放对象,对象的实例变量。

l         主线程在Java栈区内有一个方法调用栈,每执行一个方法,就会向方法调用栈中压入一个包含该方法的局部变量和参数的栈帧。

 

l         方法区存放了线程所执行的字节码指令。

l         堆区存放了线程所操纵的数据(以对象的形式存放)。

l         Java栈区则是线程的工作区,保存线程的运行状态。

 

Java虚拟机进程中,执行程序代码的任务是由线程来完成的。每个线程都有一个独立的程序计数器和方法调用栈(method invocation stack)。

l         程序计数器:也称为PC寄存器,当线程执行一个方法时,程序计数器指向方法区中下一条要执行的字节码指令。

l         方法调用栈:简称方法栈,用来跟踪线程运行中一系列的方法调用过程,栈中的元素称为栈帧。每当线程调用一个方法的时候,就会向方法栈中压入一个新帧。帧用来存储方法的参数、局部变量和运算过程中的临时数据。

栈帧由以下3部分组成:

l         局部变量区:存放局部变量和方法参数。

l         操作数栈:是线程的工作区,用来存放运算过程中生成的临时数据。

l         栈数据区:为线程执行指令提供相关的信息,包括如何定位到位于堆区和方法区的特定数据,以及如何正常退出方法或异常中断方法。

摘自《The Structure of the Java Virtual Machine

http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html

3.5 Runtime Data Areas

The Java virtual machine defines various runtime 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 虚拟机定义好几种运行期数据结构,用于程序的执行。一些数据结构是由Java虚拟机启动时创建,退出时销毁。也有一些数据结构用于线程。用于线程的数据结构是由线程启动时创建,退出时销毁。

3.5.1 The pc Register

The Java virtual machine can support many threads of execution at once (§2.19). 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, the current method (§3.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.

3.5.1 PC 寄存器

虚拟机支持多线程并行。每个Java线程都有自己的PC寄存器。不管怎样,Java线程只是自执行单个方法,线程的当前方法的代码。 方法不是native的话,PC寄存器就包含一个地址,指向虚拟机当前指令。 如果方法是native的话,PC寄存器的值就是undefined.因为Java虚拟机的pc寄存器是足够大,所以能够保持一个returnAddress 或一个特定平台的native指针。

3.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.3 A Java virtual machine stack stores frames (§3.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.

The Java virtual machine 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.4

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.

3.5.2 虚拟机栈

虚拟机线程拥有一个私有的虚拟机栈,创建线程的时候也同时创建线程栈。

栈存储frames,栈类似于其他语言,如C语言中的栈。存储local 变量和中间结果。同时方法的调用和返回的操作也用到了栈。对Java 虚拟机的栈操作,只能是pushpop frames.  栈内存不需要连续。虚拟机规范栈内存可以固定大小也可以由计算程序自动扩展和缩小。

如果栈大小固定的话,当栈创建的时候,虚拟机栈的大小是有虚拟机自由选择的。没有约束。

但是,如果栈的大小是动态的扩展和缩小的话,则虚拟机可以在栈的初始化,控制栈的最大和最小值。

虚拟机栈的异常条件如下:

l        线程的计算请求的栈大小超过了虚拟机提供的最大值,则抛出StackOverflowError

l        当没有足够多的内存来创建一个新线程请求的初始化栈,则抛出OutOfMemoryError

3.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 a UNIX process. It stores per-class structures such as the runtime constant pool, field and method data, and the code for methods and constructors, including the special methods (§3.9) used in class and instance initialization and interface type 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 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.

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

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.

3.5.4 方法区

虚拟器有一个所有线程共享的方法区方法区类似于普通语言编译后代码的存储区,也类似于Unix进程的Text segment。存储类结构,像运行期类常量池,字段,方法数据,方法代码,构造方法代码。类中特殊代码,实例的初始化,接口的初始化。

方法区是由虚拟机启动时候创建的,虽然方法区于Java堆内存的一部分,本版虚拟机规范没有制定方法区的位置和方法区如何管理编译后的代码的策略。可以固定,也可以动态增长。内存空间可以不连续。

虚拟机栈的异常条件如下:

l        方法区的内存不能满足请求的内存大小时,则抛出OutOfMemoryError

3.5.3 Heap

The Java virtual machine has a heap that is shared among all Java virtual machine threads. The heap is the runtime 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.5

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.

3.5.3 堆区

Java的堆是所有虚拟机线程共享的。也是一个running data area. 是为类实例和array 实例分配内存的数据区。虚拟机启动创建。堆为Object分配的存储区域是由自动存储管理系统(也就是垃圾回收系统)回收的。对象自己不能显式的释放。虚拟机没有指定特定类型的垃圾回收系统,而是由实现者根据系统的需要自行制定垃圾回收技术。堆内存可以固定大小也可以动态的扩展和缩小。堆内存可以不连续。

虚拟机的实现者可以提供程序员或者使用者控制堆内存的初始化大小,如果堆内存是动态增长的话,则要控制堆内存的最大和最小值。

虚拟机栈的异常条件如下:

如果计算程序请求的内存超过了自动存储系统能够提供的内存的话,就抛出OutOfMemoryError

分享到:
评论

相关推荐

    JVM入门实战/arthas实战/垃圾回收算法/垃圾回收器/jvm内存模型分析

    本系列课程从JVM基础到高级实战,老师手把手教你如何进行JVM调优,思路清晰,没有废话,旨在挑战高薪。 课程亮点: 1、基于阿里arthas进行JVM调优; 2、基于代码排查OOM问题,拒绝空讲; 3、总结JVM通用的调优思路;...

    JVM基础.doc

    学习关于JVM基础,java内模型的全面透析,Jar hell问题以及解决办法,Class文件格式 • Java编译执行流程 • ClassFile的格式介绍 • ClassFile中FieldInfo和MethodInfo介绍 • 类型描述Descriptor介绍 • ClassFile...

    jvm指令手册 +JVM必知必会,掌握虚拟机编译过程.rar

    JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的。 引入Java语言虚拟机后,Java语言在不同平台...

    jvm

    jvm

    涨见识!JVM性能监控与调优实战 一线大厂大牛讲师的JVM优化案例与解决方案课程

    JVM性能监控与调优实战课程,作为整篇课程的重中之重,非常值得同学们参考学习。课程前端讲解了JVM的性能监控和调优的概述,对调优的的方法和工具进行讲解学习,让同学们掌握方法,理解知识。课程的中间阶段我们进行...

    jdk,jvm源码

    jvm源码,jvm-native的源码,jvm支行机制,可对jvm的运行过程进行分析 个人网站:https://www.zhangjunbk.com

    Introduction to JVM Languages

    Introduction to JVM Languages English | 2017 | ISBN-10: 178712794X | 390 pages | PDF/MOBI/EPUB (conv) | 6.42 Mb Key Features This guide provides in-depth coverage of the Java Virtual Machine and its ...

    JVM与GC调优课程视频

    JVM与GC调优课程视频 〖课程介绍〗: JVM与GC调优课程视频 〖课程目录〗: 1.笔记/ ├── 第1篇-字节码篇.png?x-oss-process=style/pnp8 ├── 第2篇-类的加载篇.png?x-oss-process=style/pnp8 ├── 第3篇-运行时...

    (46页完整版)JVM体系结构与GC调优.zip

    46页PPT详解JVM,46页PPT详解JVM,46页PPT详解JVM,46页PPT详解JVM,46页PPT详解JVM,46页PPT详解JVM,46页PPT详解JVM,46页PPT详解JVM,46页PPT详解JVM,46页PPT详解JVM,46页PPT详解JVM,46页PPT详解JVM,46页PPT...

    JVM面试资料:JVM结构、JVM调优、四大垃圾回收算法、七大垃圾回收器

    JVM面试资料。 JVM结构:类加载器,执行引擎,本地方法接口,本地内存结构; 四大垃圾回收算法:复制算法、标记-清除算法、标记-整理算法、分代收集算法 七大垃圾回收器:Serial、Serial Old、ParNew、CMS、Parallel...

    nginx-upstream-jvm-route 和 nginx 对应版本,亲测可用

    此资源有两个文件,含 nginx-upstream-jvm-route 和 nginx 对应版本,都是tar.gz文件。 安装方法网上很多就不写了,亲测可用。 不用担心版本不匹配造成安装失败,再浪费积分去到处下载尝试的烦恼。 此资源有两个文件...

    metrics-jvm-3.1.5-API文档-中文版.zip

    赠送jar包:metrics-jvm-3.1.5.jar; 赠送原API文档:metrics-jvm-3.1.5-javadoc.jar; 赠送源代码:metrics-jvm-3.1.5-sources.jar; 赠送Maven依赖信息文件:metrics-jvm-3.1.5.pom; 包含翻译后的API文档:...

    Android Studio 报错failed to create jvm error code -4的解决方法

    代码如下:failed to create jvm error code -4 这一般应是内存不够用所致,解决方法参考如下。 打开 Android Studio 安装目录下的bin目录,查找并打开文件 studio.exe.vmoptions,修改代码: 代码如下:-Xmx512m 为...

    jvm 详细介绍,了解jvm各个组成部分和功能

    jvm 详细介绍,了解jvm各个组成部分和功能

    JVM调优实践 ⼀、JVM调优准备⼯作 实验报告 pdf

    JVM调优实践 ⼀、JVM调优准备⼯作 1. 机器环境: 机器 CPU 内存 单机 4C 16G 2. 默认启动参数 JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn256 -XX:MetaspaceSiz e=128m -XX:MaxMetaspaceSize=320m" JAVA_...

    jvm内存参数调优

    其二是非标准参数(-X),默认jvm实现这些参数的功能,但是并不保证所有jvm实现都满足,且不保证向后兼容; 其三是非Stable参数(-XX),此类参数各个jvm实现会有所不同,将来可能会随时取消,需要慎重使用; 本文...

    jvm内存状况查看

    Java 本身提供了多种丰富的工具来帮助开发人员查看和分析 GC 以及 JVM 内存的状况。 输出GC日志 输出 GC 日志对于跟踪分析 GC 的状况,无疑是最明显和直接的分析内存回收状况的方 法,只是 GC 日志输出后需要人肉的...

    深入JVM内核 - 原理、诊断与优化

    目前,Java是最为流行的编程语言之一,它的基础平台就是JVM。除了Java,如JRuby、Scala、Clojure等语言也运行在JVM平台。熟悉和掌握JVM平台有着重要的实用价值和意义。 在本课程中个,将详细介绍JVM的基本原理、...

    JVM内存空间分配笔记

    主要是JVM内存分配及简单的JVM性能调优

    JVM规范--高手总结

    JVM规范--高手总结 Java相关 1 1.1Java定义 1 1.2Java的开发流程 1 1.3Java运行的原理 2 1.4半编译半解释 3 1.5平台无关性 4 JVM内存模型 4 2.1 JVM规范 5 2.2 Sun JVM 8 2.3 SUN JVM内存管理(优化) 10 2.4 SUN JVM...

Global site tag (gtag.js) - Google Analytics