简体   繁体   English

如何在JVM中捕获OutOfMemoryError并在捕获时运行脚本?

[英]How to catch OutOfMemoryError in JVM and run a script if it's caught?

I have a program that sometimes throw OOME, I understand that there is a flag in the JVM options that I can set and whenever a certain Error/Exception appears (such as OOME) it calls a script I wrote. 我有一个程序,有时会抛出OOME,我知道JVM选项中有一个标志,我可以设置,每当出现某个错误/异常(如OOME)时,它会调用我编写的脚本。 The script will give the user a notification and will call a the program with a different argument so it won't get OOME again. 该脚本将为用户提供通知,并将使用不同的参数调用该程序,以便它不会再次获得OOME。

does anyone know how to set this flag? 有谁知道如何设置这个标志? what is the JVM options I need to set? 我需要设置什么JVM选项? I looked everywhere on line and couldn't find the answer. 我在网上到处看都找不到答案。

help me please! 请帮帮我! Thanks, Aye 谢谢,是的

I found one suggestion in a forum : catch the OOME in your application (like directly in main, assuming you're single threaded) and do this in the catch handler: 我在论坛中找到了一个建议:在你的应用程序中捕获OOME(比如直接在main中,假设你是单线程)并在catch处理程序中执行此操作:

catch (OutOfMemoryError not_again) {
  System.gc();
  System.runFinalization();
  System.gc();
  System.out.println("Your error message");
}

Cleaning up the heap might free just enough memory to produce a last error message before dying. 清理堆可能会释放足够的内存以在死亡之前生成最后一条错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM