如何在 Java 中检查 CPU 和内存使用情况?
我需要检查java中服务器的CPU和内存使用情况,有人知道如何做到吗?
I need to check CPU and memory usage for the server in java, anyone know how it could be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
对于Eclipse,您可以使用TPTP(测试和性能工具平台)来分析内存使用情况等。 更多信息
For Eclipse, you can use TPTP (Test and Performance Tools Platform) for analyse memory usage and etc. more information
我想在现有答案中添加一条注释:
这些方法仅跟踪 JVM 内存。 实际过程可能会消耗更多内存。
java.nio.ByteBuffer.allocateDirect() 是一个函数/库,很容易被错过,并且确实分配了本机内存,这不属于 Java 内存管理的一部分。
在 Linux 上,您可以使用类似的方法来获取实际消耗的内存: https://linuxhint.com/check_memory_usage_process_linux/< /a>
I want to add a note to the existing answers:
These methods only keep track of JVM Memory. The actual process may consume more memory.
java.nio.ByteBuffer.allocateDirect() is a function/library, that is easily missed, and indeed allocated native memory, that is not part of the Java memory management.
On Linux, you may use something like this to get the actually consumed memory: https://linuxhint.com/check_memory_usage_process_linux/
YourKit Java 分析器是一个出色的商业解决方案。 您可以在有关 CPU 分析 的文档中找到更多信息内存分析。
The YourKit Java profiler is an excellent commercial solution. You can find further information in the docs on CPU profiling and memory profiling.
如果您使用的是 Tomcat,请查看 Psi Probe,它可让您监控内部和外部环境外部存储器消耗以及许多其他领域。
If you are using Tomcat, check out Psi Probe, which lets you monitor internal and external memory consumption as well as a host of other areas.
以下是一些计算当前内存使用量(以兆字节为单位)的简单代码:
Here is some simple code to calculate the current memory usage in megabytes:
我还会添加以下方法来跟踪 CPU 负载:
您可以阅读更多内容 此处
I would also add the following way to track CPU Load:
You can read more here
如果您使用此处许多答案中发布的运行时/总内存解决方案(我已经做了很多次),如果您想要相当准确/一致的结果,请务必首先强制执行两次垃圾收集。
为了提高效率,Java 通常允许垃圾在强制 GC 之前填满所有内存,即使如此,它通常也不是完整的 GC,因此,runtime.freeMemory() 的结果始终介于“实际”可用内存量和 0 之间 第一次GC
并没有得到全部,而是得到了大部分。
好处是,如果您只执行 freeMemory() 调用,您将得到一个绝对无用且变化很大的数字,但如果首先执行 2 个 gc,则这是一个非常可靠的指标。 它还使例程变慢(可能是几秒)。
If you use the runtime/totalMemory solution that has been posted in many answers here (I've done that a lot), be sure to force two garbage collections first if you want fairly accurate/consistent results.
For effiency Java usually allows garbage to fill up all of memory before forcing a GC, and even then it's not usually a complete GC, so your results for runtime.freeMemory() always be somewhere between the "real" amount of free memory and 0.
The first GC doesn't get everything, it gets most of it.
The upswing is that if you just do the freeMemory() call you will get a number that is absolutely useless and varies widely, but if do 2 gc's first it is a very reliable gauge. It also makes the routine MUCH slower (seconds, possibly).
JConsole 是监视正在运行的 Java 应用程序的简单方法,您也可以使用探查器来获取有关您的应用程序的更多详细信息。 我喜欢使用 NetBeans Profiler 来实现此目的。
JConsole is an easy way to monitor a running Java application or you can use a Profiler to get more detailed information on your application. I like using the NetBeans Profiler for this.
从 Java 1.5 开始,JDK 附带了一个新工具: JConsole ,它可以显示任何 1.5 或更高版本 JVM 的 CPU 和内存使用情况。 它可以绘制这些参数的图表,导出到 CSV,显示加载的类数量、实例数量、死锁、线程等...
Since Java 1.5 the JDK comes with a new tool: JConsole wich can show you the CPU and memory usage of any 1.5 or later JVM. It can do charts of these parameters, export to CSV, show the number of classes loaded, the number of instances, deadlocks, threads etc...
Java 的 Runtime 对象可以报告JVM 的内存使用情况。 对于 CPU 消耗,您必须使用外部实用程序,例如 Unix 的 top 或 Windows 进程管理器。
Java's Runtime object can report the JVM's memory usage. For CPU consumption you'll have to use an external utility, like Unix's top or Windows Process Manager.
JMX,提供的 MXBeans(ThreadMXBean 等)将为您提供内存和 CPU 使用情况。
JMX, The MXBeans (ThreadMXBean, etc) provided will give you Memory and CPU usages.
对于内存使用情况,以下方法可行;
对于 CPU 使用情况,您需要使用外部应用程序来测量它。
For memory usage, the following will work,
For CPU usage, you'll need to use an external application to measure it.
来自此处
From here
如果您使用 Sun JVM,并且对应用程序的内部内存使用情况感兴趣(您的应用程序使用了多少分配的内存),我更喜欢打开 JVM 内置的垃圾收集日志记录。 您只需将 -verbose:gc 添加到启动命令中即可。
来自 Sun 文档:
有关详细信息,请参阅:http://java.sun.com/docs/热点/gc5.0/gc_tuning_5.html
If you are using the Sun JVM, and are interested in the internal memory usage of the application (how much out of the allocated memory your app is using) I prefer to turn on the JVMs built-in garbage collection logging. You simply add -verbose:gc to the startup command.
From the Sun documentation:
For more info see: http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html
如果您专门寻找 JVM 中的内存:
但是,这些应该仅作为估计值......
If you are looking specifically for memory in JVM:
However, these should be taken only as an estimate...