3 import java.lang.management.ManagementFactory;
4 import java.lang.management.OperatingSystemMXBean;
7 * Isolated class to ascertain physical memory of the system using
8 * com.sun.management.OperatingSystemMXBean class's getTotalPhysicalMemorySize
9 * method. This class is present in OpenJDK 8,9,10,11,12,13. It is present but
10 * marked as deprecated in the early-access(30) release of OpenJDK 14. In case
11 * of an alternative/unsupported JRE being used or the class/method not being
12 * implemented in an exotic architecture JRE this call has been isolated into
13 * this separate class.
23 * com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize()
26 * com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize()
27 * or -1 if this class is not present in the JRE.
29 protected static long getPhysicalMemory()
31 final OperatingSystemMXBean o = ManagementFactory
32 .getOperatingSystemMXBean();
36 if (o instanceof com.sun.management.OperatingSystemMXBean)
38 final com.sun.management.OperatingSystemMXBean osb = (com.sun.management.OperatingSystemMXBean) o;
39 return osb.getTotalPhysicalMemorySize();
41 } catch (NoClassDefFoundError e)
43 // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
45 "No com.sun.management.OperatingSystemMXBean: cannot get total physical memory size");
48 // We didn't get a com.sun.management.OperatingSystemMXBean.