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!
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!
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());
}
}
}
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");
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("")
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();