Java G1 GC:以编程方式查找巨大区域的数量 before/after GC
Java G1 GC: Programmatically finding the number of humongous regions before/after GC
我想以编程方式查找 GC 具有 运行 的巨大区域的数量 before/after,以便将其报告给外部监控服务。
我可以使用 ManagementFactory.getGarbageCollectorMXBeans
和 GarbageCollectionNotificationInfo
找到 GC 操作、持续时间和内存使用情况等信息 before/after,但据我所知,没有明显的方法以相同的方式找到巨大区域的数量。
有没有办法以编程方式访问有关巨大区域数量的信息?
JDK 不会通过 public API 导出此指标。但是,可以使用不安全技巧直接从 JVM 的内存中获取此信息。
HotSpot JVM 发布其内部结构的偏移量以用于调试目的。此信息称为 VMStructs and is available through the HotSpot Serviceability Agent。
获取巨大区域数量的工具如下所示。
import sun.jvm.hotspot.gc_implementation.g1.G1CollectedHeap;
import sun.jvm.hotspot.gc_implementation.g1.HeapRegionSetCount;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.tools.Tool;
public class HumongousRegionSet extends Tool {
@Override
public void run() {
G1CollectedHeap heap = (G1CollectedHeap) VM.getVM().getUniverse().heap();
HeapRegionSetCount count = heap.humongousSet().count();
System.out.printf("%d humongous regions of total capacity %d MB\n",
count.length(), count.capacity() >>> 20);
}
public static void main(String[] args) {
new HumongousRegionSet().execute(args);
}
}
注意:此工具从外部 Java 进程获取信息。
我还有一个概念验证项目helfy to access VMStructs right inside the JVM. Here is an example how to count humongous regions within the running Java process: HumongousRegionSet.java
(此演示目前适用于 Linux 和 Windows;尚不支持 macOS)
我想以编程方式查找 GC 具有 运行 的巨大区域的数量 before/after,以便将其报告给外部监控服务。
我可以使用 ManagementFactory.getGarbageCollectorMXBeans
和 GarbageCollectionNotificationInfo
找到 GC 操作、持续时间和内存使用情况等信息 before/after,但据我所知,没有明显的方法以相同的方式找到巨大区域的数量。
有没有办法以编程方式访问有关巨大区域数量的信息?
JDK 不会通过 public API 导出此指标。但是,可以使用不安全技巧直接从 JVM 的内存中获取此信息。
HotSpot JVM 发布其内部结构的偏移量以用于调试目的。此信息称为 VMStructs and is available through the HotSpot Serviceability Agent。
获取巨大区域数量的工具如下所示。
import sun.jvm.hotspot.gc_implementation.g1.G1CollectedHeap;
import sun.jvm.hotspot.gc_implementation.g1.HeapRegionSetCount;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.tools.Tool;
public class HumongousRegionSet extends Tool {
@Override
public void run() {
G1CollectedHeap heap = (G1CollectedHeap) VM.getVM().getUniverse().heap();
HeapRegionSetCount count = heap.humongousSet().count();
System.out.printf("%d humongous regions of total capacity %d MB\n",
count.length(), count.capacity() >>> 20);
}
public static void main(String[] args) {
new HumongousRegionSet().execute(args);
}
}
注意:此工具从外部 Java 进程获取信息。
我还有一个概念验证项目helfy to access VMStructs right inside the JVM. Here is an example how to count humongous regions within the running Java process: HumongousRegionSet.java
(此演示目前适用于 Linux 和 Windows;尚不支持 macOS)