测量 Java 程序内存使用情况的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3536149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:17:12  来源:igfitidea点击:

Best way to measure Memory Usage of a Java Program?

javaprofilervisualvmmeasurementmemory-consumption

提问by iceburn

I'm currently using VisualVM, but the problem I'm having is that I can't save the graphs it generates. I need to report some data about its memory usage and running time, though running time is easy to get with System.nanoTime(). I've also tried the NetBeans profilerbut it isn't what I want, since I'm not looking for specific parts that would be slowing it down or anything, so that would be overkill. The biggest problem with it is that it eats up too much processing time. Also doesn't let me capture/transfer the data easily, like VisualVM, at least as far as I can tell.

我目前正在使用VisualVM,但我遇到的问题是我无法保存它生成的图形。我需要报告一些有关其内存使用情况和运行时间的数据,尽管使用System.nanoTime(). 我也尝试过NetBeans 分析器,但它不是我想要的,因为我不是在寻找会减慢它的特定部分或任何东西,所以这有点矫枉过正。它最大的问题是它占用了太多的处理时间。也不允许我像 VisualVM 一样轻松捕获/传输数据,至少据我所知。

Ideally the best way to go about it would be some method call because then I'd be able to get the information a lot more easily, but anything like VisualVM that actually lets me save the graph is fine. Performance with VisualVM is pretty good too, compared to the NetBeans profiler, though I suppose that's because I wasn't using its profiler.

理想情况下,最好的方法是调用一些方法,因为那样我就可以更容易地获取信息,但是像 VisualVM 这样实际上可以让我保存图形的东西就很好。与 NetBeans 分析器相比,VisualVM 的性能也相当不错,但我想那是因为我没有使用它的分析器。

I'm currently using Ubuntu, but Windows 7 is fine. I'd rather have a program that specializes in doing this though, since the information gotten by programs who don't is likely to include the JVM and other things that would be better left out.

我目前使用的是 Ubuntu,但 Windows 7 很好。不过,我宁愿有一个专门从事此工作的程序,因为不这样做的程序获得的信息很可能包括 JVM 和其他最好忽略的信息。

Well, apparently, you can save snapshots of the current session and maximize the window in VisualVM, so you could make the charts bigger, take a snapshot and cut them... But that's kind of a hack. Better suggestions welcome.

嗯,显然,您可以保存当前会话的快照并最大化 VisualVM 中的窗口,这样您就可以使图表更大,拍摄快照并剪切它们......但这有点像黑客。欢迎提出更好的建议。

回答by Auxiliary

Runtime.getRuntime().freeMemory();

Runtime.getRuntime().totalMemory();

回答by Will Hartung

Look at the Runtimeclass. It has freeMemory, maxMemory, and totalMemory. That's probably close enough for your purposes.

查看运行时类。它有 freeMemory、maxMemory 和 totalMemory。这可能足以满足您的目的。

回答by u290629

You may prefer graceful method to measure memory, rather than hack image. JConsole is known to Monitor Applications by JMX,it provides program API. I guess it is what you need.

您可能更喜欢优雅的方法来衡量内存,而不是黑客图像。JConsole 以通过JMX监控应用程序而闻名,它提供程序 API。我想这就是你所需要的。

See: Using JConsole to Monitor Applications

请参阅:使用 JConsole 监视应用程序

回答by Amir Afghani

The HPjmeterconsole is free. Run your Java process with -Xloggc:<file>and open the <file>with it. Not only can you save your sessions, but you can compare runs. Other options to consider including in your command line are:

HPjmeter控制台是免费的。运行您的 Java 进程-Xloggc:<file><file>使用它打开。您不仅可以保存会话,还可以比较运行。要考虑包含在命令行中的其他选项是:

-XX:+PrintGCTimeStamps
-XX:+PrintGCDetails

回答by Gopi

Try JProfiler. Although its not free you can try evaluation version first.

试试JProfiler。虽然它不是免费的,但您可以先试用评估版。