如何在 Java 中检查 CPU 和内存使用情况? [英] How do I check CPU and Memory Usage in Java?
本文介绍了如何在 Java 中检查 CPU 和内存使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要用java检查服务器的CPU和内存使用情况,有人知道怎么做吗?
I need to check CPU and memory usage for the server in java, anyone know how it could be done?
推荐答案
如果您专门寻找 JVM 中的内存:
If you are looking specifically for memory in JVM:
Runtime runtime = Runtime.getRuntime();
NumberFormat format = NumberFormat.getInstance();
StringBuilder sb = new StringBuilder();
long maxMemory = runtime.maxMemory();
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
sb.append("free memory: " + format.format(freeMemory / 1024) + "<br/>");
sb.append("allocated memory: " + format.format(allocatedMemory / 1024) + "<br/>");
sb.append("max memory: " + format.format(maxMemory / 1024) + "<br/>");
sb.append("total free memory: " + format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024) + "<br/>");
然而,这些只能作为估计......
However, these should be taken only as an estimate...
这篇关于如何在 Java 中检查 CPU 和内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文