在 StackOverflowError 上重新启动JVM似乎没有-XX选项。当JVM获得 StackOverflowError 时自动重启JVM的最简单方法是什么?
答案 0 :(得分:2)
HotSpot JVM具有内置-XX:AbortVMOnException=java.lang.StackOverflowError
选项,但不幸的是,此标志仅在JVM的调试版本中可用。
工作解决方案是使用JVM TI agent来拦截所有异常,并在异常属于指定类时中止进程。以下是此类代理的一个示例。
#include <jvmti.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
static const char* fatal_error_class;
void JNICALL ExceptionCallback(jvmtiEnv* jvmti, JNIEnv* env, jthread thread,
jmethodID method, jlocation location, jobject exception,
jmethodID catch_method, jlocation catch_location) {
char* class_name;
jclass exception_class = env->GetObjectClass(exception);
jvmti->GetClassSignature(exception_class, &class_name, NULL);
class_name[strlen(class_name) - 1] = 0;
if (strcmp(class_name + 1, fatal_error_class) == 0) {
printf("Abort on fatal error\n");
exit(1);
}
jvmti->Deallocate((unsigned char*)class_name);
}
extern "C" JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* vm, char* options, void* unused) {
if (options == NULL || options[0] == 0) {
printf("Usage: -agentpath:/path/to/libabort.so=java/lang/StackOverflowError\n");
return 1;
}
fatal_error_class = strdup(options);
jvmtiEnv* jvmti;
vm->GetEnv((void**)&jvmti, JVMTI_VERSION_1_0);
jvmtiCapabilities capabilities = {0};
capabilities.can_generate_exception_events = 1;
jvmti->AddCapabilities(&capabilities);
jvmtiEventCallbacks callbacks = {0};
callbacks.Exception = ExceptionCallback;
jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL);
return 0;
}
如何编译它:
g++ -I $JAVA_HOME/include -I $JAVA_HOME/include/linux -fPIC -shared -olibabort.so abort.cpp
如何运行:
java -agentpath:/path/to/libabort.so=java/lang/StackOverflowError ...