如何监视基于Windows的JVM的内存使用情况,如果过高则触发警报?

我有一个在Windows 2008中作为JVM运行的应用程序。我想监视它的内存使用情况,并在内存使用达到特定阈值时发送电子邮件。 有没有免费的工具或方法来做到这一点?

Win2008操作系统可以发送性能计数器阈值条件的警报。

Part 1 # Perfmon Launch Performance Monitor (perfmon.msc) Create a new User Defined Data Collector Set Choose to create manually after naming Select Performance Counter Alert Click Add - choose the Object, Counter and Instance to monitor. Process/Java.exe/Working Set Specify the alert threshold Choose "open properties for this data collector set" Specify a start schedule if the server is on a reboot schedule Change the sample interval. Alert repeats at this interval when value it above threshold Under Alert task, give it a name (like sendMail) Start the Data Collector Set Part 2 # Schedule Tasks Open up scheduled tasks (compmgmt.msc/configuration/task scheduler) Create a task, not a basic task Name it the exact same name you did in the above section (like sendMail) Check "user is logged in or not" so that it runs all the time. Change username if necessary. Create a new email action under the Action tab Enter all the info for from, to, subject, etc. Enter the SMTP server. You will be prompt for the password to run the task when you are not logged on. 

实际上,JVM的内存边界是由启动参数控制的,如-Xmx2048m -Xms256m -XX:MaxPermSize=512m (例如)。 JVM将使用允许的内存。 稳定不漏的应用程序不会超出这个界限。

回到监视,可以打开JVM内部SNMP代理,然后监视堆和PermGen的内存使用情况,并通过专用SNMP社区,您可以在超过JVM发送SNMP陷阱时设置阈值。 实际上它是由HP OpenView等工具“监控的企业解决scheme”。 涉及的Cmdline参数:

 -Dcom.sun.management.snmp.port=10001 -Dcom.sun.management.snmp.acl=/home/liferay/snmp.acl -Dcom.sun.management.snmp.interface=0.0.0.0 

他们是自谋职业的。 ACL文件模板位于JRE_HOME/lib/management/snmp.acl.template 。 为了获得可读的安装,不仅仅是SNMP OID,请下载JVM-MANAGEMENT-MIB.mib 。 Oracle网站上有一些简短的解释。 启用S​​NMP后,您可以运行以下命令来检查返回的量表:

 snmpwalk -v2c -c public YourWinHost:10001 -m JVM-MANAGEMENT-MIB jvmMgtMIB 

它期望你已经把JVM-MANAGEMENT-MIB.mib为默认的Net-SNMP MIB目录(Unix或者CygWin命令行)。

不要忘了邮件发送,我正在通过Zabbix和SNMP运行在邮局的全国后端群集上进行JVM监控。 它涵盖了所有运行时监视要求。 Zabbix可以向您发送电子邮件或Jabber IM消息,以防触发某些触发器。 Zabbix也支持SNMP代理监视和SNMP陷阱。 我在云端运行了一个月的服务testing,当时监控的JVM在美国的某个地方,而在爱尔兰的Zabbix服务器上……即使是这样的距离/延迟,它也运行良好。 从2.0版本开始,可以进行JMX监视,但是对于安全的环境来说可能很复杂,因此JMX-RMI会随机设置一些端口。

一般情况下,SNMP对JVM运行时监视来说已经足够了,JMX可以在开发/testing机器上提供更多帮助,因为它可以监视应用程序的细微差别。

如果您使用的是Windows机器,则可以使用SaaS监控服务之一。 NewRelic服务让我印象深刻。 它很容易设置和真正有用的开发/testing阶段,原因可以做的事情,如线程转储,缓慢的SQL查询aso它可以发送电子邮件时达到的阈值。

由于Windows 2012 Server不赞成使用“发送电子邮件”任务types,因此您现在需要一个脚本来发送电子邮件。 例如,一个VBScript:

 Set Msg = CreateObject("CDO.Message") With Msg .To = "[email protected]" .From = "[email protected]" .Subject = "low memory" .TextBody = "low memory, check the server now" .Send .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.yourserver.com" .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "smtp-user" .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "smtp-password" End With 

其他步骤几乎相同。 请参阅此博客文章以获取更多信息

您可以使用JMX设置监视。

这是一个运行在Tomcat或JEE应用程序服务器上的服务器应用程序吗? 或者这是一个命令行Java应用程序? 或Windows服务包装的Java应用程序? 哪个JVM? 什么设置? Oracle JDK Rockit拥有优秀的内存监视器,Oracle JRockit Mission Control 4.1或http://visualvm.java.net/index.html

你可以在你的应用程序中编写一个线程,睡觉并唤醒以检查内存。

看到这些post: https : //stackoverflow.com/questions/1058991/how-to-monitor-java-memory-usage或https://stackoverflow.com/questions/74674/how-to-do-i-check- CPU和内存使用率,在Java的

您可以运行JMX,但它不显示例如垃圾收集器。 您也可以监视日志文件。 内存不足时,没有实用程序来重新启动Java。 但是,使用Java编写代码很容易,并且可以作为服务器运行,如果应用程序运行正常,它将检查应用程序,以及您需要创build的业务逻辑,例如if(TotalCPUTime > 1d) restartJava();

无论如何,要通过JMX远程重启Java,需要访问服务器,所以没有实用工具可以同时执行这两个操作 – 实际运行,监视,重新启动和监视Java进程。 您也可以在另一个Java上运行此Java,因此它将同时进行监视和重新启动,并运行一个小型java.exe来执行此操作。