小编典典

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

java

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


阅读 551

收藏
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