一、jmap获取堆概要信息
JDK9之前:
jmap -heap 33146
JDK9及之后:
jhsdb jmap --heap --pid 33146
执行示例:
zhibo-mac:JavaVirtualMachines zhibo$ jhsdb jmap --heap --pid 33146 Attaching to process ID 33146, please wait... Debugger attached successfully. Server compiler detected. JVM version is 11.0.6+8-LTS using thread-local object allocation. Garbage-First (G1) GC with 10 thread(s) Heap Configuration: MinHeapFreeRatio = 40 MaxHeapFreeRatio = 70 MaxHeapSize = 4294967296 (4096.0MB) NewSize = 1363144 (1.2999954223632812MB) MaxNewSize = 2576351232 (2457.0MB) OldSize = 5452592 (5.1999969482421875MB) NewRatio = 2 SurvivorRatio = 8 MetaspaceSize = 21807104 (20.796875MB) CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB G1HeapRegionSize = 1048576 (1.0MB) Heap Usage: G1 Heap: regions = 4096 capacity = 4294967296 (4096.0MB) used = 5242880 (5.0MB) free = 4289724416 (4091.0MB) 0.1220703125% used G1 Young Generation: Eden Space: regions = 6 capacity = 27262976 (26.0MB) used = 6291456 (6.0MB) free = 20971520 (20.0MB) 23.076923076923077% used Survivor Space: regions = 0 capacity = 0 (0.0MB) used = 0 (0.0MB) free = 0 (0.0MB) 0.0% used G1 Old Generation: regions = 0 capacity = 241172480 (230.0MB) used = 0 (0.0MB) free = 241172480 (230.0MB) 0.0% used
如果 jmap 命令执行失败,请参考:JVM工具:使用 jmap 打印 Java 堆信息时报错:Can’t attach symbolicator to the process
二、Heap Configuration
MinHeapFreeRatio = 40 空闲堆空间的最小百分比,计算公式为:HeapFreeRatio =(CurrentFreeHeapSize/CurrentTotalHeapSize) * 100,值的区间为0到100,默认值为 40。如果HeapFreeRatio < MinHeapFreeRatio,则需要进行堆扩容,扩容的时机应该在每次垃圾回收之后。 MaxHeapFreeRatio = 70 空闲堆空间的最大百分比,计算公式为:HeapFreeRatio =(CurrentFreeHeapSize/CurrentTotalHeapSize) * 100,值的区间为0到100,默认值为 70。如果HeapFreeRatio > MaxHeapFreeRatio,则需要进行堆缩容,缩容的时机应该在每次垃圾回收之后。 MaxHeapSize = 4294967296 (4096.0MB) JVM 堆空间允许的最大值。 NewSize = 1363144 (1.2999954223632812MB) JVM 新生代堆空间的默认值。 MaxNewSize = 2576351232 (2457.0MB) JVM 新生代堆空间允许的最大值。 OldSize = 5452592 (5.1999969482421875MB) JVM 老年代堆空间的默认值。 NewRatio = 2 新生代(2个Survivor区和Eden区 )与老年代(不包括永久区)的堆空间比值,表示新生代:老年代=1:2。 SurvivorRatio = 8 两个Survivor区和Eden区的堆空间比值为 8,表示 S0 : S1 :Eden = 1:1:8。 MetaspaceSize = 21807104 (20.796875MB) JVM 元空间的默认值。 CompressedClassSpaceSize = 1073741824 (1024.0MB) MaxMetaspaceSize = 17592186044415 MB JVM 元空间允许的最大值。 G1HeapRegionSize = 1048576 (1.0MB) 在使用 G1 垃圾回收算法时,JVM 会将 Heap 空间分隔为若干个 Region,该参数用来指定每个 Region 空间的大小。 三、Heap Usage G1 Heap: regions = 4096 capacity = 4294967296 (4096.0MB) used = 5242880 (5.0MB) free = 4289724416 (4091.0MB) 0.1220703125% used G1 的 Heap 使用情况,该 Heap 包含 4096 个 Region,结合上文每个 RegionSize=1M,因此 Capacity = Regions * RegionSize = 4096 * 1M = 4096M,已使用空间为 5M,空闲空间为 4091M,使用率为 0.12%。 G1 Young Generation: Eden Space: regions = 6 capacity = 27262976 (26.0MB) used = 6291456 (6.0MB) free = 20971520 (20.0MB) 23.076923076923077% used G1 的 Eden 区的使用情况,总共使用了 6 个 Region,结合上文每个 RegionSize=1M,因此 Used = Regions * RegionSize = 6 * 1M = 6M,Capacity=26M表名当前 Eden 空间分配了 26 个 Region,使用率为 23%。 G1 Young Generation: Survivor Space: regions = 0 capacity = 0 (0.0MB) used = 0 (0.0MB) free = 0 (0.0MB) 0.0% used G1 的 Survivor 区的使用情况,同 Eden区。 G1 Old Generation: regions = 0 capacity = 241172480 (230.0MB) used = 0 (0.0MB) free = 241172480 (230.0MB) 0.0% used G1 的 老年代的使用情况,同 Eden区。 上文对于 Heap 空间的参数都是可用通过 JVM 虚拟机命令行参数进行设置的