How can I find the number of cores available to my application from within Java code?
int cores = Runtime.getRuntime().availableProcessors();
If cores
is less than one, either your processor is about to die, or your JVM has a serious bug in it, or the universe is about to blow up.
Runtime
class is a String, I thought it would be helpful to show this one returns an int
. –
Conserve If you want to get number of physical cores you can run cmd and terminal command and then to parse the output to get info you need.Below is shown function that returns number of physical cores .
private int getNumberOfCPUCores() {
OSValidator osValidator = new OSValidator();
String command = "";
if(osValidator.isMac()){
command = "sysctl -n machdep.cpu.core_count";
}else if(osValidator.isUnix()){
command = "lscpu";
}else if(osValidator.isWindows()){
command = "cmd /C WMIC CPU Get /Format:List";
}
Process process = null;
int numberOfCores = 0;
int sockets = 0;
try {
if(osValidator.isMac()){
String[] cmd = { "/bin/sh", "-c", command};
process = Runtime.getRuntime().exec(cmd);
}else{
process = Runtime.getRuntime().exec(command);
}
} catch (IOException e) {
e.printStackTrace();
}
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
try {
while ((line = reader.readLine()) != null) {
if(osValidator.isMac()){
numberOfCores = line.length() > 0 ? Integer.parseInt(line) : 0;
}else if (osValidator.isUnix()) {
if (line.contains("Core(s) per socket:")) {
numberOfCores = Integer.parseInt(line.split("\\s+")[line.split("\\s+").length - 1]);
}
if(line.contains("Socket(s):")){
sockets = Integer.parseInt(line.split("\\s+")[line.split("\\s+").length - 1]);
}
} else if (osValidator.isWindows()) {
if (line.contains("NumberOfCores")) {
numberOfCores = Integer.parseInt(line.split("=")[1]);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
if(osValidator.isUnix()){
return numberOfCores * sockets;
}
return numberOfCores;
}
OSValidator class:
public class OSValidator {
private static String OS = System.getProperty("os.name").toLowerCase();
public static void main(String[] args) {
System.out.println(OS);
if (isWindows()) {
System.out.println("This is Windows");
} else if (isMac()) {
System.out.println("This is Mac");
} else if (isUnix()) {
System.out.println("This is Unix or Linux");
} else if (isSolaris()) {
System.out.println("This is Solaris");
} else {
System.out.println("Your OS is not support!!");
}
}
public static boolean isWindows() {
return (OS.contains("win"));
}
public static boolean isMac() {
return (OS.contains("mac"));
}
public static boolean isUnix() {
return (OS.contains("nix") || OS.contains("nux") || OS.contains("aix") );
}
public static boolean isSolaris() {
return (OS.contains("sunos"));
}
public static String getOS(){
if (isWindows()) {
return "win";
} else if (isMac()) {
return "osx";
} else if (isUnix()) {
return "uni";
} else if (isSolaris()) {
return "sol";
} else {
return "err";
}
}
}
This is an additional way to find out the number of CPU cores (and a lot of other information), but this code requires an additional dependence:
Native Operating System and Hardware Information https://github.com/oshi/oshi
SystemInfo systemInfo = new SystemInfo();
HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware();
CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor();
Get the number of logical CPUs available for processing:
centralProcessor.getLogicalProcessorCount();
If you want to dubbel check the amount of cores you have on your machine to the number your java program is giving you.
In Linux terminal: lscpu
In Windows terminal (cmd): echo %NUMBER_OF_PROCESSORS%
In Mac terminal: sysctl -n hw.ncpu
%NUMBER_OF_PROCESSORS%
outputs number of logical cores –
Mickeymicki This works on Windows with Cygwin installed:
System.getenv("NUMBER_OF_PROCESSORS")
groovy -e "println System.getenv('NUMBER_OF_PROCESSORS')"
–
Gladiate set NUMBER_OF_PROCESSORS
works from the Windows command line for me. –
Gladiate © 2022 - 2024 — McMap. All rights reserved.