java 我可以在内存不足异常时自动重启tomcat jvm吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7405046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:49:08  来源:igfitidea点击:

Can I auto restart tomcat jvm on out of memory exception

javatomcatjvm

提问by Abe

I know that this is not "best practice" but I would like to know if I can auto restart tomcat if my deployed app throws an outofmemory exception

我知道这不是“最佳实践”,但我想知道如果我部署的应用程序抛出内存不足异常,我是否可以自动重启 tomcat

回答by Dan

You can try to use the OnOutOfMemoryError JVM option

您可以尝试使用 OnOutOfMemoryError JVM 选项

-XX:OnOutOfMemoryError="/yourscripts/tomcat-restart"

It is also possible to generate the heap dump for later analysis:

也可以生成堆转储以供以后分析:

-XX:+HeapDumpOnOutOfMemoryError

Be careful with combining these two options. If you force killing the process in "tomcat-restart" the heap dump might not be complete.

结合这两个选项时要小心。如果在“tomcat-restart”中强制终止进程,堆转储可能不完整。

回答by Matt Crinklaw-Vogt

I know this isn't what you asked, but have you tried looking through a heap dump to see where you may be leaking memory?

我知道这不是您问的问题,但是您是否尝试过查看堆转储以查看可能泄漏内存的位置?

Some very useful tools for tracking down memory leaks:

一些非常有用的跟踪内存泄漏的工具:

jdk/bin/jmap -histo:live pid

This will give you a histogram of all live objects currently in the JVM. Look for any odd object counts. You'll have to know your application pretty well to be able to determine what object counts are odd.

这将为您提供当前在 JVM 中的所有活动对象的直方图。查找任何奇怪的对象计数。您必须非常了解您的应用程序才能确定哪些对象计数是奇数。

jdk/bin/jmap -dump:live,file=heap.hprof pid

This will dump the entire heap of the JVM identified by pid. You can then use the great Eclipse Memory Analyzerto inspect it and find out who is holding on to references of your objects. Your two biggest friends in Eclipse Memory Analyzer are the histo gram and a right click -> references -> exclude weak/soft referencesto see what is referencing your object.

这将转储由 pid 标识的 JVM 的整个堆。然后,您可以使用出色的Eclipse Memory Analyzer来检查它并找出谁在持有您的对象的引用。您在 Eclipse Memory Analyzer 中的两个最大的朋友是直方图和right click -> references -> exclude weak/soft references查看引用您的对象的内容。

jconsole is of course another good tool.

jconsole 当然是另一个好工具。

回答by mcfinnigan

not easily, and definitely not through the JVM that just suffered the out of memory exception. Your best bet would be some combination of tomcat status monitor coupled with cron scripts or related scheduled system administrator scripts; something to check the status of the server and automatically stop and restart the service if it has failed.

不容易,而且绝对不是通过刚刚遭受内存不足异常的 JVM。您最好的选择是将 tomcat 状态监视器与 cron 脚本或相关的预定系统管理员脚本相结合;检查服务器状态并在服务失败时自动停止和重新启动服务的东西。

回答by clvrmnky

Generally, no. The VM is a bad state, and cannot be completely trusted.

一般来说,没有。VM 状态不佳,不能完全信任。

Typically, one can use a configurable wrapper process that starts and stops the "real" server VM you want. An example I've worked with is "Java Service Wrapper" from Tanuki Software http://wrapper.tanukisoftware.com/doc/english/download.jsp

通常,可以使用可配置的包装器进程来启动和停止您想要的“真实”服务器 VM。我使用过的一个例子是来自 Tanuki Software http://wrapper.tanukisoftware.com/doc/english/download.jsp 的“Java Service Wrapper”

I know there are others.

我知道还有其他人。

To guard against OOMs in the first place, there are ways to instrument modern VMs via interface beans to query the status of the heap and other memory structures. These can be used to, say, warn in a log or an email if some app specific operations are pushing some established limits.

为了首先防止 OOM,有一些方法可以通过接口 bean 检测现代 VM,以查询堆和其他内存结构的状态。如果某些特定于应用程序的操作正在推动某些既定的限制,则这些可用于,例如,在日志或电子邮件中发出警告。

回答by Georgi Ivanov

Unfortunately when you kill the java process. Your script will keep a reference to the tomcat ports 8080 8005 8009 and you will not be able to start it again from the same script. The only way it works for me is:

不幸的是,当您杀死 java 进程时。您的脚本将保留对 tomcat 端口 8080 8005 8009 的引用,您将无法从同一脚本再次启动它。它对我有用的唯一方法是:

-XX:OnOutOfMemoryError="kill -9 %p" and then another cron or monit or something similar to ensure you have the tomcat running again.

-XX:OnOutOfMemoryError="kill -9 %p" 然后是另一个 cron 或 monit 或类似的东西,以确保您再次运行 tomcat。

%p is actually the JVM pid , something the JVM provides for you.

%p 实际上是 JVM pid ,这是 JVM 为您提供的。

回答by Jonathan

I use

我用

-XX:OnOutOfMemoryError='pkill java;/usr/local/tomcat/bin/start.sh'

回答by Addo Solutions

What about something like this? -XX:OnOutOfMemoryError="exec \`ps --no-heading -p $$ -o cmd\`"

像这样的事情怎么办? -XX:OnOutOfMemoryError="exec \`ps --no-heading -p $$ -o cmd\`"