1

Is there a way to get info about cpu model (for unix system) in Java? I mean without using system commands like cat /proc/cpuinfo.

I want get something like: "Intel(R) Xeon(R) CPU E5-2640 0 @ 2.50GHz"

Thanks!

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Petr Marek
  • 59
  • 1
  • 7
  • 2
    Reading this file is the simplest way to do this. – Peter Lawrey Mar 07 '19 at 18:54
  • 1
    You can possibly use [JNI](https://www.protechtraining.com/blog/post/java-native-interface-jni-example-65) but why? – PM 77-1 Mar 07 '19 at 18:57
  • Yous don't have to *(...) using system commands like `cat /proc/cpuinfo`*. You may want to open `/proc/cpuinfo` just like any other file and read it until you get a line beginning with `model name`. – mszmurlo Mar 15 '19 at 14:35

4 Answers4

2

I think this question is a duplicata for this one get OS-level system information, however I'll re-post the best answer for that one.

You can get some limited memory information from the Runtime class. It really isn't exactly what you are looking for, but I thought I would provide it for the sake of completeness. Here is a small example. You can also get disk usage information from the java.io.File class. The disk space usage stuff requires Java 1.6 or higher.

public class Main {
  public static void main(String[] args) {
    /* Total number of processors or cores available to the JVM */
    System.out.println("Available processors (cores): " + 
        Runtime.getRuntime().availableProcessors());

    /* Total amount of free memory available to the JVM */
    System.out.println("Free memory (bytes): " + 
        Runtime.getRuntime().freeMemory());

    /* This will return Long.MAX_VALUE if there is no preset limit */
    long maxMemory = Runtime.getRuntime().maxMemory();
    /* Maximum amount of memory the JVM will attempt to use */
    System.out.println("Maximum memory (bytes): " + 
        (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));

    /* Total memory currently available to the JVM */
    System.out.println("Total memory available to JVM (bytes): " + 
        Runtime.getRuntime().totalMemory());

    /* Get a list of all filesystem roots on this system */
    File[] roots = File.listRoots();

    /* For each filesystem root, print some info */
    for (File root : roots) {
      System.out.println("File system root: " + root.getAbsolutePath());
      System.out.println("Total space (bytes): " + root.getTotalSpace());
      System.out.println("Free space (bytes): " + root.getFreeSpace());
      System.out.println("Usable space (bytes): " + root.getUsableSpace());
    }
  }
}

In your case you want the Runtime.getRuntime().availableProcessors().

There is another way, using sigar API. For that you need to download sigar from this link, and check this to include it in your project How to include SIGAR API in Java Project.

You will then use something like this:

import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

public class CpuInfo {
    public static void main(String[] args) throws SigarException {
        Sigar sigar = new Sigar();
        org.hyperic.sigar.CpuInfo[] cpuInfoList = sigar.getCpuInfoList();
        for(org.hyperic.sigar.CpuInfo info : cpuInfoList){
            System.out.println("CPU Model : " + info.getModel());
        }
    }
}
Lahcen YAMOUN
  • 657
  • 3
  • 15
0

If you want that level of detail, your best bet is to read the contents of /proc/cpuinfo and parse out the part you want.

Otherwise, from within a JVM, you can get a count of the number of processor cores

int count = Runtime.getRuntime().availableProcessors();

or OS architecture:

String arch = System.getProperty("os.arch");
Kaan
  • 5,434
  • 3
  • 19
  • 41
0

I would simply read /proc/cpuinfo.

String model = Files.lines(Paths.get("/proc/cpuinfo"))
   .filter(line -> line.startsWith("model name"))
   .map(line -> line.replaceAll(".*: ", ""))
   .findFirst().orElse("")
Michael
  • 1,044
  • 5
  • 9
  • Orange PIs (and possibly other PIs as well) don't have `model name`. They list what looks like the model name under `Processor` (with capital P): https://pastebin.com/FNJqgy29 – Mark Jeronimus Nov 13 '20 at 11:31
0

It seems Sigar mentioned in another answer is no longer maintained and does not work with recent Windows versions because of 64b/32b incompatibilites (see PR 142).

There seems to be another library for the purposes, which seems to be alive (last version published a few days ago) OSHI.

With OSHI you can get the information like this:

SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
CentralProcessor cpu = hal.getProcessor();
String name = cpu.getProcessorIdentifier().getName();
Suma
  • 33,181
  • 16
  • 123
  • 191