java中如何获取CPU和内存的使用情况呢?
下文笔者讲述Java中获取CPU及内存使用情况的方法分享,如下所示:
java获取 JVM的CPU及内存情况的示例
实现思路: 借助Runtime类即可获取Java运行时的CPU及内存情况例:
java获取 JVM的CPU及内存情况的示例
package com.java265.other; import java.text.NumberFormat; public class Test11 { /** * java265.com 示例演示 */ public static void main(String[] args) { /** 获取JVM 内存及CPU运行情况的方法分享 */ 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) + "\n"); sb.append("allocated memory: " + format.format(allocatedMemory / 1024) + "\n"); sb.append("max memory: " + format.format(maxMemory / 1024) + "\n"); sb.append("total free memory: " + format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024) + "\n"); System.out.println(sb); } } ------运行以上代码,将输出以下信息---- free memory: 257,595 allocated memory: 260,096 max memory: 4,130,816 total free memory: 4,128,315
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。