简体   繁体   English

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

[英]Can I auto restart tomcat jvm on out of memory exception

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

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”中强制终止进程,堆转储可能不完整。

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.这将为您提供当前在 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.您必须非常了解您的应用程序才能确定哪些对象计数是奇数。

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

This will dump the entire heap of the JVM identified by pid.这将转储由 pid 标识的 JVM 的整个堆。 You can then use the great Eclipse Memory Analyzer to inspect it and find out who is holding on to references of your objects.然后,您可以使用出色的Eclipse Memory Analyzer来检查它并找出谁在持有您的对象的引用。 Your two biggest friends in Eclipse Memory Analyzer are the histo gram and a right click -> references -> exclude weak/soft references to see what is referencing your object.您在 Eclipse Memory Analyzer 中的两个最大的朋友是直方图和right click -> references -> exclude weak/soft references以查看引用您的对象的内容。

jconsole is of course another good tool. jconsole 当然是另一个好工具。

not easily, and definitely not through the JVM that just suffered the out of memory exception.不容易,而且绝对不是通过刚刚遭受内存不足异常的 JVM。 Your best bet would be some combination of tomcat status monitor coupled with cron scripts or related scheduled system administrator scripts;最好的办法是将 tomcat 状态监视器与 cron 脚本或相关的预定系统管理员脚本相结合; something to check the status of the server and automatically stop and restart the service if it has failed.检查服务器状态并在服务失败时自动停止和重新启动服务的东西。

Unfortunately when you kill the java process.不幸的是,当您杀死 java 进程时。 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.您的脚本将保留对 tomcat 端口 8080 8005 8009 的引用,您将无法从同一脚本再次启动它。 The only way it works for me is:它对我有用的唯一方法是:

-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 为您提供的。

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.通常,可以使用可配置的包装器进程来启动和停止您想要的“真实”服务器 VM。 An example I've worked with is "Java Service Wrapper" from Tanuki Software http://wrapper.tanukisoftware.com/doc/english/download.jsp我使用过的一个例子是来自 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.为了首先防止 OOM,有一些方法可以通过接口 bean 检测现代 VM,以查询堆和其他内存结构的状态。 These can be used to, say, warn in a log or an email if some app specific operations are pushing some established limits.如果某些特定于应用程序的操作正在推动某些既定的限制,这些可用于,例如,在日志或电子邮件中发出警告。

我用

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

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

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

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