当前位置:   article > 正文

java读取计算机CPU、内存、硬盘等信息_如何用java调用电脑的cpu属性

如何用java调用电脑的cpu属性

参考文章:Java获取系统内存、CPU、磁盘等信息

pom.xml 

  1. <dependency>
  2. <groupId>com.github.oshi</groupId>
  3. <artifactId>oshi-core</artifactId>
  4. <version>3.12.2</version>
  5. </dependency>

java代码:

  1. package org.fiend.utils.sysUtil;
  2. import com.sun.management.OperatingSystemMXBean;
  3. import oshi.SystemInfo;
  4. import oshi.hardware.CentralProcessor;
  5. import java.io.File;
  6. import java.lang.management.ManagementFactory;
  7. import java.lang.management.MemoryMXBean;
  8. import java.lang.management.MemoryUsage;
  9. import java.text.DecimalFormat;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Date;
  12. import java.util.concurrent.Executors;
  13. import java.util.concurrent.TimeUnit;
  14. /**
  15. * @author langpf 2020-08-12 17:59:01
  16. */
  17. public class OSUtils {
  18. public static void main(String[] args) {
  19. System.out.println("");
  20. init();
  21. }
  22. public static void init() {
  23. Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
  24. try {
  25. SystemInfo systemInfo = new SystemInfo();
  26. OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
  27. MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
  28. /* ============================== 操作系统信息 ============================= */
  29. System.err.println("");
  30. System.err.println("");
  31. System.err.println("============================== 操作系统信息 =============================");
  32. // 操作系统
  33. String osName = System.getProperty("os.name");
  34. // 获得线程总数
  35. ThreadGroup parentThread;
  36. parentThread = Thread.currentThread().getThreadGroup();
  37. while (parentThread.getParent() != null) {
  38. parentThread = parentThread.getParent();
  39. }
  40. int totalThread = parentThread.activeCount();
  41. System.err.println("操作系统: " + osName);
  42. System.err.println("程序启动时间: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
  43. .format(new Date(ManagementFactory.getRuntimeMXBean().getStartTime())));
  44. System.err.println("pid: " + System.getProperty("PID"));
  45. System.err.println("总线程数: " + totalThread);
  46. // 磁盘使用情况
  47. File[] files = File.listRoots();
  48. for (File file : files) {
  49. String total = new DecimalFormat("#.#").format(file.getTotalSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
  50. String free = new DecimalFormat("#.#").format(file.getFreeSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
  51. String un = new DecimalFormat("#.#").format(file.getUsableSpace() * 1.0 / 1024 / 1024 / 1024) + "G";
  52. String path = file.getPath();
  53. System.err.println(path + "总: " + total + ",可用空间: " + un + ",空闲空间: " + free);
  54. // System.err.println("=============================================");
  55. System.err.println("---------------------------------------------");
  56. }
  57. /* ============================== 堆内存信息 ============================= */
  58. System.err.println("");
  59. System.err.println("");
  60. System.err.println("============================== 堆内存信息 =============================");
  61. // 椎内存使用情况
  62. MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage();
  63. // 初始的总内存(B)
  64. long initTotalMemorySize = memoryUsage.getInit();
  65. // 最大可用内存(B)
  66. long maxMemorySize = memoryUsage.getMax();
  67. // 已使用的内存(B)
  68. long usedMemorySize = memoryUsage.getUsed();
  69. System.err.println(
  70. "初始的总内存(JVM): " + new DecimalFormat("#.#").format(initTotalMemorySize * 1.0 / 1024 / 1024) + "M");
  71. System.err.println(
  72. "最大可用内存(JVM): " + new DecimalFormat("#.#").format(maxMemorySize * 1.0 / 1024 / 1024) + "M");
  73. System.err.println(
  74. "已使用的内存(JVM): " + new DecimalFormat("#.#").format(usedMemorySize * 1.0 / 1024 / 1024) + "M");
  75. /* ============================== 物理内存信息 ============================= */
  76. System.err.println("");
  77. System.err.println("");
  78. System.err.println("============================== 物理内存信息 =============================");
  79. // 总的物理内存(B)
  80. long totalMemorySizeByte = osmxb.getTotalPhysicalMemorySize();
  81. long totalMemorySizeByte2 = systemInfo.getHardware().getMemory().getTotal();
  82. // 总的物理内存
  83. String totalMemorySize =
  84. new DecimalFormat("#.##").format(totalMemorySizeByte / 1024.0 / 1024 / 1024) + "G";
  85. String totalMemorySize2 =
  86. new DecimalFormat("#.##").format(totalMemorySizeByte2 / 1024.0 / 1024 / 1024) + "G";
  87. // 剩余的物理内存(B)
  88. long freePhysicalMemorySizeByte = osmxb.getFreePhysicalMemorySize();
  89. long freePhysicalMemorySizeByte2 = systemInfo.getHardware().getMemory().getAvailable();
  90. // 剩余的物理内存
  91. String freePhysicalMemorySize =
  92. new DecimalFormat("#.##").format(freePhysicalMemorySizeByte / 1024.0 / 1024 / 1024) + "G";
  93. String freePhysicalMemorySize2 =
  94. new DecimalFormat("#.##").format(freePhysicalMemorySizeByte2 * 1.0 / 1024 / 1024 / 1024) + "G";
  95. // 已使用的物理内存
  96. String usedMemory =
  97. new DecimalFormat("#.##").format((totalMemorySizeByte - freePhysicalMemorySizeByte) / 1024.0 / 1024 / 1024) + "G";
  98. String usedMemory2 =
  99. new DecimalFormat("#.##").format((totalMemorySizeByte2 - freePhysicalMemorySizeByte2) * 1.0 / 1024 / 1024 / 1024) + "G";
  100. String memoryUsedRate = new DecimalFormat("#.##%").format(1 - (freePhysicalMemorySizeByte * 1.0 / totalMemorySizeByte));
  101. System.err.println("总的物理内存: " + totalMemorySize);
  102. System.err.println("总的物理内存: " + totalMemorySize2);
  103. System.err.println("剩余的物理内存: " + freePhysicalMemorySize);
  104. System.err.println("剩余的物理内存: " + freePhysicalMemorySize2);
  105. System.err.println("已使用的物理内存: " + usedMemory);
  106. System.err.println("已使用的物理内存: " + usedMemory2);
  107. System.err.println("物理内存使用率: " + memoryUsedRate);
  108. /* ============================== JVM信息 ============================= */
  109. System.err.println("");
  110. System.err.println("");
  111. System.err.println("============================== JVM信息 =============================");
  112. System.err.println("JAVA_HOME: " + System.getProperty("java.home"));
  113. System.err.println("JAVA_VERSION: " + System.getProperty("java.version"));
  114. System.err.println("USER_HOME: " + System.getProperty("user.home"));
  115. System.err.println("USER_NAME: " + System.getProperty("user.name"));
  116. /* ============================== CPU信息 ============================= */
  117. System.err.println("");
  118. System.err.println("");
  119. System.err.println("============================== CPU信息 =============================");
  120. printlnCpuInfo(systemInfo);
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. }
  124. }, 0, 5, TimeUnit.SECONDS);
  125. }
  126. /**
  127. * 打印 CPU 信息
  128. * @param systemInfo s
  129. */
  130. private static void printlnCpuInfo(SystemInfo systemInfo) throws InterruptedException {
  131. CentralProcessor processor = systemInfo.getHardware().getProcessor();
  132. long[] prevTicks = processor.getSystemCpuLoadTicks();
  133. // 睡眠1s
  134. TimeUnit.SECONDS.sleep(1);
  135. long[] ticks = processor.getSystemCpuLoadTicks();
  136. long nice = ticks[CentralProcessor.TickType.NICE.getIndex()]
  137. - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
  138. long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()]
  139. - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
  140. long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()]
  141. - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
  142. long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()]
  143. - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
  144. long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()]
  145. - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
  146. long user = ticks[CentralProcessor.TickType.USER.getIndex()]
  147. - prevTicks[CentralProcessor.TickType.USER.getIndex()];
  148. long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()]
  149. - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
  150. long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()]
  151. - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
  152. long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
  153. System.err.println("cpu核数: " + Runtime.getRuntime().availableProcessors());
  154. System.err.println("cpu核数: " + processor.getLogicalProcessorCount());
  155. System.err.println("cpu系统使用率: " + new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));
  156. System.err.println("cpu用户使用率: " + new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));
  157. System.err.println("cpu当前等待率: " + new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));
  158. System.err.println("cpu当前空闲率: " + new DecimalFormat("#.##%").format(idle * 1.0 / totalCpu));
  159. System.err.format("CPU load: %.1f%% (counting ticks)%n", processor.getSystemCpuLoadBetweenTicks() * 100);
  160. System.err.format("CPU load: %.1f%% (OS MXBean)%n", processor.getSystemCpuLoad() * 100);
  161. }
  162. }

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/article/detail/58840
推荐阅读
相关标签
  

闽ICP备14008679号