Quantcast
Channel: プログラム の個人的なメモ
Viewing all articles
Browse latest Browse all 860

【Java】Java から PC の CPU情報を取得する方法を考える

$
0
0

【1】コマンドを実行した場合

Linuxの場合
 * vmstatコマンドでJavaコードから実行し、CPU情報を抽出する
* vmstatコマンドについては、以下の関連記事を参照のこと。
https://blogs.yahoo.co.jp/dk521123/37259054.html

サンプル

参考文献 
https://stackoverflow.com/questions/6284384/get-memory-and-cpu-usage

【2】com.sun.management.OperatingSystemMXBean を使った方法

サンプル

import java.lang.management.ManagementFactory;

public class Main {
  @SuppressWarnings("restriction")
  public static void main(String[] args) {
    com.sun.management.OperatingSystemMXBean operatingSystemMXBean =
        (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
     System.out.println("ProcessCpuLoad : " + operatingSystemMXBean.getProcessCpuLoad());
     System.out.println("ProcessCpuTime : " + operatingSystemMXBean.getProcessCpuTime());
     System.out.println("SystemCpuTime : " + operatingSystemMXBean.getSystemCpuLoad());
  }
}
出力結果
ProcessCpuLoad : -1.0
ProcessCpuTime : 421875000
SystemCpuTime : -1.0
参考文献
https://stackoverflow.com/questions/36647405/java-api-to-get-cpu-memory-usage-of-my-java-application

【3】MBeanServer を使った方法

サンプル

import java.lang.management.ManagementFactory;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;

public class Main {
  public static void main(String[] args) throws Exception {
    printCpuInfo();
  }

  public static void printCpuInfo()
      throws InstanceNotFoundException, ReflectionException, MalformedObjectNameException, NullPointerException {
    MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
    ObjectName objectName = ObjectName.getInstance("java.lang:type=OperatingSystem");
    AttributeList attributeList = mbeanServer.getAttributes(objectName,
        new String[] { "ProcessCpuLoad", "ProcessCpuTime", "SystemCpuLoad" });

    if (attributeList.isEmpty()) {
      System.out.println("attributeList is Empty");
    }

    for (Attribute attribute : attributeList.asList()) {
      System.out.println(attribute.getName() + " : " + attribute.getValue());
    }
  }
}
出力結果
ProcessCpuLoad : 0.37238200554819395
ProcessCpuTime : 578125000
SystemCpuLoad : -1.0
参考文献
https://stackoverflow.com/questions/18489273/how-to-get-percentage-of-cpu-usage-of-os-from-java

別解
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class Main {
  public static void main(String[] args) {
    printUsage();
  }

  private static void printUsage() {
    OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
    for (Method method : operatingSystemMXBean.getClass().getDeclaredMethods()) {
      method.setAccessible(true);
      if (method.getName().startsWith("get") && Modifier.isPublic(method.getModifiers())) {
        Object value;
        try {
          value = method.invoke(operatingSystemMXBean);
        } catch (Exception ex) {
          value = ex;
        }
        System.out.println(method.getName() + " = " + value);
      }
    }
  }
}
出力結果
getCommittedVirtualMemorySize = 236544000
getFreePhysicalMemorySize = 2628100096
getFreeSwapSpaceSize = 2142429184
getProcessCpuLoad = -1.0
getProcessCpuTime = 515625000
getSystemCpuLoad = -1.0
getTotalPhysicalMemorySize = 8491581440
getTotalSwapSpaceSize = 10035085312
参考文献
https://stackoverflow.com/questions/47177/how-do-i-monitor-the-computers-cpu-memory-and-disk-usage-in-java

関連記事

【Java】PCリソース情報(ディスク容量、JVMメモリ容量、CPU情報)を取得する

https://blogs.yahoo.co.jp/dk521123/7968881.html

【Java】 外部プログラム/コマンドを実行するには ~ ProcessBuilder ~

https://blogs.yahoo.co.jp/dk521123/34842155.html

【Java】JMX (Java Management Extensions) / MBean (Management Bean)

https://blogs.yahoo.co.jp/dk521123/34391290.html

Viewing all articles
Browse latest Browse all 860

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>