Class SystemInfo
Provides detailed CPU, memory, disk, OS, and JVM runtime information
beyond what Environment offers.
All snapshot methods return immutable records that are safe to cache and share
across threads.
提供比 Environment 更详细的 CPU、内存、磁盘、
操作系统和 JVM 运行时信息。所有快照方法返回不可变记录,可安全缓存和跨线程共享。
Features | 主要功能:
- CPU load and processor info - CPU 负载和处理器信息
- JVM heap/non-heap and physical memory metrics - JVM 堆/非堆及物理内存指标
- Disk/file-store capacity and usage - 磁盘/文件存储容量和使用情况
- OS identification and hostname - 操作系统识别和主机名
- JVM runtime details (version, uptime, PID, arguments) - JVM 运行时详情
Usage Examples | 使用示例:
// CPU information
CpuInfo cpu = SystemInfo.cpu();
double load = SystemInfo.cpuLoad();
// Memory information
MemoryInfo heap = SystemInfo.heapMemory();
long physMem = SystemInfo.physicalMemoryTotal();
// Disk information
List<DiskInfo> disks = SystemInfo.disks();
DiskInfo root = SystemInfo.disk(Path.of("/"));
// OS and runtime
String host = SystemInfo.hostname();
RuntimeInfo rt = SystemInfo.runtime();
Thread Safety | 线程安全:
All methods are stateless and thread-safe. Returned records are immutable.
所有方法都是无状态且线程安全的。返回的记录是不可变的。
- Since:
- JDK 25, opencode-base-core V1.0.3
- Author:
- Leon Soo
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic CpuInfocpu()Returns a snapshot of current CPU information.static doublecpuLoad()Returns the current system-wide CPU load as a value in [0.0, 1.0], or -1.0 if not available.static DiskInfoReturns disk information for the file store containing the given path.static longReturns the free (unallocated) disk space for the given path, in bytes.disks()Returns information for all file stores (disks/partitions).static longReturns the total disk space for the given path, in bytes.static longdiskUsable(Path path) Returns the usable disk space for the given path, in bytes.Returns an unmodifiable view of the system environment variables.static MemoryInfoReturns a snapshot of JVM heap memory information.static Stringhostname()Returns the machine hostname.static double[]Returns the system load averages for 1, 5, and 15 minutes.static MemoryInfomemory()Returns a snapshot of physical memory information.static MemoryInfoReturns a snapshot of JVM non-heap memory information.static OsInfoos()Returns a snapshot of operating system information.static longReturns the free physical memory in bytes, or -1 if unavailable.static longReturns the total physical memory in bytes, or -1 if unavailable.static doubleReturns the current JVM process CPU load as a value in [0.0, 1.0], or -1.0 if not available.static RuntimeInforuntime()Returns a snapshot of JVM runtime information.static longswapFree()Returns the free swap space in bytes, or -1 if unavailable.static longReturns the total swap space in bytes, or -1 if unavailable.static longuptime()Returns the JVM uptime in milliseconds.
-
Method Details
-
cpu
Returns a snapshot of current CPU information. 返回当前 CPU 信息的快照。- Returns:
- CPU info snapshot
-
cpuLoad
public static double cpuLoad()Returns the current system-wide CPU load as a value in [0.0, 1.0], or -1.0 if not available. 返回当前系统级 CPU 负载,值在 [0.0, 1.0] 范围内,不可用时返回 -1.0。- Returns:
- system CPU load, or -1.0 if unavailable
-
processCpuLoad
public static double processCpuLoad()Returns the current JVM process CPU load as a value in [0.0, 1.0], or -1.0 if not available. 返回当前 JVM 进程 CPU 负载,值在 [0.0, 1.0] 范围内,不可用时返回 -1.0。- Returns:
- process CPU load, or -1.0 if unavailable
-
loadAverage
public static double[] loadAverage()Returns the system load averages for 1, 5, and 15 minutes. 返回 1、5、15 分钟的系统平均负载。On Linux, attempts to read
/proc/loadavgfor all three values. On other Unix-like systems, returns the 1-minute average fromOperatingSystemMXBean.getSystemLoadAverage(). On Windows, returns an empty array.在 Linux 上尝试从
/proc/loadavg读取全部三个值。 在其他类 Unix 系统上,返回OperatingSystemMXBean.getSystemLoadAverage()的 1 分钟平均值。在 Windows 上返回空数组。- Returns:
- load average array (may be empty on unsupported platforms)
-
memory
Returns a snapshot of physical memory information. 返回物理内存信息的快照。- Returns:
- physical memory info snapshot
-
heapMemory
Returns a snapshot of JVM heap memory information. 返回 JVM 堆内存信息的快照。- Returns:
- heap memory info snapshot
-
nonHeapMemory
Returns a snapshot of JVM non-heap memory information. 返回 JVM 非堆内存信息的快照。- Returns:
- non-heap memory info snapshot
-
physicalMemoryTotal
public static long physicalMemoryTotal()Returns the total physical memory in bytes, or -1 if unavailable. 返回总物理内存(字节),不可用时返回 -1。- Returns:
- total physical memory bytes, or -1
-
physicalMemoryFree
public static long physicalMemoryFree()Returns the free physical memory in bytes, or -1 if unavailable. 返回空闲物理内存(字节),不可用时返回 -1。- Returns:
- free physical memory bytes, or -1
-
swapTotal
public static long swapTotal()Returns the total swap space in bytes, or -1 if unavailable. 返回总交换空间(字节),不可用时返回 -1。- Returns:
- total swap bytes, or -1
-
swapFree
public static long swapFree()Returns the free swap space in bytes, or -1 if unavailable. 返回空闲交换空间(字节),不可用时返回 -1。- Returns:
- free swap bytes, or -1
-
disks
-
disk
Returns disk information for the file store containing the given path. 返回包含给定路径的文件存储的磁盘信息。- Parameters:
path- the path to query - 要查询的路径- Returns:
- disk info for the path's file store
- Throws:
IOException- if an I/O error occursNullPointerException- if path is null
-
diskTotal
Returns the total disk space for the given path, in bytes. 返回给定路径的总磁盘空间(字节)。- Parameters:
path- the path to query - 要查询的路径- Returns:
- total disk space in bytes, or -1 if an error occurs
-
diskFree
Returns the free (unallocated) disk space for the given path, in bytes. 返回给定路径的空闲(未分配)磁盘空间(字节)。- Parameters:
path- the path to query - 要查询的路径- Returns:
- free disk space in bytes, or -1 if an error occurs
-
diskUsable
Returns the usable disk space for the given path, in bytes. 返回给定路径的可用磁盘空间(字节)。Usable space takes into account OS-level restrictions and may be less than unallocated space.
可用空间考虑了操作系统级别的限制,可能小于未分配空间。
- Parameters:
path- the path to query - 要查询的路径- Returns:
- usable disk space in bytes, or -1 if an error occurs
-
os
-
hostname
Returns the machine hostname. 返回机器主机名。Falls back to "unknown" if hostname resolution fails.
如果主机名解析失败则回退为 "unknown"。
- Returns:
- the hostname, or "unknown" on failure
-
uptime
public static long uptime()Returns the JVM uptime in milliseconds. 返回 JVM 运行时间(毫秒)。- Returns:
- uptime in milliseconds
-
environmentVariables
Returns an unmodifiable view of the system environment variables. 返回系统环境变量的不可修改视图。Security Warning | 安全警告: Environment variables may contain sensitive credentials (API keys, database passwords, tokens). Never log, serialize, or expose the returned map to untrusted parties.
安全警告: 环境变量可能包含敏感凭据(API 密钥、数据库密码、令牌)。 切勿将返回的 Map 记录日志、序列化或暴露给不可信方。
- Returns:
- unmodifiable map of environment variables
-
runtime
Returns a snapshot of JVM runtime information. 返回 JVM 运行时信息的快照。- Returns:
- runtime info snapshot
-