Generate Java Heap Dump on uncaught Exception(在未捕获的异常上生成 Java 堆转储)
问题描述
当触发未捕获的异常时,我尝试生成堆转储.我尝试使用 jmap,但是因为当异常发生时进程已经完成,所以这是不可能的.
I try to generate a Heap Dump when a uncaught exception is fired. I tried using jmap, but because the process is finished when the exception happens this is not possible.
使用 UncaughtExceptionHandler 也不是选项,因为我只有被执行程序的二进制文件.
Using a UncaughtExceptionHandler is no option either, because I only have the binaries of the programs that is executed.
谁能帮帮我?
通过命令行或类似方法可以使用该技术很重要,因为我需要将其自动化.使用 GUI 是不行的
It is important that the technique is available through a command line or similar, because I need to automated this. Using a GUI is no option
推荐答案
这可以通过 JVMTI 代理将监听 VMDeath 事件,然后使用 JMM 接口 启动堆转储.
This can be achieved with JVMTI agent that will listen to VMDeath event and then use JMM interface to initiate Heap Dump.
以下是此类 JVMTI 代理的示例源代码:
Here is a sample source code of such JVMTI agent:
#include <jvmti.h>
#include <string.h>
#include <stdio.h>
#include "jmm.h"
JNIEXPORT void* JNICALL JVM_GetManagement(jint version);
void JNICALL VMDeath(jvmtiEnv* jvmti, JNIEnv* jni) {
JmmInterface* jmm = (JmmInterface*) JVM_GetManagement(JMM_VERSION_1_0);
if (jmm == NULL) {
printf("Sorry, JMM is not supported
");
} else {
jstring path = (*jni)->NewStringUTF(jni, "dump.hprof");
jmm->DumpHeap0(jni, path, JNI_TRUE);
printf("Heap dumped
");
}
}
JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
jvmtiEnv* jvmti;
(*vm)->GetEnv(vm, (void**)&jvmti, JVMTI_VERSION_1_0);
jvmtiEventCallbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.VMDeath = VMDeath;
(*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
(*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL);
return 0;
}
将其编译到共享库 (libdump.so) 后,使用 -agentpath
选项运行 Java:
After you've compiled it into the shared library (libdump.so) run Java with -agentpath
option:
java -agentpath:/path/to/libdump.so MainClass
如果您希望处理未捕获的异常而不是等待 VMDeath,您可以使用类似的技术为异常事件安装回调.查看此处的示例.
If you wish to handle uncaught exceptions instead of waiting for VMDeath, you may use similar technique to install callback for Exception event. Look here for an example.
这篇关于在未捕获的异常上生成 Java 堆转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在未捕获的异常上生成 Java 堆转储


- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01