Class OSystem

java.lang.Object
io.github.connellite.system.OSystem

public class OSystem extends Object
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.

  • Constructor Details

    • OSystem

      public OSystem()
  • Method Details

    • listDisks

      public static List<OSystem.Disk> 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:
      true when os.name starts with "Windows"
    • isMac

      public static boolean isMac()
      Checks whether the running OS appears to be macOS / Darwin.
      Returns:
      true when os.name starts 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 Lang SystemUtils (not a substring search for "nix"/"nux").

    • getOsVersion

      public static String getOsVersion()
      Returns:
      value of the os.version system property
    • getCpuCores

      public static int getCpuCores()
      Returns the number of logical CPU cores visible to the JVM.
      Returns:
      logical processor count
    • getOsName

      public static String getOsName()
      Returns:
      os.name in lower case
    • getOsType

      public static String getOsType()
      Returns:
      "win", "mac", "nix", or null if 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 -1 if unavailable
    • getSystemCpuLoadPercent

      public static int getSystemCpuLoadPercent()
      Returns:
      whole-system CPU load as a percentage 0–100, or -1 if unavailable
    • tryGetProcessorName

      public static String tryGetProcessorName()
      Best-effort CPU model detection for the current OS.

      Uses OS-specific sources: Linux via /proc/cpuinfo, Windows via reg query, and macOS via sysctl. If detection fails or the OS is unsupported, returns "unknown".

      Returns:
      detected processor name, or "unknown" when unavailable