简体   繁体   English

如何使用JMX监视在内存不足时重启JVM?

[英]How to restart JVM when it runs out of memory by using JMX monitoring?

I want to create a nagios watchdog for JVM that looks when the JVM runs out of memory and restarts it. 我想为JVM创建一个nagios监视程序,查看JVM内存不足并重新启动它。

Currently I was able to setup the JVM be allow JMX but I don't know how to detect OutOfMemory condition and restart it. 目前我能够设置JVM是允许JMX但我不知道如何检测OutOfMemory条件并重新启动它。

 /check_jmx -U service:jmx:rmi:///jndi/rmi://127.0.0.1:1100/jmxrmi -O "java.lang:type=Memory" -A "HeapMemoryUsage" -K used -I HeapMemoryUsage -J used -vvvv
 JMX OK HeapMemoryUsage.used=957414288{committed=2415984640;init=2147483648;max=2863333376;used=957414288}

https://github.com/tcurdt/nagios-check-jmx https://github.com/tcurdt/nagios-check-jmx

If you're using Java 1.4.2 or later, this option allows you to execute a user defined command when the first OutOfMemeory exception occurs: -XX:OnOutOfMemoryError="<cmd args>;<cmd args>" 如果您使用的是Java 1.4.2或更高版本,则此选项允许您在发生第一个OutOfMemeory异常时执行用户定义的命令: -XX:OnOutOfMemoryError =“<cmd args>; <cmd args>”

That should give you some decent options. 这应该给你一些不错的选择。 eg you could kick off a passive check to nagios to tell it that the server is being restarted, and then kick off a shell script to stop/start your errant JVM. 例如,您可以启动被动检查nagios告诉它服务器正在重新启动,然后启动shell脚本以停止/启动错误的JVM。

I don't think you are going to be able to detect an out-of-memory condition using JMX. 我不认为您将能够使用JMX检测到内存不足的情况。 If the JVM is really at the end of it's life, JMX connections will most likely throw OOM exceptions themselves when you try to connect. 如果JVM确实处于生命的终点,那么当您尝试连接时,JMX连接很可能会自行抛出OOM异常。

We detect high memory conditions instead of OOM. 我们检测内存条件而不是OOM。 We alarm when our systems' free-memory goes below some water mark for a certain amount of time. 当我们的系统的自由存储器在一定时间内低于某个水印时,我们会发出警报。 We also have threads that run to dump per-serve metric files. 我们还有运行转储每服务度量标准文件的线程。 Since the thread is already allocated, it can reliably dump system memory information after the JVM runs out. 由于线程已经分配,​​因此可以在JVM用完后可靠地转储系统内存信息。

We log: 我们记录:

// free memory
Runtime.getRuntime().freeMemory()
// current heap usage
Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()

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

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