JVM 是什么:组成、运行流程与整体架构

Java 程序能做到"一次编写,到处运行",靠的不是源代码本身跨平台,而是 JVM 把 .class 字节码屏蔽在不同操作系统和硬件之上。

一句话概括:JVM 是 Java 字节码的运行环境,它负责加载 class 文件、管理运行时内存、执行字节码,并通过垃圾回收机制自动回收不再使用的对象。

把 JVM 放到整个 Java 程序运行链路里看,会更容易理解:
#mermaid-svg-9z0i0U3xcuTT4rBG{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-9z0i0U3xcuTT4rBG .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-9z0i0U3xcuTT4rBG .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-9z0i0U3xcuTT4rBG .error-icon{fill:#552222;}#mermaid-svg-9z0i0U3xcuTT4rBG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-9z0i0U3xcuTT4rBG .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-9z0i0U3xcuTT4rBG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-9z0i0U3xcuTT4rBG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-9z0i0U3xcuTT4rBG .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-9z0i0U3xcuTT4rBG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-9z0i0U3xcuTT4rBG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-9z0i0U3xcuTT4rBG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-9z0i0U3xcuTT4rBG .marker.cross{stroke:#333333;}#mermaid-svg-9z0i0U3xcuTT4rBG svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-9z0i0U3xcuTT4rBG p{margin:0;}#mermaid-svg-9z0i0U3xcuTT4rBG .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-9z0i0U3xcuTT4rBG .cluster-label text{fill:#333;}#mermaid-svg-9z0i0U3xcuTT4rBG .cluster-label span{color:#333;}#mermaid-svg-9z0i0U3xcuTT4rBG .cluster-label span p{background-color:transparent;}#mermaid-svg-9z0i0U3xcuTT4rBG .label text,#mermaid-svg-9z0i0U3xcuTT4rBG span{fill:#333;color:#333;}#mermaid-svg-9z0i0U3xcuTT4rBG .node rect,#mermaid-svg-9z0i0U3xcuTT4rBG .node circle,#mermaid-svg-9z0i0U3xcuTT4rBG .node ellipse,#mermaid-svg-9z0i0U3xcuTT4rBG .node polygon,#mermaid-svg-9z0i0U3xcuTT4rBG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-9z0i0U3xcuTT4rBG .rough-node .label text,#mermaid-svg-9z0i0U3xcuTT4rBG .node .label text,#mermaid-svg-9z0i0U3xcuTT4rBG .image-shape .label,#mermaid-svg-9z0i0U3xcuTT4rBG .icon-shape .label{text-anchor:middle;}#mermaid-svg-9z0i0U3xcuTT4rBG .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-9z0i0U3xcuTT4rBG .rough-node .label,#mermaid-svg-9z0i0U3xcuTT4rBG .node .label,#mermaid-svg-9z0i0U3xcuTT4rBG .image-shape .label,#mermaid-svg-9z0i0U3xcuTT4rBG .icon-shape .label{text-align:center;}#mermaid-svg-9z0i0U3xcuTT4rBG .node.clickable{cursor:pointer;}#mermaid-svg-9z0i0U3xcuTT4rBG .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-9z0i0U3xcuTT4rBG .arrowheadPath{fill:#333333;}#mermaid-svg-9z0i0U3xcuTT4rBG .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-9z0i0U3xcuTT4rBG .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-9z0i0U3xcuTT4rBG .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-9z0i0U3xcuTT4rBG .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-9z0i0U3xcuTT4rBG .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-9z0i0U3xcuTT4rBG .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-9z0i0U3xcuTT4rBG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-9z0i0U3xcuTT4rBG .cluster text{fill:#333;}#mermaid-svg-9z0i0U3xcuTT4rBG .cluster span{color:#333;}#mermaid-svg-9z0i0U3xcuTT4rBG div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-9z0i0U3xcuTT4rBG .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-9z0i0U3xcuTT4rBG rect.text{fill:none;stroke-width:0;}#mermaid-svg-9z0i0U3xcuTT4rBG .icon-shape,#mermaid-svg-9z0i0U3xcuTT4rBG .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-9z0i0U3xcuTT4rBG .icon-shape p,#mermaid-svg-9z0i0U3xcuTT4rBG .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-9z0i0U3xcuTT4rBG .icon-shape .label rect,#mermaid-svg-9z0i0U3xcuTT4rBG .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-9z0i0U3xcuTT4rBG .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-9z0i0U3xcuTT4rBG .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-9z0i0U3xcuTT4rBG :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Java 源代码
javac 编译
class 字节码文件
JVM 加载并执行
操作系统指令
CPU 和内存

源代码先被编译成平台无关的字节码,不同平台安装各自对应的 JVM,再由 JVM 把字节码翻译、解释或编译成底层机器能执行的指令。

JVM 解决了什么问题

如果没有 JVM,Java 程序就需要像 C 或 C++ 一样,面向不同操作系统分别编译、适配和管理内存。JVM 把这些复杂度收到了虚拟机内部。

能力 作用
字节码执行 让 Java 程序以 .class 形式运行
跨平台 同一份字节码可以交给不同平台的 JVM 执行
自动内存管理 对象分配在堆上,由垃圾回收器自动回收
安全校验 类加载时会校验字节码是否合法
运行期优化 热点代码可以被 JIT 编译器优化成本地机器码

所以 JVM 不是一个单纯的"解释器",它更像一套完整的运行时系统。

JVM 由哪些部分组成

JVM 的核心组成可以分成四块:类加载子系统、运行时数据区、执行引擎、本地接口。
#mermaid-svg-wLFI1sWvRO5CwaGF{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-wLFI1sWvRO5CwaGF .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-wLFI1sWvRO5CwaGF .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-wLFI1sWvRO5CwaGF .error-icon{fill:#552222;}#mermaid-svg-wLFI1sWvRO5CwaGF .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-wLFI1sWvRO5CwaGF .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-wLFI1sWvRO5CwaGF .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-wLFI1sWvRO5CwaGF .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-wLFI1sWvRO5CwaGF .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-wLFI1sWvRO5CwaGF .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-wLFI1sWvRO5CwaGF .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-wLFI1sWvRO5CwaGF .marker{fill:#333333;stroke:#333333;}#mermaid-svg-wLFI1sWvRO5CwaGF .marker.cross{stroke:#333333;}#mermaid-svg-wLFI1sWvRO5CwaGF svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-wLFI1sWvRO5CwaGF p{margin:0;}#mermaid-svg-wLFI1sWvRO5CwaGF .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-wLFI1sWvRO5CwaGF .cluster-label text{fill:#333;}#mermaid-svg-wLFI1sWvRO5CwaGF .cluster-label span{color:#333;}#mermaid-svg-wLFI1sWvRO5CwaGF .cluster-label span p{background-color:transparent;}#mermaid-svg-wLFI1sWvRO5CwaGF .label text,#mermaid-svg-wLFI1sWvRO5CwaGF span{fill:#333;color:#333;}#mermaid-svg-wLFI1sWvRO5CwaGF .node rect,#mermaid-svg-wLFI1sWvRO5CwaGF .node circle,#mermaid-svg-wLFI1sWvRO5CwaGF .node ellipse,#mermaid-svg-wLFI1sWvRO5CwaGF .node polygon,#mermaid-svg-wLFI1sWvRO5CwaGF .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-wLFI1sWvRO5CwaGF .rough-node .label text,#mermaid-svg-wLFI1sWvRO5CwaGF .node .label text,#mermaid-svg-wLFI1sWvRO5CwaGF .image-shape .label,#mermaid-svg-wLFI1sWvRO5CwaGF .icon-shape .label{text-anchor:middle;}#mermaid-svg-wLFI1sWvRO5CwaGF .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-wLFI1sWvRO5CwaGF .rough-node .label,#mermaid-svg-wLFI1sWvRO5CwaGF .node .label,#mermaid-svg-wLFI1sWvRO5CwaGF .image-shape .label,#mermaid-svg-wLFI1sWvRO5CwaGF .icon-shape .label{text-align:center;}#mermaid-svg-wLFI1sWvRO5CwaGF .node.clickable{cursor:pointer;}#mermaid-svg-wLFI1sWvRO5CwaGF .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-wLFI1sWvRO5CwaGF .arrowheadPath{fill:#333333;}#mermaid-svg-wLFI1sWvRO5CwaGF .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-wLFI1sWvRO5CwaGF .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-wLFI1sWvRO5CwaGF .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wLFI1sWvRO5CwaGF .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-wLFI1sWvRO5CwaGF .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wLFI1sWvRO5CwaGF .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-wLFI1sWvRO5CwaGF .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-wLFI1sWvRO5CwaGF .cluster text{fill:#333;}#mermaid-svg-wLFI1sWvRO5CwaGF .cluster span{color:#333;}#mermaid-svg-wLFI1sWvRO5CwaGF div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-wLFI1sWvRO5CwaGF .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-wLFI1sWvRO5CwaGF rect.text{fill:none;stroke-width:0;}#mermaid-svg-wLFI1sWvRO5CwaGF .icon-shape,#mermaid-svg-wLFI1sWvRO5CwaGF .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wLFI1sWvRO5CwaGF .icon-shape p,#mermaid-svg-wLFI1sWvRO5CwaGF .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-wLFI1sWvRO5CwaGF .icon-shape .label rect,#mermaid-svg-wLFI1sWvRO5CwaGF .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wLFI1sWvRO5CwaGF .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-wLFI1sWvRO5CwaGF .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-wLFI1sWvRO5CwaGF :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 运行时数据区
程序计数器
虚拟机栈
本地方法栈

方法区
class 字节码文件
类加载子系统
运行时数据区
执行引擎
解释器
JIT 编译器
垃圾回收器
本地方法接口
本地方法库

这几部分的职责分别是:

组成部分 主要职责
类加载子系统 .class 文件加载到 JVM,并完成验证、准备、解析、初始化
运行时数据区 管理程序运行时需要的内存空间
执行引擎 执行字节码,包括解释执行和即时编译
垃圾回收器 回收堆和方法区中不再使用的对象或类元数据
本地方法接口 让 Java 调用 C、C++ 等本地方法

Java 程序的运行流程

一段 Java 代码从启动到执行,大致会经历下面这条链路:
#mermaid-svg-7A4em9nu8nlth0db{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-7A4em9nu8nlth0db .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-7A4em9nu8nlth0db .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-7A4em9nu8nlth0db .error-icon{fill:#552222;}#mermaid-svg-7A4em9nu8nlth0db .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-7A4em9nu8nlth0db .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-7A4em9nu8nlth0db .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-7A4em9nu8nlth0db .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-7A4em9nu8nlth0db .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-7A4em9nu8nlth0db .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-7A4em9nu8nlth0db .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-7A4em9nu8nlth0db .marker{fill:#333333;stroke:#333333;}#mermaid-svg-7A4em9nu8nlth0db .marker.cross{stroke:#333333;}#mermaid-svg-7A4em9nu8nlth0db svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-7A4em9nu8nlth0db p{margin:0;}#mermaid-svg-7A4em9nu8nlth0db .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-7A4em9nu8nlth0db .cluster-label text{fill:#333;}#mermaid-svg-7A4em9nu8nlth0db .cluster-label span{color:#333;}#mermaid-svg-7A4em9nu8nlth0db .cluster-label span p{background-color:transparent;}#mermaid-svg-7A4em9nu8nlth0db .label text,#mermaid-svg-7A4em9nu8nlth0db span{fill:#333;color:#333;}#mermaid-svg-7A4em9nu8nlth0db .node rect,#mermaid-svg-7A4em9nu8nlth0db .node circle,#mermaid-svg-7A4em9nu8nlth0db .node ellipse,#mermaid-svg-7A4em9nu8nlth0db .node polygon,#mermaid-svg-7A4em9nu8nlth0db .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-7A4em9nu8nlth0db .rough-node .label text,#mermaid-svg-7A4em9nu8nlth0db .node .label text,#mermaid-svg-7A4em9nu8nlth0db .image-shape .label,#mermaid-svg-7A4em9nu8nlth0db .icon-shape .label{text-anchor:middle;}#mermaid-svg-7A4em9nu8nlth0db .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-7A4em9nu8nlth0db .rough-node .label,#mermaid-svg-7A4em9nu8nlth0db .node .label,#mermaid-svg-7A4em9nu8nlth0db .image-shape .label,#mermaid-svg-7A4em9nu8nlth0db .icon-shape .label{text-align:center;}#mermaid-svg-7A4em9nu8nlth0db .node.clickable{cursor:pointer;}#mermaid-svg-7A4em9nu8nlth0db .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-7A4em9nu8nlth0db .arrowheadPath{fill:#333333;}#mermaid-svg-7A4em9nu8nlth0db .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-7A4em9nu8nlth0db .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-7A4em9nu8nlth0db .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7A4em9nu8nlth0db .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-7A4em9nu8nlth0db .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7A4em9nu8nlth0db .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-7A4em9nu8nlth0db .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-7A4em9nu8nlth0db .cluster text{fill:#333;}#mermaid-svg-7A4em9nu8nlth0db .cluster span{color:#333;}#mermaid-svg-7A4em9nu8nlth0db div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-7A4em9nu8nlth0db .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-7A4em9nu8nlth0db rect.text{fill:none;stroke-width:0;}#mermaid-svg-7A4em9nu8nlth0db .icon-shape,#mermaid-svg-7A4em9nu8nlth0db .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7A4em9nu8nlth0db .icon-shape p,#mermaid-svg-7A4em9nu8nlth0db .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-7A4em9nu8nlth0db .icon-shape .label rect,#mermaid-svg-7A4em9nu8nlth0db .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7A4em9nu8nlth0db .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-7A4em9nu8nlth0db .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-7A4em9nu8nlth0db :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 启动 Java 程序
找到入口类
类加载器加载 class
验证字节码安全性
为类变量分配内存
符号引用转直接引用
执行静态变量和静态代码块
执行 main 方法
对象在堆中分配
方法调用产生栈帧
执行引擎解释或编译字节码
垃圾回收器回收无用对象

这个流程里有两个关键点:

  1. 类不是一开始全部加载,而是通常在第一次主动使用时加载和初始化。
  2. 对象主要分配在堆上,方法调用信息主要在线程自己的虚拟机栈里。

理解了这两个点,后面再看类加载、运行时数据区和垃圾回收,就不会散。

解释执行和 JIT 编译

JVM 执行字节码有两种主要方式:

执行方式 说明 特点
解释执行 解释器逐条读取字节码并执行 启动快,但长期运行性能一般
JIT 编译 把热点代码编译成本地机器码 前期有编译成本,长期运行性能更好

很多人会把 Java 简单理解成解释型语言,但 HotSpot JVM 并不是只解释执行。实际运行中,解释器先让程序快速启动;代码执行频率变高后,JIT 编译器会把热点方法编译成本地机器码,减少重复解释的成本。
#mermaid-svg-VW1yZPKef2Wvh1ei{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-VW1yZPKef2Wvh1ei .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-VW1yZPKef2Wvh1ei .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-VW1yZPKef2Wvh1ei .error-icon{fill:#552222;}#mermaid-svg-VW1yZPKef2Wvh1ei .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-VW1yZPKef2Wvh1ei .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-VW1yZPKef2Wvh1ei .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-VW1yZPKef2Wvh1ei .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-VW1yZPKef2Wvh1ei .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-VW1yZPKef2Wvh1ei .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-VW1yZPKef2Wvh1ei .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-VW1yZPKef2Wvh1ei .marker{fill:#333333;stroke:#333333;}#mermaid-svg-VW1yZPKef2Wvh1ei .marker.cross{stroke:#333333;}#mermaid-svg-VW1yZPKef2Wvh1ei svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-VW1yZPKef2Wvh1ei p{margin:0;}#mermaid-svg-VW1yZPKef2Wvh1ei .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-VW1yZPKef2Wvh1ei .cluster-label text{fill:#333;}#mermaid-svg-VW1yZPKef2Wvh1ei .cluster-label span{color:#333;}#mermaid-svg-VW1yZPKef2Wvh1ei .cluster-label span p{background-color:transparent;}#mermaid-svg-VW1yZPKef2Wvh1ei .label text,#mermaid-svg-VW1yZPKef2Wvh1ei span{fill:#333;color:#333;}#mermaid-svg-VW1yZPKef2Wvh1ei .node rect,#mermaid-svg-VW1yZPKef2Wvh1ei .node circle,#mermaid-svg-VW1yZPKef2Wvh1ei .node ellipse,#mermaid-svg-VW1yZPKef2Wvh1ei .node polygon,#mermaid-svg-VW1yZPKef2Wvh1ei .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-VW1yZPKef2Wvh1ei .rough-node .label text,#mermaid-svg-VW1yZPKef2Wvh1ei .node .label text,#mermaid-svg-VW1yZPKef2Wvh1ei .image-shape .label,#mermaid-svg-VW1yZPKef2Wvh1ei .icon-shape .label{text-anchor:middle;}#mermaid-svg-VW1yZPKef2Wvh1ei .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-VW1yZPKef2Wvh1ei .rough-node .label,#mermaid-svg-VW1yZPKef2Wvh1ei .node .label,#mermaid-svg-VW1yZPKef2Wvh1ei .image-shape .label,#mermaid-svg-VW1yZPKef2Wvh1ei .icon-shape .label{text-align:center;}#mermaid-svg-VW1yZPKef2Wvh1ei .node.clickable{cursor:pointer;}#mermaid-svg-VW1yZPKef2Wvh1ei .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-VW1yZPKef2Wvh1ei .arrowheadPath{fill:#333333;}#mermaid-svg-VW1yZPKef2Wvh1ei .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-VW1yZPKef2Wvh1ei .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-VW1yZPKef2Wvh1ei .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-VW1yZPKef2Wvh1ei .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-VW1yZPKef2Wvh1ei .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-VW1yZPKef2Wvh1ei .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-VW1yZPKef2Wvh1ei .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-VW1yZPKef2Wvh1ei .cluster text{fill:#333;}#mermaid-svg-VW1yZPKef2Wvh1ei .cluster span{color:#333;}#mermaid-svg-VW1yZPKef2Wvh1ei div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-VW1yZPKef2Wvh1ei .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-VW1yZPKef2Wvh1ei rect.text{fill:none;stroke-width:0;}#mermaid-svg-VW1yZPKef2Wvh1ei .icon-shape,#mermaid-svg-VW1yZPKef2Wvh1ei .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-VW1yZPKef2Wvh1ei .icon-shape p,#mermaid-svg-VW1yZPKef2Wvh1ei .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-VW1yZPKef2Wvh1ei .icon-shape .label rect,#mermaid-svg-VW1yZPKef2Wvh1ei .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-VW1yZPKef2Wvh1ei .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-VW1yZPKef2Wvh1ei .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-VW1yZPKef2Wvh1ei :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 否

字节码
解释器快速执行
是否成为热点代码
JIT 编译成本地机器码
后续直接执行优化后的机器码

JVM、JRE、JDK 的区别

面试里也经常顺手问 JVM、JRE、JDK 的区别。
#mermaid-svg-XHnmOraUMMgyX3zb{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-XHnmOraUMMgyX3zb .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-XHnmOraUMMgyX3zb .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-XHnmOraUMMgyX3zb .error-icon{fill:#552222;}#mermaid-svg-XHnmOraUMMgyX3zb .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-XHnmOraUMMgyX3zb .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-XHnmOraUMMgyX3zb .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-XHnmOraUMMgyX3zb .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-XHnmOraUMMgyX3zb .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-XHnmOraUMMgyX3zb .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-XHnmOraUMMgyX3zb .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-XHnmOraUMMgyX3zb .marker{fill:#333333;stroke:#333333;}#mermaid-svg-XHnmOraUMMgyX3zb .marker.cross{stroke:#333333;}#mermaid-svg-XHnmOraUMMgyX3zb svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-XHnmOraUMMgyX3zb p{margin:0;}#mermaid-svg-XHnmOraUMMgyX3zb .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-XHnmOraUMMgyX3zb .cluster-label text{fill:#333;}#mermaid-svg-XHnmOraUMMgyX3zb .cluster-label span{color:#333;}#mermaid-svg-XHnmOraUMMgyX3zb .cluster-label span p{background-color:transparent;}#mermaid-svg-XHnmOraUMMgyX3zb .label text,#mermaid-svg-XHnmOraUMMgyX3zb span{fill:#333;color:#333;}#mermaid-svg-XHnmOraUMMgyX3zb .node rect,#mermaid-svg-XHnmOraUMMgyX3zb .node circle,#mermaid-svg-XHnmOraUMMgyX3zb .node ellipse,#mermaid-svg-XHnmOraUMMgyX3zb .node polygon,#mermaid-svg-XHnmOraUMMgyX3zb .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-XHnmOraUMMgyX3zb .rough-node .label text,#mermaid-svg-XHnmOraUMMgyX3zb .node .label text,#mermaid-svg-XHnmOraUMMgyX3zb .image-shape .label,#mermaid-svg-XHnmOraUMMgyX3zb .icon-shape .label{text-anchor:middle;}#mermaid-svg-XHnmOraUMMgyX3zb .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-XHnmOraUMMgyX3zb .rough-node .label,#mermaid-svg-XHnmOraUMMgyX3zb .node .label,#mermaid-svg-XHnmOraUMMgyX3zb .image-shape .label,#mermaid-svg-XHnmOraUMMgyX3zb .icon-shape .label{text-align:center;}#mermaid-svg-XHnmOraUMMgyX3zb .node.clickable{cursor:pointer;}#mermaid-svg-XHnmOraUMMgyX3zb .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-XHnmOraUMMgyX3zb .arrowheadPath{fill:#333333;}#mermaid-svg-XHnmOraUMMgyX3zb .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-XHnmOraUMMgyX3zb .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-XHnmOraUMMgyX3zb .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-XHnmOraUMMgyX3zb .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-XHnmOraUMMgyX3zb .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-XHnmOraUMMgyX3zb .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-XHnmOraUMMgyX3zb .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-XHnmOraUMMgyX3zb .cluster text{fill:#333;}#mermaid-svg-XHnmOraUMMgyX3zb .cluster span{color:#333;}#mermaid-svg-XHnmOraUMMgyX3zb div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-XHnmOraUMMgyX3zb .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-XHnmOraUMMgyX3zb rect.text{fill:none;stroke-width:0;}#mermaid-svg-XHnmOraUMMgyX3zb .icon-shape,#mermaid-svg-XHnmOraUMMgyX3zb .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-XHnmOraUMMgyX3zb .icon-shape p,#mermaid-svg-XHnmOraUMMgyX3zb .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-XHnmOraUMMgyX3zb .icon-shape .label rect,#mermaid-svg-XHnmOraUMMgyX3zb .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-XHnmOraUMMgyX3zb .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-XHnmOraUMMgyX3zb .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-XHnmOraUMMgyX3zb :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} JDK
JRE
JVM
Java 核心类库
javac 等开发工具
jmap jstack jstat 等诊断工具

名称 含义 面向谁
JVM Java Virtual Machine,执行字节码的虚拟机 程序运行
JRE JVM 加核心类库,能运行 Java 程序 运行环境
JDK JRE 加开发、编译、诊断工具 开发者

如果只是运行 Java 程序,JRE 理论上就够;如果要编译、打包、排查问题,需要 JDK。

面试回答模板

可以这样回答:

JVM 是 Java 字节码的运行环境,负责把 .class 文件加载到内存中并执行。它主要由类加载子系统、运行时数据区、执行引擎、本地方法接口和垃圾回收器组成。Java 源码先通过 javac 编译成字节码,类加载器把字节码加载进 JVM,运行时数据区负责分配堆、栈、方法区等内存,执行引擎通过解释器和 JIT 编译器执行字节码,垃圾回收器负责回收不再使用的对象。JVM 的价值主要是跨平台、自动内存管理和运行期优化。

小结

JVM 这块不要一上来背一堆名词。先抓住一条主线:

Java 源码编译成字节码,JVM 加载字节码,把运行时数据放进不同内存区域,再由执行引擎执行,并由垃圾回收器自动管理对象生命周期。

相关推荐
hoiii1871 小时前
基于 STM32 的标准遥控器架构与源码
stm32·嵌入式硬件·架构
Gopher_HBo1 小时前
存储层LSM Tree
后端·架构
步步为营DotNet1 小时前
.NET Aspire 在云原生微服务架构中的深度实践与剖析
云原生·架构·.net
江华森1 小时前
《网络架构实战:从单机到云原生的全栈思考》博客系列
网络·云原生·架构
真实的菜1 小时前
Redis 从入门到精通(六):集群模式(Cluster)—— 分布式架构、哈希槽与 Gossip 协议全解
redis·分布式·架构
喵了几个咪1 小时前
技术复盘:基于 GoWind Admin 实现 Kratos 框架单体轻量化落地
前端·架构
light blue bird1 小时前
3C 数码电子BOM 协同工作台组件
java·开发语言·jvm·windows·.net·桌面端
故渊at10 小时前
系列三:组件化与模块化进阶 | 第11篇 组件化项目规范与问题根治:依赖、资源、Manifest 与混淆的全链路管控
android·架构·mvvm·模块化·组件化
goodluckyaa12 小时前
NVIDIAGPU 架构中的不变常量(宏观 → 微观)
架构·gpu算力