从应用程序内请求 java 堆转储(核心转储)

发布于 2024-08-12 15:50:56 字数 356 浏览 6 评论 0原文

我需要一种从应用程序内部请求堆转储的方法。

理由:当我遇到特定的错误情况时,我想转储堆,以便我可以看到内存中占用了什么。

但我想自动执行此操作(例如,当我检测到发生了某些特定情况时。或者当看门狗不再获得其 ping 时。当某些测试失败时)。因此,我需要一种从应用程序本身内部转储堆的方法。我似乎找不到 MX beans 的东西。尽管 MX Bean 可以提供非常好的堆栈跟踪,包括监视器和“可拥有的同步器”信息、死锁和争用信息,但我似乎找不到请求堆转储的方法。有这样的办法吗?或者通过一些间接的方式,例如这些JVisualVM的东西是如何做到的?并且可以告诉 JVM 在 OutOfMemoryExceptions 上转储核心..?

I need a way to request a heap dump from within the application.

Rationale: When I encounter a specific error condition, I'd like to dump heap, so that I can see what is holding on to the memory.

But I would like to automate this (For example, when I detect that some specific condition has occurred. Or when a watchdog doesn't gets its pings anymore. When some test fails). Thus I need a way to dump the heap from within the application itself. I can't seem to find it with the MX beans stuff. Although the MX Beans can give very nice stack traces with monitor and "ownable synchronizer" info, deadlock and contention info, I can't seem to find a way to request a heap dump. Are there any such way? Or by some indirect means, for example how does these JVisualVM things do it? And one can tell the JVM to dump core on OutOfMemoryExceptions..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没结果 2024-08-19 15:50:56

如果还不够使用 -XX:HeapDumpOnOutOfMemoryError 命令行参数用于在 OutOfMemoryError 上转储堆,有一种特定于 HotSpot 的方式 以编程方式转储堆来自 Java 应用程序

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
String dumpFilePath = "./banana.hprof";
HotSpotDiagnosticMXBean hotSpotDiagnosticMXBean = ManagementFactory.newPlatformMXBeanProxy(
    mBeanServer,
    "com.sun.management:type=HotSpotDiagnostic",
    HotSpotDiagnosticMXBean.class);
hotSpotDiagnosticMXBean.dumpHeap(dumpFilePath, true);

If it's not enough to use the -XX:HeapDumpOnOutOfMemoryError command line argument to dump heap on OutOfMemoryError, there's a HotSpot-specific way of programmatically dumping heap from Java applications:

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
String dumpFilePath = "./banana.hprof";
HotSpotDiagnosticMXBean hotSpotDiagnosticMXBean = ManagementFactory.newPlatformMXBeanProxy(
    mBeanServer,
    "com.sun.management:type=HotSpotDiagnostic",
    HotSpotDiagnosticMXBean.class);
hotSpotDiagnosticMXBean.dumpHeap(dumpFilePath, true);
撑一把青伞 2024-08-19 15:50:56

如何使用 VM 选项 -XX:+HeapDumpOnOutOfMemoryError 告诉 HotSpot VM 在内存不足时生成堆转储?

What about using the VM option -XX:+HeapDumpOnOutOfMemoryError to tell the HotSpot VM to generate a heap dump when it runs out of memory?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文