Package io.github.connellite.system
Class OSystem
java.lang.Object
io.github.connellite.system.OSystem
Snapshot helpers for the operating system, JVM memory, and CPU metrics.
CPU load percentages use the HotSpot extension OperatingSystemMXBean
when available; otherwise those methods return -1. Values from
OperatingSystemMXBean.getProcessCpuLoad() and
OperatingSystemMXBean.getCpuLoad() may be -1.0 at runtime
when the JVM cannot measure them yet.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordOne filesystem root as returned bylistDisks(). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intReturns the number of logical CPU cores visible to the JVM.static doublestatic intstatic longReturns currently free JVM heap bytes.static longReturns the maximum heap size configured for the current JVM.static longReturns total heap bytes currently allocated by the JVM.static doublestatic Stringstatic Stringstatic Stringstatic intstatic booleanisMac()Checks whether the running OS appears to be macOS / Darwin.static booleanisUnix()Checks whether the running OS appears to be Unix-like (Linux, AIX, *BSD, Solaris, …), not Windows or macOS.static booleanChecks whether the running OS appears to be Windows.static List<OSystem.Disk>Logical volume roots reported by the JVM (File.listRoots()).static StringBest-effort CPU model detection for the current OS.
-
Constructor Details
-
OSystem
public OSystem()
-
-
Method Details
-
listDisks
Logical volume roots reported by the JVM (File.listRoots()). -
isWindows
public static boolean isWindows()Checks whether the running OS appears to be Windows.- Returns:
truewhenos.namestarts with"Windows"
-
isMac
public static boolean isMac()Checks whether the running OS appears to be macOS / Darwin.- Returns:
truewhenos.namestarts with"Mac"or"Darwin"
-
isUnix
public static boolean isUnix()Checks whether the running OS appears to be Unix-like (Linux, AIX, *BSD, Solaris, …), not Windows or macOS.Uses prefix matching on
os.name, in the same style as Commons LangSystemUtils(not a substring search for"nix"/"nux"). -
getOsVersion
- Returns:
- value of the
os.versionsystem property
-
getCpuCores
public static int getCpuCores()Returns the number of logical CPU cores visible to the JVM.- Returns:
- logical processor count
-
getOsName
- Returns:
os.namein lower case
-
getOsType
- Returns:
"win","mac","nix", ornullif none match
-
getJvmFreeMemory
public static long getJvmFreeMemory()Returns currently free JVM heap bytes.- Returns:
- free heap size in bytes
-
getJvmMaxMemory
public static long getJvmMaxMemory()Returns the maximum heap size configured for the current JVM.- Returns:
- max heap size in bytes
-
getJvmTotalMemory
public static long getJvmTotalMemory()Returns total heap bytes currently allocated by the JVM.- Returns:
- allocated heap size in bytes
-
getHeapMemoryUsageMb
public static double getHeapMemoryUsageMb()- Returns:
- heap used in megabytes (mebibytes: 1024² bytes), rounded to two decimal places
-
getNonHeapMemoryUsageMb
public static double getNonHeapMemoryUsageMb()- Returns:
- non-heap used in megabytes (mebibytes: 1024² bytes), rounded to two decimal places
-
getJvmCpuLoadPercent
public static int getJvmCpuLoadPercent()- Returns:
- JVM process CPU load as a percentage 0–100, or
-1if unavailable
-
getSystemCpuLoadPercent
public static int getSystemCpuLoadPercent()- Returns:
- whole-system CPU load as a percentage 0–100, or
-1if unavailable
-
tryGetProcessorName
Best-effort CPU model detection for the current OS.Uses OS-specific sources: Linux via
/proc/cpuinfo, Windows viareg query, and macOS viasysctl. If detection fails or the OS is unsupported, returns"unknown".- Returns:
- detected processor name, or
"unknown"when unavailable
-