gpt4 book ai didi

java - 检测CPU型号信息

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:32 24 4
gpt4 key购买 nike

有没有办法在 Java 中获取有关 cpu 模型(对于 unix 系统)的信息?我的意思是不使用像 cat/proc/cpuinfo 这样的系统命令。

我想要这样的东西:“Intel(R) Xeon(R) CPU E5-2640 0 @ 2.50GHz”

谢谢!

最佳答案

我认为这个问题是这个问题的重复get OS-level system information ,不过我会重新发布该答案的最佳答案。

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

在您的情况下,您需要 Runtime.getRuntime().availableProcessors()

还有另一种方法,使用 sigar API。为此,您需要从 this link 下载 sigar , 并选中它以将其包含在您的项目中 How to include SIGAR API in Java Project .

然后你将使用这样的东西:

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

关于java - 检测CPU型号信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55050864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com