简体   繁体   English

如何监视Java程序的内存使用情况?

[英]how to monitor the memory usage of a java program?

I just proposed an algorithm, and I want to prove its superiority compared with another algorithm in terms of time&space consumption. 我只是提出了一种算法,我想证明其在时间和空间消耗方面与另一种算法相比的优越性。 I will implement my algorithm in Java, does anyone know how to monitor the memory consumption of a Java program when the program is executing? 我将用Java实现算法,有人知道如何在执行程序时监视Java程序的内存消耗吗? Thanks. 谢谢。

from JDK 6 onwards you have a tool in the bin directory which monitors CPU, memory consumption as well as the number of threads spawned. 从JDK 6开始,您在bin目录中有一个工具可以监视CPU,内存消耗以及产生的线程数。 its called visualvm . 它叫做visualvm Just start it and then start your java process. 只需启动它,然后启动您的Java进程即可。 You will see the java process in the tool on the left hand side. 您将在左侧的工具中看到Java进程。 Double click on the process and view the statistics. 双击该进程并查看统计信息。 Hope this helps. 希望这可以帮助。 :) :)

While the tools mentioned in other answers will tell you how much memory Java allocated to your heap, this can only be taken as an upper bound on your program's memory consumption - the JVM does not garbage collect objects in the moment they're no longer needed, it can even increase heap size instead of garbage collecting and only perform garbage collection when it reaches the given heap limit ( -Xmx ). 虽然其他答案中提到的工具会告诉您Java分配给堆的内存量,但这只能作为程序内存消耗的上限 -JVM不会在不再需要对象时进行垃圾收集,它甚至可以增加堆大小而不是进行垃圾收集,并且仅在达到给定的堆限制( -Xmx )时才执行垃圾收集。

So, the graphs you'll see may reach much higher than what the program really needs. 因此,您将看到的图形可能会超出程序真正需要的图形。

One way to deal with this is to lower the heap size limit ( -Xmx ) incrementally until the program crashes on OutOfMemory exception. 解决此问题的一种方法是逐渐降低堆大小限制( -Xmx ),直到程序因OutOfMemory异常而崩溃为止。

Another way can be used if you know at which point in your algorithm it will consume the most memory - place a breakpoint at that point, make a heap dump and then examine it to see how much living objects occupy. 如果您知道在算法的哪一点将消耗最多的内存,可以使用另一种方法-在该点放置一个断点,进行堆转储,然后检查它以查看有多少活动对象被占用。

You can use 您可以使用

long memory = runtime.totalMemory() - runtime.freeMemory();

This place also gives you runtime so that might be pretty useful for what you're trying to do as well! 这个地方还为您提供了运行时,因此对于您尝试做的事情也可能非常有用! http://www.vogella.com/tutorials/JavaPerformance/article.html#runtimeinfo http://www.vogella.com/tutorials/JavaPerformance/article.html#runtimeinfo

Answer is found with a quick Google search. 通过快速的Google搜索即可找到答案。

Use the totalMemory( ) and freeMemory( ) methods [...] The java.lang.Runtime.totalMemory() 使用totalMemory()和freeMemory()方法[...] java.lang.Runtime.totalMemory()

from http://crunchify.com/java-runtime-get-free-used-and-total-memory-in-java/ 来自http://crunchify.com/java-runtime-get-free-used-and-total-memory-in-java/

so 所以

long totalmem = java.lang.Runtime.totalMemory();
long freemem  = java.lang.Runtime.freeMemory();
long consump  = totalmem - freemem;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM