gpt4 book ai didi

Java G1 GC : Programmatically finding the number of humongous regions before/after GC

转载 作者:行者123 更新时间:2023-12-04 23:35:31 25 4
gpt4 key购买 nike

我想以编程方式查找 GC 运行之前/之后的巨大区域数量,以便将其报告给外部监控服务。

我可以在使用 ManagementFactory.getGarbageCollectorMXBeans 之前/之后找到诸如 GC 操作、持续时间和内存使用情况等信息。和 GarbageCollectionNotificationInfo ,但据我所知,没有明显的方法可以以相同的方式找到巨大区域的数量。

有没有办法以编程方式访问有关巨大区域数量的信息?

最佳答案

JDK 不通过公共(public) API 导出此指标。但是,可以使用 Unsafe 技巧直接从 JVM 的内存中获取此信息。

HotSpot JVM 发布其内部结构的偏移量以用于调试目的。此信息称为 VMStructs可通过 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直接在 JVM 内部访问 VMStructs。下面是一个如何计算正在运行的 Java 进程中巨大区域的示例: HumongousRegionSet.java

(此演示目前适用于 Linux 和 Windows;尚不支持 macOS)

关于Java G1 GC : Programmatically finding the number of humongous regions before/after GC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58856845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com