如何监视Java内存使用情况? [英] How to monitor Java memory usage?

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

问题描述

我们有一个运行在Jboss上的j2ee应用程序,我们希望监视它的内存使用情况。目前我们使用下面的代码:

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

此代码正常工作。这意味着它显示出符合现实的记忆曲线。当我们从一个数据库创建一个大的xml文件时,曲线就会上升,提取完成后就会下降。





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


  • 如何获得我的内存使用率曲线?

  • 明确的gc()有什么问题?我不在意使用明确的gc()可能发生的小的性能问题,我会在1-3%内估计这些问题。我需要的是内存和线程监视器,它可以帮助我分析我们的客户站点系统。
  • 如果您想真正了解虚拟机内存中发生的情况,您应该使用一个很好的工具,如 VisualVM 。这个软件是免费的,它是一个很好的方式来看看发生了什么。



    显式gc()调用没有什么是错误的。但是,请记住,当您调用gc()时,您建议垃圾回收器正在运行。不能保证它会在您运行该命令的确切时间运行。

    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);
    

    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.

    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:

    • how can I have my memory usage curve?
    • what is wrong with the explicit gc()? I don't care about small performance issues which can happen with explicit gc() and which I would estimate in 1-3%. What I need is memory and thread monitor which helps me in analysis of our system on customer site.

    解决方案

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

    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内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆