我需要在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 和内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!