从java获取操作系统内存大小

2022-01-11 00:00:00 operating-system memory java

有没有什么方法可以从 java 中获取操作系统上总内存的大小?使用

Are there any way to get the size of the total memory on the operating system from java? Using

Runtime.getRuntime().maxMemory()

返回 JVM 允许的内存,而不是操作系统.有没有人有办法获得这个(从java代码)?

returns the allowed memory for the JVM, not of the operating system. Does anyone have a way to obtain this (from java code)?

推荐答案

com.sun.management.OperatingSystemMXBean bean =
  (com.sun.management.OperatingSystemMXBean)
    java.lang.management.ManagementFactory.getOperatingSystemMXBean();
long max = bean.getTotalPhysicalMemorySize();

返回 JVM 的可用 RAM 大小(受 32 位限制),而不是堆大小.

returns available RAM size for JVM (limited by 32bit), not the heap size.

相关文章