package main;
import com.sun.management.OperatingSystemMXBean;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Test {
public static void main(String[] args) throws Exception {
createThread(10); // 建立 10 條 執行緒
while (true) {
getData();
System.out.println("----------------------------------------------");
Thread.sleep(1000);
}
}
static void getData() {
Runtime lRuntime = Runtime.getRuntime();
long freeM = lRuntime.freeMemory() / 1024 / 1024;
long maxM = lRuntime.maxMemory() / 1024 / 1024;
long tM = lRuntime.totalMemory() / 1024 / 1024;
OperatingSystemMXBean osmb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
java.lang.management.ThreadMXBean t = ManagementFactory.getThreadMXBean();
File[] roots = File.listRoots(); // 取得硬碟分區
System.out.println("本程式執行緒數量:" + t.getThreadCount());
System.out.println("JVM 執行緒數量:" + t.getDaemonThreadCount());
System.out.println("最大執行緒數量:" + t.getPeakThreadCount());
System.out.println();
System.out.println("本程式最大記憶體:" + maxM + " MB");
System.out.println("本程式可用記憶體:" + freeM + " MB");
System.out.println("本程式總記憶體:" + tM + " MB");
System.out.println();
System.out.println("系統物理記憶體總計:" + (osmb.getTotalPhysicalMemorySize() / 1024 / 1024) + " MB");
System.out.println("系統物理記憶體可用:" + (osmb.getFreePhysicalMemorySize() / 1024 / 1024) + " MB");
System.out.println("系統交換記憶體總計:" + (osmb.getTotalSwapSpaceSize() / 1024 / 1024) + " MB");
System.out.println("系統交換記憶體可用:" + (osmb.getFreeSwapSpaceSize() / 1024 / 1024) + " MB");
System.out.println();
System.out.println("CPU 使用率(本程式):" + (osmb.getProcessCpuLoad() * 100) + " %");
System.out.println("CPU 使用率(系統):" + (osmb.getSystemCpuLoad() * 100) + " %");
System.out.println("CPU 時間:" + osmb.getProcessCpuTime());
System.out.println();
System.out.println("系统磁盘使用情况:");
for (File file : roots) {
System.out.print(file.getPath());
System.out.print(" 未使用:" + (file.getFreeSpace() / 1024 / 1024 / 1024) + " GB \t");
System.out.print(" 可用:" + (file.getUsableSpace() / 1024 / 1024 / 1024) + " GB \t");
System.out.print(" 總空間:" + (file.getTotalSpace() / 1024 / 1024 / 1024) + " GB \t");
System.out.println();
}
}
static void createThread(int count) {
for (int i = 0; i < count; i++) {
new Thread(() -> {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}
}).start();
}
}
}
執行結果:
----------------------------------------------
本程式執行緒數量:15
JVM 執行緒數量:4
最大執行緒數量:15
本程式最大記憶體:910 MB
本程式可用記憶體:117 MB
本程式總記憶體:123 MB
系統物理記憶體總計:16381 MB
系統物理記憶體可用:10548 MB
系統交換記憶體總計:16381 MB
系統交換記憶體可用:7890 MB
CPU 使用率(本程式):0.7654192920344272 %
CPU 使用率(系統):22.003632832205554 %
CPU 時間:765625000
系统磁盘使用情况:
C:\ 未使用:84 GB 可用:84 GB 總空間:111 GB
D:\ 未使用:3 GB 可用:3 GB 總空間:931 GB
E:\ 未使用:916 GB 可用:916 GB 總空間:2794 GB
F:\ 未使用:1 GB 可用:1 GB 總空間:74 GB
H:\ 未使用:0 GB 可用:0 GB 總空間:29 GB
----------------------------------------------
留言列表