如何检查 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...
相关文章