Best way to measure Memory Usage of a Java Program?

我们两清 提交于 2019-12-21 12:54:42

问题


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.


回答1:


Runtime.getRuntime().freeMemory();

Runtime.getRuntime().totalMemory();



回答2:


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




回答3:


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




回答4:


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




回答5:


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:

-XX:+PrintGCTimeStamps
-XX:+PrintGCDetails


来源:https://stackoverflow.com/questions/3536149/best-way-to-measure-memory-usage-of-a-java-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!