Inside the Java Virtual Machine: The Algorithm and Implementation of JVM G1GC
Nakamura ShigehiroPosts & Telecom Press2021
No rating yet- Word count
- 224K words
- Chapters
- 1
- Series
- Turing Programming Series · Java
- ISBN
- 9787115554529
Tags
Computing Programming Java Virtual MachineMemory ManagementHistorical DataGarbage CollectionUnderlying PrinciplesPerformance TuningSummary
This book offers an in-depth look at the inner workings of the Java Virtual Machine, centered on G1GC (Garbage First Garbage Collection), the garbage collection algorithm in JVM memory management. It is divided into two main parts: "Algorithms" and "Implementation." The Algorithms part first introduces the basic idea and heap structure of G1GC, including the heap layout under Region partitioning and the two main threads of concurrent marking and evacuation; it then breaks down concurrent marking stage by stage—initial marking, concurrent marking, final marking, live-object counting, and cleanup—and explains collection-set selection, root evacuation, and the evacuation flow in the evacuation phase, along with supporting mechanisms such as the evacuation-specific remembered set, write barriers, and hot cards. On this foundation the author devotes substantial space to G1GC's soft real-time behavior: how evacuation time is predicted from historical data, how prediction confidence is assessed, and how GC pauses are scheduled; the differences between generational and pure G1GC modes are also covered. The Implementation part draws on HotSpotVM source code, beginning with environment setup and the overall structure of object management, then analyzing the CollectedHeap, CollectorPolicy, and the various GC factory classes, the G1 heap structure, the memory allocator and TLABs, object and object-header structures, and HotSpotVM's thread creation, lifecycle, and mutual exclusion handling; it goes on to dissect the parallel and concurrent GC thread frameworks, the concrete implementations of concurrent marking and evacuation, pause-time prediction and scheduling, exact GC, and write-barrier performance overhead. With more than ninety diagrams and thirty-odd code excerpts, the book is suited to readers who want to understand G1GC at both the algorithmic and source-code levels.Editor's review
市面上讲 JVM 的书不少,但愿意把 G1 回收器「为什么这样设计」讲透的并不多。本书作者中村成洋长期研究垃圾回收,是 CRuby 的 committer,也是《垃圾回收的算法与实现》实现篇的作者;本书的源头是他的日文专著《徹底解剖「G1GC」》算法篇与实现篇。全书最大的特点是把 G1GC 拆成「算法」与「实现」两层来读:算法篇用九十余张图表、三十余段代码,把并发标记各阶段、转移的回收集合与根转移、软实时性中的暂停时间预测等机制一层层摆开;实现篇直接对着 HotSpotVM 源码,讲对象管理、堆结构、分配器、TLAB、对象头、线程与互斥处理,直到并发标记、转移、预测调度和写屏障开销的实现细节。它不替代面试手册,也不教调参,而是补上「G1 到底怎么跑起来」这一块知识。对已经会用 G1、却总觉得云里雾里的工程师,这本书像一份细致的源码地图;对读过 Garbage-First 论文却没能完全理解的读者,它提供了论文之外的分步解释。Who it's for
A good fit for
- 已经熟悉 Java 基础与 JVM 内存模型,想深入理解 G1 回收器原理的工程师
- 准备阅读 HotSpot 源码、需要一份导读与路线图的中高级开发者
- 从事 JVM 调优,希望从机制层面解释 GC 日志与暂停时间的人
- 对垃圾回收算法本身感兴趣、想横向比较多款回收器的技术爱好者
Not a good fit for
- 刚学 Java、尚未接触 JVM 内存区域与对象分配概念的初学者
- 只想找几条 GC 参数快速调优、不关心原理的读者
- 主要使用非 HotSpot 系列虚拟机且需要对应实现细节的读者
Reading tips
- 先通读算法篇,把并发标记、转移与暂停预测的流程画出自己的流程图,再进入实现篇对源码。
- 阅读实现篇时配合 OpenJDK 7/8 的 hotspot 源码,边看边定位书中提到的类与字段,效果最好。
- 遇到软实时性、预测调度等抽象章节,可结合一次线上 GC 日志中的实际暂停时间对照理解。
- 不必一次读完二十章,可把第 16、17、18 章作为 G1 实现的重点,其余章节按需查阅。
About the publisher
Posts & Telecom Press China · Founded in 1953Posts & Telecom Press is known for its strengths in science and technology, computing, and information and communications titles, and has extended its publishing into photography, art, software learning, and inspirational growth, giving it a fairly diverse range. Its catalog includes computing and communications works such as Refactoring and Patterns (Revised Edition) and Huawei Switch Learning Guide (Second Edition), alongside bilingual collector's editions such as The Little Prince (Chinese-English-French illustrated collector's edition), The Great Gatsby, and Three Days to See. Beijing Ten Years: 1980-1990 and Ghost Blade: WLOP Personal Illustration Collection reflect its presence in photography and art. Its authors include Antoine de Saint-Exupéry, Helen Keller, Joshua Kerievsky, Wang Da, Zhang Zhaozeng, and WLOP.
Contents
- Turing Programming Series · Java
FAQ
这本书和《深入理解Java虚拟机》是同一本吗?
不是。本书是日本作者中村成洋所著、吴炎昌与杨文轩翻译,由人民邮电出版社出版的《深入Java虚拟机:JVM G1GC的算法与实现》,专讲 G1GC;周志明的《深入理解Java虚拟机》覆盖范围更广,作者与结构都不同。阅读本书需要哪些前置知识?
建议已了解 JVM 内存区域、对象分配与常见 GC 概念,最好使用过 G1 并看过 GC 日志。实现篇涉及 C++ 与 HotSpot 源码,具备一定 C++ 阅读能力和 Linux 线程基础会更顺畅。书中的实现基于哪个 JDK 版本?
书中以较早的 OpenJDK 版本(G1 在 Java 7 中引入)的 HotSpot 源码为依据,讲解的是 G1 的经典实现结构。较新 JDK 中部分类与调用路径已有调整,但并发标记、转移、预测调度的核心思路仍可对照理解。这本书偏理论还是偏实战调优?
偏原理与实现,不提供现成的调参清单。它解释 G1 如何并发标记、如何选择回收集合、如何预测暂停时间,读完有助于自己读懂 GC 日志并判断参数含义,但参数实践仍需结合具体应用。全书篇幅如何,需要多久读完?
中文版正文约二百二十余页,分二十章,篇幅不长但信息密度高。建议预留数周时间,算法篇可较快通读,实现篇配合源码逐章研读会明显拉长时间。This site is an index: you start “Search all sources” in your browser, and the entries it lists all point to third-party sites; please judge their availability yourself.
About the author
Nakamura Shigehiro AuthorNakamura Shigehiro is a Japanese author known for writing on computer technology. Publicly available information about his nationality, dates of birth and death, and personal background is limited, and two of his works are currently included in the Chinese book database. Both works address the underlying mechanisms of program execution: one discusses the algorithms and implementation of garbage collection, while the other focuses on the algorithms and implementation of the G1 garbage collector in the Java Virtual Machine. Judging from these subjects, the author is concerned with foundational engineering issues such as memory management, automatic reclamation, and virtual machine internals, and his writing tends to set abstract algorithms against concrete implementations to help readers understand how systems work at a low level. His works commonly fall under computer and programming design categories and are suitable for readers with some programming background who wish to gain a deeper understanding of runtime environments.