如何以编程方式检索Java中可用的磁盘上可用交换内存大小

如何以编程方式检索Java中可用的磁盘上可用交换内存大小,java,memory,Java,Memory,我曾尝试通过以下方式在Windows上的Java 8中以编程方式获取可用(磁盘上)交换大小(请参见getFreeSwapMemoryInMB()),除了可用交换内存(磁盘上)之外,所有指标似乎都是正确的: 是否有一种方法可以获得磁盘上免费交换大小的可靠指标?现在,根据Windows性能监视器的建议(使用%已使用页面文件),该指标低于预期。这是OperatingSystemMXBean的已知问题吗? 使用的术语:虚拟内存=物理内存(RAM)+交换内存(磁盘上)。 谢谢 public class A

我曾尝试通过以下方式在Windows上的Java 8中以编程方式获取可用(磁盘上)交换大小(请参见getFreeSwapMemoryInMB()),除了可用交换内存(磁盘上)之外,所有指标似乎都是正确的:

是否有一种方法可以获得磁盘上免费交换大小的可靠指标?现在,根据Windows性能监视器的建议(使用%已使用页面文件),该指标低于预期。这是OperatingSystemMXBean的已知问题吗? 使用的术语:虚拟内存=物理内存(RAM)+交换内存(磁盘上)。 谢谢

public class ApplicationRuntime {

    private static final OperatingSystemMXBean OS  
         = (com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();

    public static long getTotalVirtualMemory() {
         return OS.getTotalSwapSpaceSize(); // OS.getTotalSwapSpaceSize() actually returns the total **virtual** memory (RAM + on-disk)
    }

    public static long getFreeVirtualMemory() {
        return OS.getFreeSwapSpaceSize(); // OS.getFreeSwapSpaceSize() actually returns the total free **virtual** memory (RAM + on-disk)
    }

    public static long getFreePhysicalMemory() {
        return OS.getFreePhysicalMemorySize();
    }

    public static long getTotalPhysicalMemory() {
        return OS.getTotalPhysicalMemorySize();
    }

    public static long getTotalSwapMemory() { // total on-disk virtual memory
        return getTotalVirtualMemory() - getTotalPhysicalMemory(); // CORRECT (according to Windows settings)
    }

    public static long getFreeSwapMemory() { // free on-disk virtual memory
        return getFreeVirtualMemory() - getFreePhysicalMemory(); // INCORRECT seems to return way less than the actual value (many GB less than the Windows performance monitor suggests, on 32GB of total on-disk swap)
    }
}