测量 Java 程序内存使用情况的最佳方法?
我目前正在使用 VisualVM,但我遇到的问题是我无法保存它生成的图表。我需要报告一些有关其内存使用情况和运行时间的数据,尽管使用 System.nanoTime() 很容易获得运行时间。我还尝试过 NetBeans 分析器,但这不是我想要的,因为我不是在寻找会减慢速度的特定部分或任何东西,所以这有点过分了。它最大的问题是它消耗了太多的处理时间。也不允许我像 VisualVM 那样轻松捕获/传输数据,至少据我所知。
理想情况下,最好的方法是调用一些方法,因为这样我就可以更轻松地获取信息,但是像 VisualVM 这样实际上可以让我保存图形的东西都可以。与 NetBeans 分析器相比,VisualVM 的性能也相当不错,尽管我想这是因为我没有使用它的分析器。
我目前使用的是 Ubuntu,但 Windows 7 也不错。不过,我宁愿有一个专门做这件事的程序,因为不这样做的程序获得的信息可能包括 JVM 和其他最好不要的东西。
嗯,显然,您可以保存当前会话的快照并最大化 VisualVM 中的窗口,这样您就可以使图表更大,拍摄快照并剪切它们......但这有点像黑客。欢迎更好的建议。
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 profiler but 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.
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.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
查看 Runtime 类。它有空闲内存、最大内存和总内存。对于您的目的来说,这可能已经足够接近了。
Look at the Runtime class. It has freeMemory, maxMemory, and totalMemory. That's probably close enough for your purposes.
您可能更喜欢优雅的方法来测量内存,而不是黑客图像。
JConsole以JMX方式监控应用程序,它提供程序API。我想这就是你所需要的。
请参阅:使用 JConsole 监控应用程序
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.
See: Using JConsole to Monitor Applications
尝试 JProfiler。虽然它不是免费的,但您可以先尝试评估版。
Try JProfiler. Although its not free you can try evaluation version first.
HPjmeter 控制台是免费的。使用
-Xloggc:
运行 Java 进程并用它打开
。您不仅可以保存训练,还可以比较跑步情况。需要考虑在命令行中包含的其他选项包括:The HPjmeter console 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: