一尘不染

如何检查Java中的CPU和内存使用情况?

java

我需要在Java中检查服务器的CPU和内存使用情况,有人知道怎么做吗?


阅读 518

收藏
2020-03-21

共1个答案

一尘不染

如果你正在专门寻找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/>");

但是,这些仅应作为估计…

2020-03-21