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

package mkd.Utils;

import java.io.File;

import java.text.NumberFormat;

public class systemInfo {

private Runtime runtime = Runtime.getRuntime();

public String Info() {

StringBuilder sb = new StringBuilder();

sb.append(this.OsInfo());

sb.append(this.MemInfo());

sb.append(this.DiskInfo());

return sb.toString();

}

public String OSname() {

return System.getProperty("os.name");

}

public String OSversion() {

return System.getProperty("os.version");

}

public String OsArch() {

return System.getProperty("os.arch");

}

public long totalMem() {

return Runtime.getRuntime().totalMemory();

}

public long usedMem() {

return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();

}

public String MemInfo() {

NumberFormat format = NumberFormat.getInstance();

StringBuilder sb = new StringBuilder();

long maxMemory = runtime.maxMemory();

long allocatedMemory = runtime.totalMemory();

long freeMemory = runtime.freeMemory();

sb.append("Free memory: ");

sb.append(format.format(freeMemory / 1024));

sb.append("
");

sb.append("Allocated memory: ");

sb.append(format.format(allocatedMemory / 1024));

sb.append("
");

sb.append("Max memory: ");

sb.append(format.format(maxMemory / 1024));

sb.append("
");

sb.append("Total free memory: ");

sb.append(format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024));

sb.append("
");

return sb.toString();

}

public String OsInfo() {

StringBuilder sb = new StringBuilder();

sb.append("OS: ");

sb.append(this.OSname());

sb.append("
");

sb.append("Version: ");

sb.append(this.OSversion());

sb.append("
");

sb.append(": ");

sb.append(this.OsArch());

sb.append("
");

sb.append("Available processors (cores): ");

sb.append(runtime.availableProcessors());

sb.append("
");

return sb.toString();

}

public String DiskInfo() {

/* Get a list of all filesystem roots on this system */

File[] roots = File.listRoots();

StringBuilder sb = new StringBuilder();

/* For each filesystem root, print some info */

for (File root : roots) {

sb.append("File system root: ");

sb.append(root.getAbsolutePath());

sb.append("
");

sb.append("Total space (bytes): ");

sb.append(root.getTotalSpace());

sb.append("
");

sb.append("Free space (bytes): ");

sb.append(root.getFreeSpace());

sb.append("
");

sb.append("Usable space (bytes): ");

sb.append(root.getUsableSpace());

sb.append("
");

}

return sb.toString();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值