gpt4 book ai didi

error-handling - 在StackOverflowError上自动重启JVM的最简单方法

转载 作者:行者123 更新时间:2023-12-03 08:11:24 25 4
gpt4 key购买 nike

似乎没有-XX选项可以在 StackOverflowError 上重新启动JVM。当它获得 StackOverflowError 时,自动重启JVM的最简单方法是什么?

最佳答案

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 ...

关于error-handling - 在StackOverflowError上自动重启JVM的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44775936/

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