Record Class MemoryInfo

java.lang.Object
java.lang.Record
cloud.opencode.base.core.system.MemoryInfo
Record Components:
total - total memory in bytes - 总内存(字节)
used - used memory in bytes - 已用内存(字节)
free - free memory in bytes - 空闲内存(字节)
max - maximum memory in bytes, -1 if undefined - 最大内存(字节),未定义时为 -1

public record MemoryInfo(long total, long used, long free, long max) extends Record
Immutable snapshot of memory information. 内存信息的不可变快照。

Represents a point-in-time view of memory usage, applicable to both JVM heap/non-heap memory and physical system memory.

表示内存使用情况的时间点视图,适用于 JVM 堆/非堆内存和物理系统内存。

Usage Examples | 使用示例:

MemoryInfo heap = SystemInfo.heapMemory();
System.out.println("Heap used: " + heap.usedDisplay());
System.out.println("Usage: " + heap.usagePercent() + "%");

MemoryInfo physical = SystemInfo.memory();
System.out.println("Physical total: " + physical.totalDisplay());
Since:
JDK 25, opencode-base-core V1.0.3
Author:
Leon Soo
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    MemoryInfo(long total, long used, long free, long max)
    Compact canonical constructor with validation.
  • Method Summary

    Modifier and Type
    Method
    Description
    final boolean
    Indicates whether some other object is "equal to" this one.
    long
    Returns the value of the free record component.
    Returns a human-readable representation of free memory.
    final int
    Returns a hash code value for this object.
    long
    max()
    Returns the value of the max record component.
    static MemoryInfo
    of(long total, long used, long free, long max)
    Creates a MemoryInfo with the given values.
    static MemoryInfo
    ofPhysical(long total, long free)
    Creates a MemoryInfo for physical memory where max equals total.
    final String
    Returns a string representation of this record class.
    long
    Returns the value of the total record component.
    Returns a human-readable representation of total memory.
    double
    Returns the memory usage percentage (used/total * 100).
    long
    Returns the value of the used record component.
    Returns a human-readable representation of used memory.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • MemoryInfo

      public MemoryInfo(long total, long used, long free, long max)
      Compact canonical constructor with validation. 带验证的紧凑规范构造器。
  • Method Details

    • of

      public static MemoryInfo of(long total, long used, long free, long max)
      Creates a MemoryInfo with the given values. 使用给定值创建 MemoryInfo。
      Parameters:
      total - total bytes - 总字节数
      used - used bytes - 已用字节数
      free - free bytes - 空闲字节数
      max - max bytes (-1 if undefined) - 最大字节数(未定义时为 -1)
      Returns:
      a new MemoryInfo instance
    • ofPhysical

      public static MemoryInfo ofPhysical(long total, long free)
      Creates a MemoryInfo for physical memory where max equals total. 为物理内存创建 MemoryInfo,其中 max 等于 total。
      Parameters:
      total - total physical memory bytes - 总物理内存字节数
      free - free physical memory bytes - 空闲物理内存字节数
      Returns:
      a new MemoryInfo instance
    • usagePercent

      public double usagePercent()
      Returns the memory usage percentage (used/total * 100). 返回内存使用百分比(used/total * 100)。
      Returns:
      usage percentage in range [0.0, 100.0], or 0.0 if total is 0
    • totalDisplay

      public String totalDisplay()
      Returns a human-readable representation of total memory. 返回总内存的可读表示。
      Returns:
      formatted string e.g. "16.0 GB"
    • usedDisplay

      public String usedDisplay()
      Returns a human-readable representation of used memory. 返回已用内存的可读表示。
      Returns:
      formatted string e.g. "8.5 GB"
    • freeDisplay

      public String freeDisplay()
      Returns a human-readable representation of free memory. 返回空闲内存的可读表示。
      Returns:
      formatted string e.g. "7.5 GB"
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • total

      public long total()
      Returns the value of the total record component.
      Returns:
      the value of the total record component
    • used

      public long used()
      Returns the value of the used record component.
      Returns:
      the value of the used record component
    • free

      public long free()
      Returns the value of the free record component.
      Returns:
      the value of the free record component
    • max

      public long max()
      Returns the value of the max record component.
      Returns:
      the value of the max record component