规范 ≠ 实现
Specification ≠ implementation
JVM 规范描述运行时数据区的职责;“年轻代、老年代、TLAB”等通常是 HotSpot 与具体收集器的实现策略。
The JVM specification describes runtime data areas. Young/old generations and TLABs are typically HotSpot and collector implementation strategies.
一份从“内存在哪里”到“垃圾如何被回收”的双语图解指南。读完后,你将能把对象分配、线程栈、类元数据、GC 与常见故障串成一张完整地图。
A visual, bilingual guide from where memory lives to how garbage is reclaimed. Connect object allocation, thread stacks, class metadata, GC, and common failures into one coherent map.
JVM 规范描述运行时数据区的职责;“年轻代、老年代、TLAB”等通常是 HotSpot 与具体收集器的实现策略。
The JVM specification describes runtime data areas. Young/old generations and TLABs are typically HotSpot and collector implementation strategies.
进程还使用线程栈、元空间、代码缓存、直接内存、JNI 与 GC 自身的数据结构。
The process also uses thread stacks, metaspace, code cache, direct memory, JNI, and the collector’s own data structures.
对象不可达后只是具备回收资格;何时、以何种方式回收,由垃圾收集器决定。
An unreachable object is merely eligible for reclamation. The collector decides when and how that memory is recovered.
绝大多数对象实例和数组在这里分配,由垃圾收集器管理。常见 HotSpot 配置会按对象年龄组织年轻代与老年代,但这不是 JVM 规范强制规定的物理布局。
Most object instances and arrays are allocated here and managed by the garbage collector. Common HotSpot configurations organize it into young and old generations, but that physical layout is not mandated by the JVM specification.
多数小对象先尝试在线程本地分配缓冲区(TLAB)中快速分配。
Most small objects first try a fast allocation in the thread-local allocation buffer (TLAB).
新对象通常从 Eden 开始;大对象或特定收集器可能采用不同路径。
New objects commonly begin in Eden; large objects or particular collectors may take another path.
GC 从根集合出发追踪引用;未被触达的对象成为垃圾。
GC traces references from roots; objects not reached become garbage.
多次年轻代回收后仍存活的对象,可能被晋升到老年代。
Objects surviving repeated young collections may be promoted to the old generation.
收集器释放空间,并可能复制或压缩存活对象以降低碎片。
The collector frees space and may copy or compact survivors to reduce fragmentation.
典型根包括活动线程栈中的引用、已加载类的静态字段引用、JNI 全局引用,以及 JVM 内部持有的引用。
Typical roots include references in active thread stacks, static fields of loaded classes, JNI global references, and references held internally by the JVM.
以较高吞吐量为主要目标,回收阶段通常使用多线程,但会产生停顿。
Prioritizes throughput and uses multiple GC threads, while still introducing stop-the-world pauses.
将堆划分为多个 Region,并围绕可预测停顿目标安排回收工作;它是常见的通用选择。
Divides the heap into regions and schedules work around pause-time goals; a common general-purpose choice.
将更多工作与应用线程并发执行,以降低停顿;代价是额外 CPU、内存和运行时开销。
Perform more work concurrently with the application to reduce pauses, at the cost of CPU, memory, and runtime overhead.
重要 / IMPORTANT具体可用的收集器、默认选择和行为取决于 JDK 发行版与版本。先基于延迟目标、吞吐量和堆大小建立假设,再用真实负载测量。Collector availability, defaults, and behavior depend on the JDK distribution and version. Start from latency, throughput, and heap-size goals—then measure with a representative workload.
检查存活集、分配速率、堆转储与 GC 日志。
Inspect the live set, allocation rate, heap dump, and GC logs.
检查类数量、动态生成类,以及类加载器是否泄漏。
Inspect class count, generated classes, and class-loader leaks.
检查无限递归、过深调用链和单个栈帧大小。
Look for unbounded recursion, deep call chains, and large frames.
检查 NIO 缓冲区生命周期、池配置与堆外上限。
Inspect NIO buffer lifetimes, pool configuration, and off-heap limits.
对照堆、线程数、元空间、代码缓存、直接内存与本地库。
Reconcile heap, thread count, metaspace, code cache, direct memory, and native libraries.
jcmd <pid> GC.heap_info
jcmd <pid> VM.native_memory summary
jstat -gcutil <pid> 1s
-Xlog:gc*命令与选项的可用性取决于 JDK。生产环境中使用前,请查看对应版本的文档并评估开销。
Command and option availability depends on the JDK. Check the matching version’s documentation and evaluate overhead before production use.
遇到任何 Java 内存现象时,不要先猜 GC 参数。先定位区域、所有权与生命周期,再验证回收路径和实际证据。
When Java memory behaves unexpectedly, do not begin by guessing GC flags. Locate the region, ownership, and lifetime—then verify the reclamation path with evidence.