本文介绍了如何在通过JMX或代码进行主要垃圾回收之后监视内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多监控工具,例如其他幽灵般的,只监视当前的内存使用情况。如果你想检查内存泄漏或即将发生内存不足的情况,如果你有一个应用程序产生垃圾立即收集,这不是特别有用。不完美,但恕我直言更有趣的是,它会是一个主要的垃圾收集后立即监视内存使用情况。如果这是很高的话,那么崩溃就在你身上。

因此:你能够在最后一次重大垃圾收集之后立即找到内存使用情况 - 无论是从Java代码还是通过JMX ?我知道有一些工具像这样做(它不适用于生产),它可以被写入垃圾回收日志中,但我正在寻找比解析垃圾回收日志文件更直接的解决方案。 :-)要清楚:我正在寻找可以轻松用于任何应用程序的产品,而不是用于调试的昂贵工具。



:使用-XX:+ UseConcMarkSweepGC的JDK 7,但我也对一般性答案感兴趣。 解决方案



垃圾收集器MBean具有属性LastGcInfo,它是包含GC之前和之后内存池大小信息的组合数据对象。 / p>

另外,从Java 7 JMX通知订阅开始,可用于在不进行轮询的情况下接收GC事件。



您可以找到使用GC MBean的代码示例。


Many monitoring tools, like the otherwise phantastic JavaMelody, just monitor the current memory usage. If you want to check for memory leaks or impending out of memory situations, this is not particularily helpful, if you have an application that generates loads of garbage which gets collected immediately. Not perfect, but IMHO much more interesting, would it be to monitor the memory usage immediately after a major garbage collection. If that's high, a crash is looming over you.

So: can you find out the memory usage immediately after the last major garbage collection - either from Java code or via JMX? I know there are some tools like VisualVM which do this (which is no option for production use), and it can be written in the garbage collection log, but I'm looking for a more straightforward solution than parsing the garbage collection logfile. :-) To be clear: I'm looking for something that can easily be used in any application in production, not any expensive tool for debugging.

In case that matters: JDK 7 with -XX:+UseConcMarkSweepGC , but I am interested in general answers, too.

解决方案

Information about memory available right after gc (youg or old) is available via JMX.

Garbage collector MBean has attribute LastGcInfo which is composite data object including information about memory pool sizes before and after GC.

In addition, starting with Java 7 JMX notification subscription could be used to receive GC events without polling.

You can find example of code working with GC MBean here.

这篇关于如何在通过JMX或代码进行主要垃圾回收之后监视内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 15:46