JAL-3477 Javadoc, refactoring and slight changes to memory default decisions based...
[jalview.git] / src / jalview / bin / GetMemory.java
diff --git a/src/jalview/bin/GetMemory.java b/src/jalview/bin/GetMemory.java
new file mode 100644 (file)
index 0000000..26c3d5a
--- /dev/null
@@ -0,0 +1,52 @@
+package jalview.bin;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.OperatingSystemMXBean;
+
+/**
+ * Isolated class to ascertain physical memory of the system using
+ * com.sun.management.OperatingSystemMXBean class's getTotalPhysicalMemorySize
+ * method. This class is present in OpenJDK 8,9,10,11,12,13. It is present but
+ * marked as deprecated in the early-access(30) release of OpenJDK 14. In case
+ * of an alternative/unsupported JRE being used or the class/method not being
+ * implemented in an exotic architecture JRE this call has been isolated into
+ * this separate class.
+ * 
+ * @author bsoares
+ *
+ */
+public class GetMemory
+{
+
+  /**
+   * Wrapper for
+   * com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize()
+   * 
+   * @return Result of
+   *         com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize()
+   *         or -1 if this class is not present in the JRE.
+   */
+  protected static long getPhysicalMemory()
+  {
+    final OperatingSystemMXBean o = ManagementFactory
+            .getOperatingSystemMXBean();
+
+    try
+    {
+      if (o instanceof com.sun.management.OperatingSystemMXBean)
+      {
+        final com.sun.management.OperatingSystemMXBean osb = (com.sun.management.OperatingSystemMXBean) o;
+        return osb.getTotalPhysicalMemorySize();
+      }
+    } catch (NoClassDefFoundError e)
+    {
+      // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
+      System.err.println(
+              "No com.sun.management.OperatingSystemMXBean: cannot get total physical memory size");
+    }
+
+    // We didn't get a com.sun.management.OperatingSystemMXBean.
+    return -1;
+  }
+
+}