(二)Java 虚拟机具体是怎样运行 Java 字节码的?

Write Once,Run Anywhere

Java 祖师爷就是由于发现用 C++ 开发的过程中会花大量的时间处理内存、不同的指令架构等的问题,所以才有高“移植性”的Java 诞生。采访Java 祖师爷的报道链接 。

 And  there are a bunch of places in C, where it’s really easy to make errors that can cause your system to fall over and just do bad things. Most of them have to do with the memory model. The two big ones we have, three big ones I guess were the unchecked array bounds, the weird and wonderful world of pointers and then the fact that you can cast anything to anything and they’re always completely unchecked.

And  when these consumer electronics companies, to look through the things that caused them reliability issues; those were like nearly at the top. And so, it’s like well okay, so, I was the person in the project who was tasked with, well go off and try to figure out how to fix these problems. And I did and it started out as just take C++ and try to fix it, but then it got like further and further out of control as other issues popped in.

So, like one of the interesting issues for a lot of these companies was that they really hated the way that they got tied to particular processor architectures.  that once you made a decision to go with like an Intel Architecture machine or any other particular architecture, then you were just because of the toolchain you were tied with that architecture for life. And so, you couldn’t  for whatever business reasons would crop up, you couldn’t change your mind. And that they found deeply disturbing. So, the oak really started from IOT, or at least what was now called IOT.

 

Java 程序执行过程的两种视角

通过编译器将 Java 程序转换成该虚拟机所能识别的指令序列,称 Java 字节码 (是因为 Java 字节码指令的操作码被固定为一个字节

从虚拟机[HotSpot] 视角来看,执行 Java 代码首先需要将它编译而成的 class 文件(特定顺序结构的字节码)加载到 Java 虚拟机中,加载后的 Java 类信息会被存放于元空间中。实际运行时,虚拟机会执行其中的指令。

在运行过程中,每当调用进入一个 Java 方法,Java 虚拟机会在当前线程的 Java 方法栈中生成一个栈帧,用以存放局部变量以及字节码的操作数。这个栈帧的大小是提前计算好的,而且 Java 虚拟机不要求栈帧在内存空间里连续分布。

从硬件视角来看,Java 字节码无法直接执行。因此,Java 虚拟机需要将字节码翻译成目标机器码。在 HotSpot 里面,上述翻译过程有两种形式:第一种是解释执行,即逐条将字节码翻译成机器码并执行 [在解释执行过程中,每当为 Java 方法分配栈桢时,Java 虚拟机往往需要开辟一块额外的空间作为操作数栈,来存放计算的操作数以及返回结果];第二种是即时编译(Just-In-Time compilation,JIT),即将一个方法中包含的所有字节码编译成机器码后再执行。

前者的优势在于无需等待编译,而后者的优势在于实际运行速度更快。HotSpot 默认采用混合模式,综合了解释执行和即时编译两者的优点。它会先解释执行字节码,而后将其中反复执行的热点代码,以方法为单位进行即时编译。

为了满足不同用户场景的需要,HotSpot 内置了多个即时编译器:C1、C2 和 Graal。Graal 是 Java 10 正式引入的实验性即时编译器。HotSpot 装载了多个不同的即时编译器,以便在编译时间和生成代码的执行效率之间做取舍。其中,C1 又叫做 Client 编译器,面向的是对启动性能有要求的客户端 GUI 程序,采用的优化手段相对简单,因此编译时间较短。C2 又叫做 Server 编译器,面向的是对峰值性能有要求的服务器端程序,采用的优化手段相对复杂,因此编译时间较长,但同时生成代码的执行效率较高。

从 Java 7 开始,HotSpot 默认采用分层编译的方式:热点方法首先会被 C1 编译,而后热点方法中的热点会进一步被 C2 编译。为了不干扰应用的正常运行,HotSpot 的即时编译是放在额外的编译线程中进行的。

HotSpot 会根据 CPU 的数量设置编译线程的数目,并且按 1:2 的比例配置给 C1 及 C2 编译器。(没找到出处,评论去老师回复的)默认的分层编译应该是达到两千调C1,达到一万五调C2。

P.S. 本系列文章为学习出自郑雨迪的《深入拆解 Java 虚拟机》课程整理笔记。购买请扫描下方二维码:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值