垃圾回收算法是思想,垃圾回收器是实现。面试里问"有哪些垃圾回收器",不是让你背一串名字,而是看你能不能说清楚:不同收集器分别追求什么目标,适合什么场景,G1 为什么成为现代 JVM 的重要默认选择。
一句话概括:Serial 追求简单,Parallel 追求吞吐量,CMS 追求低停顿,G1 通过 Region 化和可预测停顿在吞吐量与延迟之间做平衡。
常见垃圾回收器全景
#mermaid-svg-TotBysr7yMeFAzEC{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-TotBysr7yMeFAzEC .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-TotBysr7yMeFAzEC .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-TotBysr7yMeFAzEC .error-icon{fill:#552222;}#mermaid-svg-TotBysr7yMeFAzEC .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-TotBysr7yMeFAzEC .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-TotBysr7yMeFAzEC .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-TotBysr7yMeFAzEC .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-TotBysr7yMeFAzEC .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-TotBysr7yMeFAzEC .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-TotBysr7yMeFAzEC .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-TotBysr7yMeFAzEC .marker{fill:#333333;stroke:#333333;}#mermaid-svg-TotBysr7yMeFAzEC .marker.cross{stroke:#333333;}#mermaid-svg-TotBysr7yMeFAzEC svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-TotBysr7yMeFAzEC p{margin:0;}#mermaid-svg-TotBysr7yMeFAzEC .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-TotBysr7yMeFAzEC .cluster-label text{fill:#333;}#mermaid-svg-TotBysr7yMeFAzEC .cluster-label span{color:#333;}#mermaid-svg-TotBysr7yMeFAzEC .cluster-label span p{background-color:transparent;}#mermaid-svg-TotBysr7yMeFAzEC .label text,#mermaid-svg-TotBysr7yMeFAzEC span{fill:#333;color:#333;}#mermaid-svg-TotBysr7yMeFAzEC .node rect,#mermaid-svg-TotBysr7yMeFAzEC .node circle,#mermaid-svg-TotBysr7yMeFAzEC .node ellipse,#mermaid-svg-TotBysr7yMeFAzEC .node polygon,#mermaid-svg-TotBysr7yMeFAzEC .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-TotBysr7yMeFAzEC .rough-node .label text,#mermaid-svg-TotBysr7yMeFAzEC .node .label text,#mermaid-svg-TotBysr7yMeFAzEC .image-shape .label,#mermaid-svg-TotBysr7yMeFAzEC .icon-shape .label{text-anchor:middle;}#mermaid-svg-TotBysr7yMeFAzEC .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-TotBysr7yMeFAzEC .rough-node .label,#mermaid-svg-TotBysr7yMeFAzEC .node .label,#mermaid-svg-TotBysr7yMeFAzEC .image-shape .label,#mermaid-svg-TotBysr7yMeFAzEC .icon-shape .label{text-align:center;}#mermaid-svg-TotBysr7yMeFAzEC .node.clickable{cursor:pointer;}#mermaid-svg-TotBysr7yMeFAzEC .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-TotBysr7yMeFAzEC .arrowheadPath{fill:#333333;}#mermaid-svg-TotBysr7yMeFAzEC .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-TotBysr7yMeFAzEC .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-TotBysr7yMeFAzEC .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-TotBysr7yMeFAzEC .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-TotBysr7yMeFAzEC .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-TotBysr7yMeFAzEC .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-TotBysr7yMeFAzEC .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-TotBysr7yMeFAzEC .cluster text{fill:#333;}#mermaid-svg-TotBysr7yMeFAzEC .cluster span{color:#333;}#mermaid-svg-TotBysr7yMeFAzEC 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-TotBysr7yMeFAzEC .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-TotBysr7yMeFAzEC rect.text{fill:none;stroke-width:0;}#mermaid-svg-TotBysr7yMeFAzEC .icon-shape,#mermaid-svg-TotBysr7yMeFAzEC .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-TotBysr7yMeFAzEC .icon-shape p,#mermaid-svg-TotBysr7yMeFAzEC .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-TotBysr7yMeFAzEC .icon-shape .label rect,#mermaid-svg-TotBysr7yMeFAzEC .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-TotBysr7yMeFAzEC .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-TotBysr7yMeFAzEC .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-TotBysr7yMeFAzEC :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 垃圾回收器
Serial 系列
Parallel 系列
CMS 系列
G1
Serial
Serial Old
Parallel Scavenge
Parallel Old
ParNew
CMS
| 收集器 | 主要作用区域 | 核心特点 | 适合场景 |
|---|---|---|---|
| Serial | 新生代 | 单线程,复制算法,STW | 小堆、客户端程序 |
| Serial Old | 老年代 | 单线程,标记整理,STW | 小堆或兜底 |
| Parallel Scavenge | 新生代 | 多线程,关注吞吐量 | 后台计算、批处理 |
| Parallel Old | 老年代 | 多线程,标记整理 | 吞吐量优先 |
| ParNew | 新生代 | 多线程,常与 CMS 搭配 | 低停顿老年代方案的一部分 |
| CMS | 老年代 | 并发标记清除,低停顿 | 对响应时间敏感的老系统 |
| G1 | 新生代和老年代 | Region 化,可预测停顿 | 大堆、服务端应用 |
JDK 8 在服务端模式下常见默认选择是 Parallel GC;JDK 9 之后 G1 成为默认垃圾回收器。CMS 已经在较新的 JDK 中退出历史舞台,维护新系统时更常关注 G1、ZGC、Shenandoah 等方案。
并行和并发不要混
垃圾回收器里经常出现两个词:并行和并发。它们不是一回事。
| 概念 | 关注点 | 例子 |
|---|---|---|
| 并行 | 多个 GC 线程同时干活 | Parallel GC 多个线程一起回收 |
| 并发 | GC 线程和应用线程同时运行 | CMS 或 G1 的并发标记阶段 |
#mermaid-svg-8HgAj8bSYGHh4Wiy{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-8HgAj8bSYGHh4Wiy .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-8HgAj8bSYGHh4Wiy .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-8HgAj8bSYGHh4Wiy .error-icon{fill:#552222;}#mermaid-svg-8HgAj8bSYGHh4Wiy .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-8HgAj8bSYGHh4Wiy .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-8HgAj8bSYGHh4Wiy .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-8HgAj8bSYGHh4Wiy .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-8HgAj8bSYGHh4Wiy .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-8HgAj8bSYGHh4Wiy .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-8HgAj8bSYGHh4Wiy .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-8HgAj8bSYGHh4Wiy .marker{fill:#333333;stroke:#333333;}#mermaid-svg-8HgAj8bSYGHh4Wiy .marker.cross{stroke:#333333;}#mermaid-svg-8HgAj8bSYGHh4Wiy svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-8HgAj8bSYGHh4Wiy p{margin:0;}#mermaid-svg-8HgAj8bSYGHh4Wiy .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-8HgAj8bSYGHh4Wiy .cluster-label text{fill:#333;}#mermaid-svg-8HgAj8bSYGHh4Wiy .cluster-label span{color:#333;}#mermaid-svg-8HgAj8bSYGHh4Wiy .cluster-label span p{background-color:transparent;}#mermaid-svg-8HgAj8bSYGHh4Wiy .label text,#mermaid-svg-8HgAj8bSYGHh4Wiy span{fill:#333;color:#333;}#mermaid-svg-8HgAj8bSYGHh4Wiy .node rect,#mermaid-svg-8HgAj8bSYGHh4Wiy .node circle,#mermaid-svg-8HgAj8bSYGHh4Wiy .node ellipse,#mermaid-svg-8HgAj8bSYGHh4Wiy .node polygon,#mermaid-svg-8HgAj8bSYGHh4Wiy .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-8HgAj8bSYGHh4Wiy .rough-node .label text,#mermaid-svg-8HgAj8bSYGHh4Wiy .node .label text,#mermaid-svg-8HgAj8bSYGHh4Wiy .image-shape .label,#mermaid-svg-8HgAj8bSYGHh4Wiy .icon-shape .label{text-anchor:middle;}#mermaid-svg-8HgAj8bSYGHh4Wiy .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-8HgAj8bSYGHh4Wiy .rough-node .label,#mermaid-svg-8HgAj8bSYGHh4Wiy .node .label,#mermaid-svg-8HgAj8bSYGHh4Wiy .image-shape .label,#mermaid-svg-8HgAj8bSYGHh4Wiy .icon-shape .label{text-align:center;}#mermaid-svg-8HgAj8bSYGHh4Wiy .node.clickable{cursor:pointer;}#mermaid-svg-8HgAj8bSYGHh4Wiy .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-8HgAj8bSYGHh4Wiy .arrowheadPath{fill:#333333;}#mermaid-svg-8HgAj8bSYGHh4Wiy .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-8HgAj8bSYGHh4Wiy .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-8HgAj8bSYGHh4Wiy .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-8HgAj8bSYGHh4Wiy .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-8HgAj8bSYGHh4Wiy .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-8HgAj8bSYGHh4Wiy .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-8HgAj8bSYGHh4Wiy .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-8HgAj8bSYGHh4Wiy .cluster text{fill:#333;}#mermaid-svg-8HgAj8bSYGHh4Wiy .cluster span{color:#333;}#mermaid-svg-8HgAj8bSYGHh4Wiy 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-8HgAj8bSYGHh4Wiy .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-8HgAj8bSYGHh4Wiy rect.text{fill:none;stroke-width:0;}#mermaid-svg-8HgAj8bSYGHh4Wiy .icon-shape,#mermaid-svg-8HgAj8bSYGHh4Wiy .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-8HgAj8bSYGHh4Wiy .icon-shape p,#mermaid-svg-8HgAj8bSYGHh4Wiy .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-8HgAj8bSYGHh4Wiy .icon-shape .label rect,#mermaid-svg-8HgAj8bSYGHh4Wiy .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-8HgAj8bSYGHh4Wiy .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-8HgAj8bSYGHh4Wiy .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-8HgAj8bSYGHh4Wiy :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 并行
应用线程暂停
多个 GC 线程一起回收
并发
应用线程继续运行
GC 线程同时做部分标记或清理
所以 Parallel 的重点是"多个 GC 线程一起工作",但它仍然会 STW。CMS 和 G1 的一些阶段可以和应用线程并发执行,但也不是全程无停顿,初始标记、重新标记、对象复制等阶段仍然可能暂停应用线程。
Serial 收集器
Serial 是最简单的垃圾回收器。它使用单个 GC 线程完成回收,回收时所有应用线程暂停。
"Serial GC 线程" "应用线程" "Serial GC 线程" "应用线程" #mermaid-svg-xWuf8lYFetVAsF0i{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-xWuf8lYFetVAsF0i .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-xWuf8lYFetVAsF0i .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-xWuf8lYFetVAsF0i .error-icon{fill:#552222;}#mermaid-svg-xWuf8lYFetVAsF0i .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-xWuf8lYFetVAsF0i .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-xWuf8lYFetVAsF0i .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-xWuf8lYFetVAsF0i .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-xWuf8lYFetVAsF0i .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-xWuf8lYFetVAsF0i .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-xWuf8lYFetVAsF0i .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-xWuf8lYFetVAsF0i .marker{fill:#333333;stroke:#333333;}#mermaid-svg-xWuf8lYFetVAsF0i .marker.cross{stroke:#333333;}#mermaid-svg-xWuf8lYFetVAsF0i svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-xWuf8lYFetVAsF0i p{margin:0;}#mermaid-svg-xWuf8lYFetVAsF0i .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-xWuf8lYFetVAsF0i text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-xWuf8lYFetVAsF0i .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-xWuf8lYFetVAsF0i .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-xWuf8lYFetVAsF0i .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-xWuf8lYFetVAsF0i .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-xWuf8lYFetVAsF0i #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-xWuf8lYFetVAsF0i .sequenceNumber{fill:white;}#mermaid-svg-xWuf8lYFetVAsF0i #sequencenumber{fill:#333;}#mermaid-svg-xWuf8lYFetVAsF0i #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-xWuf8lYFetVAsF0i .messageText{fill:#333;stroke:none;}#mermaid-svg-xWuf8lYFetVAsF0i .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-xWuf8lYFetVAsF0i .labelText,#mermaid-svg-xWuf8lYFetVAsF0i .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-xWuf8lYFetVAsF0i .loopText,#mermaid-svg-xWuf8lYFetVAsF0i .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-xWuf8lYFetVAsF0i .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-xWuf8lYFetVAsF0i .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-xWuf8lYFetVAsF0i .noteText,#mermaid-svg-xWuf8lYFetVAsF0i .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-xWuf8lYFetVAsF0i .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-xWuf8lYFetVAsF0i .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-xWuf8lYFetVAsF0i .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-xWuf8lYFetVAsF0i .actorPopupMenu{position:absolute;}#mermaid-svg-xWuf8lYFetVAsF0i .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-xWuf8lYFetVAsF0i .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-xWuf8lYFetVAsF0i .actor-man circle,#mermaid-svg-xWuf8lYFetVAsF0i line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-xWuf8lYFetVAsF0i :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} "正常运行""STW 暂停""单线程回收""恢复运行"
它的优势是简单、额外开销小;缺点是停顿明显,不适合大堆和高并发服务端应用。
Parallel 收集器
Parallel 系列使用多条 GC 线程并行回收,目标是提高吞吐量。
吞吐量可以简单理解为:
text
应用运行时间 / (应用运行时间 + GC 时间)
Parallel 适合后台任务、批处理、计算型任务等"更关心总体处理效率,而不是单次停顿极短"的场景。
"GC 线程 3" "GC 线程 2" "GC 线程 1" "应用线程" "GC 线程 3" "GC 线程 2" "GC 线程 1" "应用线程" #mermaid-svg-0R9Lk2K2lh7lHBFr{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-0R9Lk2K2lh7lHBFr .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-0R9Lk2K2lh7lHBFr .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-0R9Lk2K2lh7lHBFr .error-icon{fill:#552222;}#mermaid-svg-0R9Lk2K2lh7lHBFr .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-0R9Lk2K2lh7lHBFr .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-0R9Lk2K2lh7lHBFr .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-0R9Lk2K2lh7lHBFr .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-0R9Lk2K2lh7lHBFr .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-0R9Lk2K2lh7lHBFr .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-0R9Lk2K2lh7lHBFr .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-0R9Lk2K2lh7lHBFr .marker{fill:#333333;stroke:#333333;}#mermaid-svg-0R9Lk2K2lh7lHBFr .marker.cross{stroke:#333333;}#mermaid-svg-0R9Lk2K2lh7lHBFr svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-0R9Lk2K2lh7lHBFr p{margin:0;}#mermaid-svg-0R9Lk2K2lh7lHBFr .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-0R9Lk2K2lh7lHBFr text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-0R9Lk2K2lh7lHBFr .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-0R9Lk2K2lh7lHBFr .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-0R9Lk2K2lh7lHBFr .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-0R9Lk2K2lh7lHBFr .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-0R9Lk2K2lh7lHBFr #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-0R9Lk2K2lh7lHBFr .sequenceNumber{fill:white;}#mermaid-svg-0R9Lk2K2lh7lHBFr #sequencenumber{fill:#333;}#mermaid-svg-0R9Lk2K2lh7lHBFr #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-0R9Lk2K2lh7lHBFr .messageText{fill:#333;stroke:none;}#mermaid-svg-0R9Lk2K2lh7lHBFr .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-0R9Lk2K2lh7lHBFr .labelText,#mermaid-svg-0R9Lk2K2lh7lHBFr .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-0R9Lk2K2lh7lHBFr .loopText,#mermaid-svg-0R9Lk2K2lh7lHBFr .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-0R9Lk2K2lh7lHBFr .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-0R9Lk2K2lh7lHBFr .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-0R9Lk2K2lh7lHBFr .noteText,#mermaid-svg-0R9Lk2K2lh7lHBFr .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-0R9Lk2K2lh7lHBFr .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-0R9Lk2K2lh7lHBFr .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-0R9Lk2K2lh7lHBFr .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-0R9Lk2K2lh7lHBFr .actorPopupMenu{position:absolute;}#mermaid-svg-0R9Lk2K2lh7lHBFr .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-0R9Lk2K2lh7lHBFr .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-0R9Lk2K2lh7lHBFr .actor-man circle,#mermaid-svg-0R9Lk2K2lh7lHBFr line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-0R9Lk2K2lh7lHBFr :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} "正常运行""STW 暂停""并行处理部分对象""并行处理部分对象""并行处理部分对象""恢复运行"
Parallel 的关键词是"并行"和"吞吐量",不是"完全无停顿"。它仍然会 Stop The World,只是回收阶段可以用多线程缩短总回收时间。
CMS 收集器
CMS 全称 Concurrent Mark Sweep,目标是降低老年代垃圾回收停顿时间。
它的核心流程包括初始标记、并发标记、重新标记、并发清除。
#mermaid-svg-6eEvd6OvCR0qA3aj{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-6eEvd6OvCR0qA3aj .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-6eEvd6OvCR0qA3aj .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-6eEvd6OvCR0qA3aj .error-icon{fill:#552222;}#mermaid-svg-6eEvd6OvCR0qA3aj .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-6eEvd6OvCR0qA3aj .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-6eEvd6OvCR0qA3aj .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-6eEvd6OvCR0qA3aj .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-6eEvd6OvCR0qA3aj .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-6eEvd6OvCR0qA3aj .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-6eEvd6OvCR0qA3aj .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-6eEvd6OvCR0qA3aj .marker{fill:#333333;stroke:#333333;}#mermaid-svg-6eEvd6OvCR0qA3aj .marker.cross{stroke:#333333;}#mermaid-svg-6eEvd6OvCR0qA3aj svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-6eEvd6OvCR0qA3aj p{margin:0;}#mermaid-svg-6eEvd6OvCR0qA3aj .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-6eEvd6OvCR0qA3aj .cluster-label text{fill:#333;}#mermaid-svg-6eEvd6OvCR0qA3aj .cluster-label span{color:#333;}#mermaid-svg-6eEvd6OvCR0qA3aj .cluster-label span p{background-color:transparent;}#mermaid-svg-6eEvd6OvCR0qA3aj .label text,#mermaid-svg-6eEvd6OvCR0qA3aj span{fill:#333;color:#333;}#mermaid-svg-6eEvd6OvCR0qA3aj .node rect,#mermaid-svg-6eEvd6OvCR0qA3aj .node circle,#mermaid-svg-6eEvd6OvCR0qA3aj .node ellipse,#mermaid-svg-6eEvd6OvCR0qA3aj .node polygon,#mermaid-svg-6eEvd6OvCR0qA3aj .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-6eEvd6OvCR0qA3aj .rough-node .label text,#mermaid-svg-6eEvd6OvCR0qA3aj .node .label text,#mermaid-svg-6eEvd6OvCR0qA3aj .image-shape .label,#mermaid-svg-6eEvd6OvCR0qA3aj .icon-shape .label{text-anchor:middle;}#mermaid-svg-6eEvd6OvCR0qA3aj .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-6eEvd6OvCR0qA3aj .rough-node .label,#mermaid-svg-6eEvd6OvCR0qA3aj .node .label,#mermaid-svg-6eEvd6OvCR0qA3aj .image-shape .label,#mermaid-svg-6eEvd6OvCR0qA3aj .icon-shape .label{text-align:center;}#mermaid-svg-6eEvd6OvCR0qA3aj .node.clickable{cursor:pointer;}#mermaid-svg-6eEvd6OvCR0qA3aj .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-6eEvd6OvCR0qA3aj .arrowheadPath{fill:#333333;}#mermaid-svg-6eEvd6OvCR0qA3aj .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-6eEvd6OvCR0qA3aj .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-6eEvd6OvCR0qA3aj .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-6eEvd6OvCR0qA3aj .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-6eEvd6OvCR0qA3aj .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-6eEvd6OvCR0qA3aj .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-6eEvd6OvCR0qA3aj .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-6eEvd6OvCR0qA3aj .cluster text{fill:#333;}#mermaid-svg-6eEvd6OvCR0qA3aj .cluster span{color:#333;}#mermaid-svg-6eEvd6OvCR0qA3aj 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-6eEvd6OvCR0qA3aj .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-6eEvd6OvCR0qA3aj rect.text{fill:none;stroke-width:0;}#mermaid-svg-6eEvd6OvCR0qA3aj .icon-shape,#mermaid-svg-6eEvd6OvCR0qA3aj .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-6eEvd6OvCR0qA3aj .icon-shape p,#mermaid-svg-6eEvd6OvCR0qA3aj .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-6eEvd6OvCR0qA3aj .icon-shape .label rect,#mermaid-svg-6eEvd6OvCR0qA3aj .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-6eEvd6OvCR0qA3aj .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-6eEvd6OvCR0qA3aj .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-6eEvd6OvCR0qA3aj :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 初始标记
并发标记
重新标记
并发清除
| 阶段 | 是否 STW | 说明 |
|---|---|---|
| 初始标记 | 是 | 标记 GC Roots 直接关联对象 |
| 并发标记 | 否 | 和应用线程并发执行,继续追踪引用链 |
| 重新标记 | 是 | 修正并发期间引用变化导致的标记偏差 |
| 并发清除 | 否 | 清理不可达对象 |
CMS 的优点是停顿时间短;缺点是使用标记清除算法,会产生内存碎片,并且并发阶段会和应用线程抢 CPU。如果并发清理赶不上对象分配速度,还可能触发更重的 Full GC。
现在新项目一般不再优先选择 CMS,但理解 CMS 有助于理解"低停顿收集器为什么要把部分阶段并发化"。
G1 的核心思想
G1 全称 Garbage First。它不再把堆简单看成连续的新生代和老年代,而是把堆切成多个大小相等的 Region。每个 Region 可以根据需要扮演 Eden、Survivor、Old 或 Humongous。
#mermaid-svg-0W6LVNyO6QyxyBgv{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-0W6LVNyO6QyxyBgv .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-0W6LVNyO6QyxyBgv .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-0W6LVNyO6QyxyBgv .error-icon{fill:#552222;}#mermaid-svg-0W6LVNyO6QyxyBgv .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-0W6LVNyO6QyxyBgv .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-0W6LVNyO6QyxyBgv .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-0W6LVNyO6QyxyBgv .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-0W6LVNyO6QyxyBgv .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-0W6LVNyO6QyxyBgv .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-0W6LVNyO6QyxyBgv .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-0W6LVNyO6QyxyBgv .marker{fill:#333333;stroke:#333333;}#mermaid-svg-0W6LVNyO6QyxyBgv .marker.cross{stroke:#333333;}#mermaid-svg-0W6LVNyO6QyxyBgv svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-0W6LVNyO6QyxyBgv p{margin:0;}#mermaid-svg-0W6LVNyO6QyxyBgv .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-0W6LVNyO6QyxyBgv .cluster-label text{fill:#333;}#mermaid-svg-0W6LVNyO6QyxyBgv .cluster-label span{color:#333;}#mermaid-svg-0W6LVNyO6QyxyBgv .cluster-label span p{background-color:transparent;}#mermaid-svg-0W6LVNyO6QyxyBgv .label text,#mermaid-svg-0W6LVNyO6QyxyBgv span{fill:#333;color:#333;}#mermaid-svg-0W6LVNyO6QyxyBgv .node rect,#mermaid-svg-0W6LVNyO6QyxyBgv .node circle,#mermaid-svg-0W6LVNyO6QyxyBgv .node ellipse,#mermaid-svg-0W6LVNyO6QyxyBgv .node polygon,#mermaid-svg-0W6LVNyO6QyxyBgv .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-0W6LVNyO6QyxyBgv .rough-node .label text,#mermaid-svg-0W6LVNyO6QyxyBgv .node .label text,#mermaid-svg-0W6LVNyO6QyxyBgv .image-shape .label,#mermaid-svg-0W6LVNyO6QyxyBgv .icon-shape .label{text-anchor:middle;}#mermaid-svg-0W6LVNyO6QyxyBgv .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-0W6LVNyO6QyxyBgv .rough-node .label,#mermaid-svg-0W6LVNyO6QyxyBgv .node .label,#mermaid-svg-0W6LVNyO6QyxyBgv .image-shape .label,#mermaid-svg-0W6LVNyO6QyxyBgv .icon-shape .label{text-align:center;}#mermaid-svg-0W6LVNyO6QyxyBgv .node.clickable{cursor:pointer;}#mermaid-svg-0W6LVNyO6QyxyBgv .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-0W6LVNyO6QyxyBgv .arrowheadPath{fill:#333333;}#mermaid-svg-0W6LVNyO6QyxyBgv .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-0W6LVNyO6QyxyBgv .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-0W6LVNyO6QyxyBgv .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-0W6LVNyO6QyxyBgv .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-0W6LVNyO6QyxyBgv .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-0W6LVNyO6QyxyBgv .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-0W6LVNyO6QyxyBgv .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-0W6LVNyO6QyxyBgv .cluster text{fill:#333;}#mermaid-svg-0W6LVNyO6QyxyBgv .cluster span{color:#333;}#mermaid-svg-0W6LVNyO6QyxyBgv 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-0W6LVNyO6QyxyBgv .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-0W6LVNyO6QyxyBgv rect.text{fill:none;stroke-width:0;}#mermaid-svg-0W6LVNyO6QyxyBgv .icon-shape,#mermaid-svg-0W6LVNyO6QyxyBgv .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-0W6LVNyO6QyxyBgv .icon-shape p,#mermaid-svg-0W6LVNyO6QyxyBgv .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-0W6LVNyO6QyxyBgv .icon-shape .label rect,#mermaid-svg-0W6LVNyO6QyxyBgv .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-0W6LVNyO6QyxyBgv .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-0W6LVNyO6QyxyBgv .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-0W6LVNyO6QyxyBgv :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Java 堆
Region 1
Region 2
Region 3
Region 4
Region 5
Eden
Survivor
Old
Humongous
Free
G1 的关键点有三个:
- 堆被拆成多个 Region,而不是只按物理连续区域划分新生代和老年代。
- G1 会估算每个 Region 的回收价值,优先回收垃圾最多、收益最高的 Region。
- 可以通过停顿时间目标,让 JVM 尽量在指定时间内选择合适数量的 Region 回收。
Humongous Region 用来存放大对象。通常对象大小超过 Region 的一半时,就可能被当作大对象处理。
G1 的回收过程
G1 常见回收过程可以分为 Young GC、并发标记、Mixed GC。
#mermaid-svg-XlFTPABtlU088UKA{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-XlFTPABtlU088UKA .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-XlFTPABtlU088UKA .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-XlFTPABtlU088UKA .error-icon{fill:#552222;}#mermaid-svg-XlFTPABtlU088UKA .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-XlFTPABtlU088UKA .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-XlFTPABtlU088UKA .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-XlFTPABtlU088UKA .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-XlFTPABtlU088UKA .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-XlFTPABtlU088UKA .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-XlFTPABtlU088UKA .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-XlFTPABtlU088UKA .marker{fill:#333333;stroke:#333333;}#mermaid-svg-XlFTPABtlU088UKA .marker.cross{stroke:#333333;}#mermaid-svg-XlFTPABtlU088UKA svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-XlFTPABtlU088UKA p{margin:0;}#mermaid-svg-XlFTPABtlU088UKA .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-XlFTPABtlU088UKA .cluster-label text{fill:#333;}#mermaid-svg-XlFTPABtlU088UKA .cluster-label span{color:#333;}#mermaid-svg-XlFTPABtlU088UKA .cluster-label span p{background-color:transparent;}#mermaid-svg-XlFTPABtlU088UKA .label text,#mermaid-svg-XlFTPABtlU088UKA span{fill:#333;color:#333;}#mermaid-svg-XlFTPABtlU088UKA .node rect,#mermaid-svg-XlFTPABtlU088UKA .node circle,#mermaid-svg-XlFTPABtlU088UKA .node ellipse,#mermaid-svg-XlFTPABtlU088UKA .node polygon,#mermaid-svg-XlFTPABtlU088UKA .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-XlFTPABtlU088UKA .rough-node .label text,#mermaid-svg-XlFTPABtlU088UKA .node .label text,#mermaid-svg-XlFTPABtlU088UKA .image-shape .label,#mermaid-svg-XlFTPABtlU088UKA .icon-shape .label{text-anchor:middle;}#mermaid-svg-XlFTPABtlU088UKA .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-XlFTPABtlU088UKA .rough-node .label,#mermaid-svg-XlFTPABtlU088UKA .node .label,#mermaid-svg-XlFTPABtlU088UKA .image-shape .label,#mermaid-svg-XlFTPABtlU088UKA .icon-shape .label{text-align:center;}#mermaid-svg-XlFTPABtlU088UKA .node.clickable{cursor:pointer;}#mermaid-svg-XlFTPABtlU088UKA .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-XlFTPABtlU088UKA .arrowheadPath{fill:#333333;}#mermaid-svg-XlFTPABtlU088UKA .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-XlFTPABtlU088UKA .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-XlFTPABtlU088UKA .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-XlFTPABtlU088UKA .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-XlFTPABtlU088UKA .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-XlFTPABtlU088UKA .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-XlFTPABtlU088UKA .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-XlFTPABtlU088UKA .cluster text{fill:#333;}#mermaid-svg-XlFTPABtlU088UKA .cluster span{color:#333;}#mermaid-svg-XlFTPABtlU088UKA 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-XlFTPABtlU088UKA .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-XlFTPABtlU088UKA rect.text{fill:none;stroke-width:0;}#mermaid-svg-XlFTPABtlU088UKA .icon-shape,#mermaid-svg-XlFTPABtlU088UKA .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-XlFTPABtlU088UKA .icon-shape p,#mermaid-svg-XlFTPABtlU088UKA .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-XlFTPABtlU088UKA .icon-shape .label rect,#mermaid-svg-XlFTPABtlU088UKA .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-XlFTPABtlU088UKA .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-XlFTPABtlU088UKA .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-XlFTPABtlU088UKA :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 是
否
是
对象分配到 Eden Region
Eden 是否不足
Young GC
存活对象复制到 Survivor 或 Old
老年代占用是否达到阈值
并发标记
重新标记
统计 Region 回收价值
Mixed GC
回收 Eden、Survivor 和部分 Old Region
Young GC
Young GC 主要回收 Eden Region。存活对象会被复制到 Survivor Region,年龄较大或空间不足时进入 Old Region。
#mermaid-svg-3hguKUVcisCC2UmH{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-3hguKUVcisCC2UmH .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-3hguKUVcisCC2UmH .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-3hguKUVcisCC2UmH .error-icon{fill:#552222;}#mermaid-svg-3hguKUVcisCC2UmH .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-3hguKUVcisCC2UmH .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-3hguKUVcisCC2UmH .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-3hguKUVcisCC2UmH .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-3hguKUVcisCC2UmH .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-3hguKUVcisCC2UmH .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-3hguKUVcisCC2UmH .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-3hguKUVcisCC2UmH .marker{fill:#333333;stroke:#333333;}#mermaid-svg-3hguKUVcisCC2UmH .marker.cross{stroke:#333333;}#mermaid-svg-3hguKUVcisCC2UmH svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-3hguKUVcisCC2UmH p{margin:0;}#mermaid-svg-3hguKUVcisCC2UmH .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-3hguKUVcisCC2UmH .cluster-label text{fill:#333;}#mermaid-svg-3hguKUVcisCC2UmH .cluster-label span{color:#333;}#mermaid-svg-3hguKUVcisCC2UmH .cluster-label span p{background-color:transparent;}#mermaid-svg-3hguKUVcisCC2UmH .label text,#mermaid-svg-3hguKUVcisCC2UmH span{fill:#333;color:#333;}#mermaid-svg-3hguKUVcisCC2UmH .node rect,#mermaid-svg-3hguKUVcisCC2UmH .node circle,#mermaid-svg-3hguKUVcisCC2UmH .node ellipse,#mermaid-svg-3hguKUVcisCC2UmH .node polygon,#mermaid-svg-3hguKUVcisCC2UmH .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-3hguKUVcisCC2UmH .rough-node .label text,#mermaid-svg-3hguKUVcisCC2UmH .node .label text,#mermaid-svg-3hguKUVcisCC2UmH .image-shape .label,#mermaid-svg-3hguKUVcisCC2UmH .icon-shape .label{text-anchor:middle;}#mermaid-svg-3hguKUVcisCC2UmH .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-3hguKUVcisCC2UmH .rough-node .label,#mermaid-svg-3hguKUVcisCC2UmH .node .label,#mermaid-svg-3hguKUVcisCC2UmH .image-shape .label,#mermaid-svg-3hguKUVcisCC2UmH .icon-shape .label{text-align:center;}#mermaid-svg-3hguKUVcisCC2UmH .node.clickable{cursor:pointer;}#mermaid-svg-3hguKUVcisCC2UmH .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-3hguKUVcisCC2UmH .arrowheadPath{fill:#333333;}#mermaid-svg-3hguKUVcisCC2UmH .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-3hguKUVcisCC2UmH .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-3hguKUVcisCC2UmH .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-3hguKUVcisCC2UmH .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-3hguKUVcisCC2UmH .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-3hguKUVcisCC2UmH .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-3hguKUVcisCC2UmH .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-3hguKUVcisCC2UmH .cluster text{fill:#333;}#mermaid-svg-3hguKUVcisCC2UmH .cluster span{color:#333;}#mermaid-svg-3hguKUVcisCC2UmH 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-3hguKUVcisCC2UmH .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-3hguKUVcisCC2UmH rect.text{fill:none;stroke-width:0;}#mermaid-svg-3hguKUVcisCC2UmH .icon-shape,#mermaid-svg-3hguKUVcisCC2UmH .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-3hguKUVcisCC2UmH .icon-shape p,#mermaid-svg-3hguKUVcisCC2UmH .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-3hguKUVcisCC2UmH .icon-shape .label rect,#mermaid-svg-3hguKUVcisCC2UmH .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-3hguKUVcisCC2UmH .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-3hguKUVcisCC2UmH .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-3hguKUVcisCC2UmH :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Eden Region
存活对象
Survivor Region
Old Region
垃圾对象直接清理
这个过程需要 STW,因为对象复制和引用修正必须保证一致性。
并发标记
当老年代占用达到一定阈值后,G1 会启动并发标记,找出老年代中哪些对象存活,并统计各 Region 的回收价值。
#mermaid-svg-x4SWWSwd7qStVBHT{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-x4SWWSwd7qStVBHT .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-x4SWWSwd7qStVBHT .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-x4SWWSwd7qStVBHT .error-icon{fill:#552222;}#mermaid-svg-x4SWWSwd7qStVBHT .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-x4SWWSwd7qStVBHT .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-x4SWWSwd7qStVBHT .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-x4SWWSwd7qStVBHT .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-x4SWWSwd7qStVBHT .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-x4SWWSwd7qStVBHT .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-x4SWWSwd7qStVBHT .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-x4SWWSwd7qStVBHT .marker{fill:#333333;stroke:#333333;}#mermaid-svg-x4SWWSwd7qStVBHT .marker.cross{stroke:#333333;}#mermaid-svg-x4SWWSwd7qStVBHT svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-x4SWWSwd7qStVBHT p{margin:0;}#mermaid-svg-x4SWWSwd7qStVBHT .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-x4SWWSwd7qStVBHT .cluster-label text{fill:#333;}#mermaid-svg-x4SWWSwd7qStVBHT .cluster-label span{color:#333;}#mermaid-svg-x4SWWSwd7qStVBHT .cluster-label span p{background-color:transparent;}#mermaid-svg-x4SWWSwd7qStVBHT .label text,#mermaid-svg-x4SWWSwd7qStVBHT span{fill:#333;color:#333;}#mermaid-svg-x4SWWSwd7qStVBHT .node rect,#mermaid-svg-x4SWWSwd7qStVBHT .node circle,#mermaid-svg-x4SWWSwd7qStVBHT .node ellipse,#mermaid-svg-x4SWWSwd7qStVBHT .node polygon,#mermaid-svg-x4SWWSwd7qStVBHT .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-x4SWWSwd7qStVBHT .rough-node .label text,#mermaid-svg-x4SWWSwd7qStVBHT .node .label text,#mermaid-svg-x4SWWSwd7qStVBHT .image-shape .label,#mermaid-svg-x4SWWSwd7qStVBHT .icon-shape .label{text-anchor:middle;}#mermaid-svg-x4SWWSwd7qStVBHT .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-x4SWWSwd7qStVBHT .rough-node .label,#mermaid-svg-x4SWWSwd7qStVBHT .node .label,#mermaid-svg-x4SWWSwd7qStVBHT .image-shape .label,#mermaid-svg-x4SWWSwd7qStVBHT .icon-shape .label{text-align:center;}#mermaid-svg-x4SWWSwd7qStVBHT .node.clickable{cursor:pointer;}#mermaid-svg-x4SWWSwd7qStVBHT .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-x4SWWSwd7qStVBHT .arrowheadPath{fill:#333333;}#mermaid-svg-x4SWWSwd7qStVBHT .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-x4SWWSwd7qStVBHT .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-x4SWWSwd7qStVBHT .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-x4SWWSwd7qStVBHT .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-x4SWWSwd7qStVBHT .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-x4SWWSwd7qStVBHT .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-x4SWWSwd7qStVBHT .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-x4SWWSwd7qStVBHT .cluster text{fill:#333;}#mermaid-svg-x4SWWSwd7qStVBHT .cluster span{color:#333;}#mermaid-svg-x4SWWSwd7qStVBHT 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-x4SWWSwd7qStVBHT .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-x4SWWSwd7qStVBHT rect.text{fill:none;stroke-width:0;}#mermaid-svg-x4SWWSwd7qStVBHT .icon-shape,#mermaid-svg-x4SWWSwd7qStVBHT .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-x4SWWSwd7qStVBHT .icon-shape p,#mermaid-svg-x4SWWSwd7qStVBHT .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-x4SWWSwd7qStVBHT .icon-shape .label rect,#mermaid-svg-x4SWWSwd7qStVBHT .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-x4SWWSwd7qStVBHT .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-x4SWWSwd7qStVBHT .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-x4SWWSwd7qStVBHT :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 初始标记
并发标记
重新标记
清理统计
并发标记阶段大部分可以和应用线程一起运行,但初始标记和重新标记仍然需要短暂停顿。
Mixed GC
Mixed GC 不只回收新生代,还会选择一部分回收价值高的 Old Region 一起回收。
#mermaid-svg-dIWSmPkaVTcpCvyY{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-dIWSmPkaVTcpCvyY .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-dIWSmPkaVTcpCvyY .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-dIWSmPkaVTcpCvyY .error-icon{fill:#552222;}#mermaid-svg-dIWSmPkaVTcpCvyY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-dIWSmPkaVTcpCvyY .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-dIWSmPkaVTcpCvyY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-dIWSmPkaVTcpCvyY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-dIWSmPkaVTcpCvyY .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-dIWSmPkaVTcpCvyY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-dIWSmPkaVTcpCvyY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-dIWSmPkaVTcpCvyY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-dIWSmPkaVTcpCvyY .marker.cross{stroke:#333333;}#mermaid-svg-dIWSmPkaVTcpCvyY svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-dIWSmPkaVTcpCvyY p{margin:0;}#mermaid-svg-dIWSmPkaVTcpCvyY .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-dIWSmPkaVTcpCvyY .cluster-label text{fill:#333;}#mermaid-svg-dIWSmPkaVTcpCvyY .cluster-label span{color:#333;}#mermaid-svg-dIWSmPkaVTcpCvyY .cluster-label span p{background-color:transparent;}#mermaid-svg-dIWSmPkaVTcpCvyY .label text,#mermaid-svg-dIWSmPkaVTcpCvyY span{fill:#333;color:#333;}#mermaid-svg-dIWSmPkaVTcpCvyY .node rect,#mermaid-svg-dIWSmPkaVTcpCvyY .node circle,#mermaid-svg-dIWSmPkaVTcpCvyY .node ellipse,#mermaid-svg-dIWSmPkaVTcpCvyY .node polygon,#mermaid-svg-dIWSmPkaVTcpCvyY .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-dIWSmPkaVTcpCvyY .rough-node .label text,#mermaid-svg-dIWSmPkaVTcpCvyY .node .label text,#mermaid-svg-dIWSmPkaVTcpCvyY .image-shape .label,#mermaid-svg-dIWSmPkaVTcpCvyY .icon-shape .label{text-anchor:middle;}#mermaid-svg-dIWSmPkaVTcpCvyY .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-dIWSmPkaVTcpCvyY .rough-node .label,#mermaid-svg-dIWSmPkaVTcpCvyY .node .label,#mermaid-svg-dIWSmPkaVTcpCvyY .image-shape .label,#mermaid-svg-dIWSmPkaVTcpCvyY .icon-shape .label{text-align:center;}#mermaid-svg-dIWSmPkaVTcpCvyY .node.clickable{cursor:pointer;}#mermaid-svg-dIWSmPkaVTcpCvyY .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-dIWSmPkaVTcpCvyY .arrowheadPath{fill:#333333;}#mermaid-svg-dIWSmPkaVTcpCvyY .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-dIWSmPkaVTcpCvyY .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-dIWSmPkaVTcpCvyY .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-dIWSmPkaVTcpCvyY .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-dIWSmPkaVTcpCvyY .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-dIWSmPkaVTcpCvyY .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-dIWSmPkaVTcpCvyY .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-dIWSmPkaVTcpCvyY .cluster text{fill:#333;}#mermaid-svg-dIWSmPkaVTcpCvyY .cluster span{color:#333;}#mermaid-svg-dIWSmPkaVTcpCvyY 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-dIWSmPkaVTcpCvyY .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-dIWSmPkaVTcpCvyY rect.text{fill:none;stroke-width:0;}#mermaid-svg-dIWSmPkaVTcpCvyY .icon-shape,#mermaid-svg-dIWSmPkaVTcpCvyY .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-dIWSmPkaVTcpCvyY .icon-shape p,#mermaid-svg-dIWSmPkaVTcpCvyY .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-dIWSmPkaVTcpCvyY .icon-shape .label rect,#mermaid-svg-dIWSmPkaVTcpCvyY .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-dIWSmPkaVTcpCvyY .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-dIWSmPkaVTcpCvyY .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-dIWSmPkaVTcpCvyY :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 所有 Region
Eden Region
Survivor Region
Old Region
选择垃圾比例高的 Old Region
Mixed GC 回收集合
这就是 Garbage First 名字的来源:优先回收垃圾收益更高的区域。
G1 为什么适合服务端应用
G1 的优势不是"永远最快",而是更适合大堆和可控停顿目标。
| 能力 | 说明 |
|---|---|
| Region 化 | 回收可以不必每次覆盖整个老年代 |
| 复制整理 | 回收后减少碎片 |
| 可预测停顿 | 根据停顿目标选择回收集合 |
| 兼顾吞吐和延迟 | 比传统低停顿方案更均衡 |
常见参数:
bash
-XX:+UseG1GC
-XX:MaxGCPauseMillis=200
-XX:InitiatingHeapOccupancyPercent=45
MaxGCPauseMillis 是目标,不是硬保证。如果对象分配速度太快、堆设置不合理、Mixed GC 回收跟不上,G1 仍然可能出现长停顿甚至 Full GC。
G1 也不是万能的
G1 常见风险包括:
| 风险 | 说明 |
|---|---|
| 大对象过多 | Humongous Region 分配和回收压力变大 |
| 堆太小 | Region 选择空间不足,GC 更频繁 |
| 分配速度过快 | 并发标记或回收跟不上应用分配 |
| 停顿目标过低 | JVM 为满足目标每次回收太少,导致回收效率下降 |
调 G1 时,不要只盯一个参数。要结合 GC 日志,看 Young GC 频率、Mixed GC 效果、老年代增长趋势、Full GC 原因和停顿分布。
面试回答模板
可以这样回答:
JVM 常见垃圾回收器有 Serial、Parallel、CMS 和 G1。Serial 是单线程收集器,简单但停顿明显;Parallel 使用多线程回收,目标是提高吞吐量;CMS 主要用于老年代,采用并发标记清除,目标是降低停顿,但会产生内存碎片,并且在较新 JDK 中已经移除。G1 是面向服务端的垃圾回收器,它把堆划分成多个 Region,每个 Region 可以作为 Eden、Survivor、Old 或 Humongous。G1 会根据 Region 的垃圾比例和停顿时间目标,优先回收价值高的 Region。它的流程包括 Young GC、并发标记和 Mixed GC。G1 适合大堆和需要控制停顿的场景,但停顿目标不是硬保证,如果回收速度赶不上分配速度,仍然可能触发 Full GC。
小结
垃圾回收器可以按目标来记:
Serial 要简单,Parallel 要吞吐,CMS 要低停顿,G1 要在大堆场景下兼顾停顿和吞吐。
能把 G1 的 Region、Young GC、并发标记、Mixed GC 讲清楚,GC 收集器这组问题就比较稳了。