如何监控 Java 内存使用情况?

How to monitor Java memory usage?(如何监控 Java 内存使用情况?)

本文介绍了如何监控 Java 内存使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个在 Jboss 上运行的 j2ee 应用程序,我们想要监控它的内存使用情况.目前我们使用以下代码

We have a j2ee application running on Jboss and we want to monitor its memory usage. Currently we use the following code

    System.gc();
    Runtime rt = Runtime.getRuntime();
    long usedMB = (rt.totalMemory() - rt.freeMemory()) / 1024 / 1024;
    logger.information(this, "memory usage" + usedMB);

此代码运行良好.这意味着它显示了与现实相对应的记忆曲线.当我们从数据库创建一个大的 xml 文件时,曲线上升,提取完成后曲线下降.

This code works fine. That means it shows memory curve which corresponds to reality. When we create a big xml file from a DB a curve goes up, after the extraction is finished it goes down.

一位顾问告诉我们,显式调用 gc() 是错误的,让 jvm 决定何时运行 gc".基本上他的论点与在这里讨论过的相同.但我还是不明白:

A consultant told us that calling gc() explicitly is wrong, "let jvm decide when to run gc". Basically his arguments were the same as disscussed here. But I still don't understand:

  • 如何获得我的内存使用曲线?
  • 显式 gc() 有什么问题?我不关心显式 gc() 可能发生的小性能问题,我估计在 1-3% 之间.我需要的是内存和线程监视器,它可以帮助我在客户站点上分析我们的系统.

推荐答案

如果你想真正了解 VM 内存中发生了什么,你应该使用像 VisualVM.这是免费软件,是了解正在发生的事情的好方法.

If you want to really look at what is going on in the VM memory you should use a good tool like VisualVM. This is Free Software and it's a great way to see what is going on.

明确的 gc() 调用并没有真正错误".但是,请记住,当您调用 gc() 时,您是在建议"垃圾收集器运行.无法保证它会在您运行该命令的确切时间运行.

Nothing is really "wrong" with explicit gc() calls. However, remember that when you call gc() you are "suggesting" that the garbage collector run. There is no guarantee that it will run at the exact time you run that command.

这篇关于如何监控 Java 内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何监控 Java 内存使用情况?

基础教程推荐