Merge branch 'bug/JAL-3477_memory_max_allocation' into develop
authorJim Procter <jprocter@issues.jalview.org>
Wed, 26 Feb 2020 16:55:54 +0000 (16:55 +0000)
committerJim Procter <jprocter@issues.jalview.org>
Wed, 26 Feb 2020 16:55:54 +0000 (16:55 +0000)
replaced all libs with libs onbug/JAL-3477_memory_max_allocation, though these will still need to be rebuilt (but how !)

getdown/lib/getdown-core.jar
getdown/lib/getdown-launcher-local.jar
getdown/lib/getdown-launcher.jar
getdown/src/getdown/core/src/main/java/jalview/bin/GetMemory.java [moved from getdown/src/getdown/core/src/main/java/jalview/bin/MemoryPercent.java with 51% similarity]
getdown/src/getdown/core/src/main/java/jalview/bin/MemorySetting.java
j11lib/getdown-core.jar
j8lib/getdown-core.jar
src/jalview/bin/GetMemory.java [moved from src/jalview/bin/MemoryPercent.java with 51% similarity]
src/jalview/bin/Launcher.java
src/jalview/bin/MemorySetting.java

index 968202b..d703890 100644 (file)
Binary files a/getdown/lib/getdown-core.jar and b/getdown/lib/getdown-core.jar differ
index feb6f2a..957e385 100644 (file)
Binary files a/getdown/lib/getdown-launcher-local.jar and b/getdown/lib/getdown-launcher-local.jar differ
index dd4b2b1..e40ba8d 100644 (file)
Binary files a/getdown/lib/getdown-launcher.jar and b/getdown/lib/getdown-launcher.jar differ
@@ -3,9 +3,29 @@ package jalview.bin;
 import java.lang.management.ManagementFactory;
 import java.lang.management.OperatingSystemMXBean;
 
-public class MemoryPercent
+/**
+ * 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
+ *
+ */
+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
index f6924b9..7849ba2 100644 (file)
@@ -1,20 +1,33 @@
 package jalview.bin;
 
+/**
+ * Methods to decide on appropriate memory setting for Jalview based on two
+ * optionally provided values: jvmmempc - the maximum percentage of total
+ * physical memory to allocate, and jvmmemmax - the maximum absolute amount of
+ * physical memory to allocate. These can be provided as arguments or system
+ * properties. Other considerations such as minimum application requirements and
+ * leaving space for OS are used too.
+ * 
+ * @author bsoares
+ *
+ */
 public class MemorySetting
 {
-  public static final long leaveFreeMinMemory = 536870912; // 0.5 GB
+  public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc";
 
-  public static final long applicationMinMemory = 536870912; // 0.5 GB
+  public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax";
 
-  private final static int maxHeapSizePerCentDefault = 90;
+  private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; // 90%
 
-  public final static String maxHeapSizePerCentProperty = "jvmmempc";
+  private static final long GIGABYTE = 1073741824; // 1GB
 
-  private final static long maxHeapSizeDefault = 34359738368L; // 32GB
+  public static final long LEAVE_FREE_MIN_MEMORY = GIGABYTE/2;
 
-  private final static long noMemMaxHeapSizeDefault = 8589934592L; // 8GB
+  public static final long APPLICATION_MIN_MEMORY = GIGABYTE/2;
 
-  public final static String maxHeapSizeProperty = "jvmmemmax";
+  private static final long MAX_HEAPSIZE_GB_DEFAULT = 32;
+
+  private static final long NOMEM_MAX_HEAPSIZE_GB_DEFAULT = 8;
 
   protected static boolean logToClassChecked = false;
 
@@ -23,19 +36,49 @@ public class MemorySetting
     return getMemorySetting(null, null);
   }
 
-  public static long getMemorySetting(String jvmmemmaxorig,
-          String jvmmempcorig)
+  /**
+   * Decide on appropriate memory setting for Jalview based on the two arguments
+   * values: jvmmempc - the maximum percentage of total physical memory to
+   * allocate, and jvmmemmax - the maximum absolute amount of physical memory to
+   * allocate. These can be provided as arguments. If not provided as arguments
+   * (or set as null) system properties will be used instead (if set). The memory
+   * setting returned will be the lower of the two values. If either of the values
+   * are not provided then defaults will be used (jvmmempc=90, jvmmemmax=32GB). If
+   * total physical memory can't be ascertained when jvmmempc was set or neither
+   * jvmmempc nor jvmmemmax were set, then jvmmemmax defaults to a much safer 8GB.
+   * In this case explicitly setting jvmmemmax and not setting jvmmempc can set a
+   * higher memory for Jalview. The calculation also tries to ensure 0.5GB memory
+   * for the OS, but also tries to ensure at least 0.5GB memory for Jalview (which
+   * takes priority over the OS) If there is less then 0.5GB of physical memory
+   * then the total physical memory is used for Jalview.
+   * 
+   * @param jvmmemmaxarg
+   *                       Maximum value of memory to set. This can be a numeric
+   *                       string optionally followed by "b", "k", "m", "g", "t"
+   *                       (case insensitive) to indicate bytes, kilobytes,
+   *                       megabytes, gigabytes, terabytes respectively. If null a
+   *                       default value of 32G will be used. If null and either
+   *                       physical memory can't be determined then the default is
+   *                       8GB.
+   * @param jvmmempcarg
+   *                       Max percentage of physical memory to use. Defaults to
+   *                       "90".
+   * 
+   * @return The amount of memory (in bytes) to allocate to Jalview
+   */
+  public static long getMemorySetting(String jvmmemmaxarg,
+          String jvmmempcarg)
   {
     // actual Xmx value-to-be
     long maxMemLong = -1;
 
-    // get (absolute) jvmmaxmem setting
-    long memmax = maxHeapSizeDefault;
-    if (jvmmemmaxorig == null)
+    // (absolute) jvmmaxmem setting, start with default
+    long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
+    if (jvmmemmaxarg == null)
     {
-      jvmmemmaxorig = System.getProperty(maxHeapSizeProperty);
+      jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME);
     }
-    String jvmmemmax = jvmmemmaxorig;
+    String jvmmemmax = jvmmemmaxarg;
     if (jvmmemmax != null && jvmmemmax.length() > 0)
     {
       long multiplier = 1;
@@ -71,22 +114,23 @@ public class MemorySetting
         memmax = Long.parseLong(jvmmemmax);
       } catch (NumberFormatException e)
       {
-        memmax = maxHeapSizeDefault;
+        memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
         System.out.println("MemorySetting Property '"
-                + maxHeapSizeProperty
+                + MAX_HEAPSIZE_PROPERTY_NAME
                 + "' ("
-                + jvmmemmaxorig + "') badly formatted, using default ("
-                + memmax + ").");
+                + jvmmemmaxarg + "') badly formatted, using default ("
+                + MAX_HEAPSIZE_GB_DEFAULT + "g).");
       }
 
       // apply multiplier if not too big (i.e. bigger than a long)
       if (Long.MAX_VALUE / memmax < multiplier)
       {
-        memmax = maxHeapSizeDefault;
+        memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
         System.out.println(
-                "MemorySetting Property '" + maxHeapSizeProperty + "' ("
-                        + jvmmemmaxorig
-                        + ") too big, using default (" + memmax + ").");
+                "MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' ("
+                        + jvmmemmaxarg
+                        + ") too big, using default ("
+                        + MAX_HEAPSIZE_GB_DEFAULT + "g).");
       }
       else
       {
@@ -94,14 +138,14 @@ public class MemorySetting
       }
 
       // check at least minimum value (this accounts for negatives too)
-      if (memmax < applicationMinMemory)
+      if (memmax < APPLICATION_MIN_MEMORY)
       {
-        memmax = applicationMinMemory;
+        memmax = APPLICATION_MIN_MEMORY;
         System.out.println(
-                "MemorySetting Property '" + maxHeapSizeProperty + "' ("
-                        + jvmmemmaxorig
+                "MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' ("
+                        + jvmmemmaxarg
                         + ") too small, using minimum ("
-                        + applicationMinMemory + ").");
+                        + APPLICATION_MIN_MEMORY + ").");
       }
 
     }
@@ -113,18 +157,14 @@ public class MemorySetting
       // set.");
     }
 
-    // get max percent of physical memory
-    float percent = maxHeapSizePerCentDefault;
-    if (jvmmempcorig == null)
-    {
-      jvmmempcorig = System.getProperty(maxHeapSizePerCentProperty);
-    }
-    String jvmmempc = jvmmempcorig;
-    if (jvmmempc == null)
+    // get max percent of physical memory, starting with default
+    float percent = MAX_HEAPSIZE_PERCENT_DEFAULT;
+    if (jvmmempcarg == null)
     {
-      jvmmempc = System.getProperty(maxHeapSizePerCentProperty);
+      jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME);
     }
-    long pcmem = -1;
+    String jvmmempc = jvmmempcarg;
+    long mempc = -1;
     try
     {
       if (jvmmempc != null)
@@ -137,83 +177,103 @@ public class MemorySetting
         else
         {
           System.out.println(
-                  "MemorySetting Property '" + maxHeapSizePerCentProperty
-                  + "' should be in range 1..100");
+                  "MemorySetting Property '"
+                          + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
+                          + "' should be in range 1..100. Using default "
+                          + percent + "%");
         }
       }
     } catch (NumberFormatException e)
     {
       System.out.println(
-              "MemorySetting property '" + maxHeapSizePerCentProperty
-                      + "' (" + jvmmempc + ") badly formatted");
+              "MemorySetting property '" + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
+                      + "' (" + jvmmempcarg + ") badly formatted");
     }
 
     // catch everything in case of no com.sun.management.OperatingSystemMXBean
     boolean memoryPercentError = false;
     try
     {
-      long physicalMem = MemoryPercent.getPhysicalMemory();
-      if (physicalMem > applicationMinMemory)
+      long physicalMem = GetMemory.getPhysicalMemory();
+      if (physicalMem > APPLICATION_MIN_MEMORY)
       {
         // try and set at least applicationMinMemory and thereafter ensure
         // leaveFreeMinMemory is left for the OS
 
-        pcmem = (long) ((physicalMem * percent) / 100F);
+        mempc = (long) ((physicalMem / 100F) * percent);
 
         // check for memory left for OS
-        if (physicalMem - pcmem < leaveFreeMinMemory)
+        boolean reducedmempc = false;
+        if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
         {
-          pcmem = physicalMem - leaveFreeMinMemory;
+          mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
+          reducedmempc = true;
           System.out.println("MemorySetting Property '"
-                  + maxHeapSizePerCentProperty + "' (" + jvmmempcorig
-                  + ") too large. Leaving free space for OS, using ("
-                  + pcmem + ").");
+                  + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
+                  + ") too large. Leaving free space for OS and reducing to ("
+                  + mempc + ").");
         }
 
         // check for minimum application memsize
-        if (pcmem < applicationMinMemory)
+        if (mempc < APPLICATION_MIN_MEMORY)
         {
-          pcmem = applicationMinMemory;
-          System.out.println("MemorySetting Property '"
-                  + maxHeapSizePerCentProperty + "' (" + jvmmempcorig
-                  + ") too small, using minimum (" + applicationMinMemory
-                  + ").");
+          if (reducedmempc)
+          {
+            System.out.println("Reduced MemorySetting (" + mempc
+                    + ") too small. Increasing to application minimum ("
+                    + APPLICATION_MIN_MEMORY + ").");
+          }
+          else
+          {
+            System.out.println("MemorySetting Property '"
+                    + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
+                    + ") too small. Using minimum (" + APPLICATION_MIN_MEMORY
+                    + ").");
+          }
+          mempc = APPLICATION_MIN_MEMORY;
         }
       }
       else
       {
         // not enough memory for application, just try and grab what we can!
-        pcmem = physicalMem;
-        System.out.println("MemorySetting Property '"
-                + maxHeapSizePerCentProperty + "' (" + jvmmempcorig
-                + "): Not enough memory, using max available (" + pcmem
-                + ").");
+        mempc = physicalMem;
+        System.out.println(
+                "Not enough physical memory for application. Ignoring MemorySetting Property '"
+                        + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
+                        + jvmmempcarg
+                        + "). Using maximum memory available ("
+                        + physicalMem + ").");
       }
 
     } catch (Throwable t)
     {
       memoryPercentError = true;
-      System.out.println("Problem calling MemoryPercent.memPercent("
-              + percent
-              + "). Likely to be problem with com.sun.management.OperatingSystemMXBean");
+      System.out.println(
+              "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean");
       t.printStackTrace();
     }
-    // In the case of an error reading the percentage of physical memory (when jvmmempc was set), let's cap maxMemLong to 8GB
-    if (memoryPercentError && jvmmempc != null && pcmem == -1
-            && memmax > noMemMaxHeapSizeDefault)
+
+    // In the case of an error reading the percentage of physical memory (when
+    // jvmmempc was set OR neither jvmmempc nor jvmmemmax were set), let's cap
+    // maxMemLong to 8GB
+    if (memoryPercentError && mempc == -1
+            && !(jvmmempcarg == null && jvmmemmaxarg != null) // the same as (jvmmempcarg != null || (jvmmempcarg == null && jvmmemmaxarg
+                                                              // == null))
+            && memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE)
     {
       System.out.println(
-              "Capping maximum memory to 8GB due to failure to read physical memory size.");
-      memmax = noMemMaxHeapSizeDefault;
+              "Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT
+                      + "g due to failure to read physical memory size.");
+      memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
     }
 
-    if (pcmem == -1) // not set
+    if (mempc == -1) // percentage memory not set
     {
       maxMemLong = memmax;
     }
     else
     {
-      maxMemLong = Math.min(pcmem, memmax);
+      maxMemLong = Math.min(mempc, memmax);
     }
 
     return maxMemLong;
index 968202b..d703890 100644 (file)
Binary files a/j11lib/getdown-core.jar and b/j11lib/getdown-core.jar differ
index 968202b..d703890 100644 (file)
Binary files a/j8lib/getdown-core.jar and b/j8lib/getdown-core.jar differ
similarity index 51%
rename from src/jalview/bin/MemoryPercent.java
rename to src/jalview/bin/GetMemory.java
index 3288e66..e89bffb 100644 (file)
@@ -3,9 +3,29 @@ package jalview.bin;
 import java.lang.management.ManagementFactory;
 import java.lang.management.OperatingSystemMXBean;
 
-public class MemoryPercent
+/**
+ * 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
+ *
+ */
+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
index 041a7b1..1541615 100644 (file)
@@ -4,19 +4,43 @@ import java.io.File;
 import java.io.IOException;
 import java.lang.management.ManagementFactory;
 import java.util.ArrayList;
-
+import java.util.List;
+
+/**
+ * A Launcher class for Jalview. This class is used to launch Jalview from the
+ * shadowJar when Getdown is not used or available. It attempts to take all the
+ * command line arguments to pass on to the jalview.bin.Jalview class, but to
+ * insert a -Xmx memory setting to a sensible default, using the -jvmmempc and
+ * -jvmmemmax application arguments if specified. If not specified then system
+ * properties will be looked for by jalview.bin.MemorySetting. If the user has
+ * provided the JVM with a -Xmx setting directly and not set -jvmmempc or
+ * -jvmmemmax then this setting will be used and system properties ignored. If
+ * -Xmx is set as well as -jvmmempc or -jvmmemmax as argument(s) then the -Xmx
+ * argument will NOT be passed on to the main application launch.
+ * 
+ * @author bsoares
+ *
+ */
 public class Launcher
 {
   private final static String startClass = "jalview.bin.Jalview";
 
   private final static String dockIconPath = "JalviewLogo_Huge.png";
 
+  /**
+   * main method for jalview.bin.Launcher. This restarts the same JRE's JVM with
+   * the same arguments but with memory adjusted based on extracted -jvmmempc and
+   * -jvmmemmax application arguments. If on a Mac then extra dock:icon and
+   * dock:name arguments are also set.
+   * 
+   * @param args
+   */
   public static void main(String[] args)
   {
     final String javaBin = System.getProperty("java.home") + File.separator
             + "bin" + File.separator + "java";
 
-    ArrayList<String> command = new ArrayList<>();
+    List<String> command = new ArrayList<>();
     command.add(javaBin);
 
     String memSetting = null;
@@ -30,23 +54,48 @@ public class Launcher
     }
     command.add("-cp");
     command.add(ManagementFactory.getRuntimeMXBean().getClassPath());
+
+    String jvmmempc = null;
+    String jvmmemmax = null;
     ArrayList<String> arguments = new ArrayList<>();
     for (String arg : args)
     {
-      arguments.add(arg);
+      // jvmmempc and jvmmemmax args used to set memory and are not passed on to
+      // startClass
+      if (arg.startsWith(
+              "-" + MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "="))
+      {
+        jvmmempc = arg.substring(
+                MemorySetting.MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.length()
+                        + 2);
+      }
+      else if (arg.startsWith(
+              "-" + MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME + "="))
+      {
+        jvmmemmax = arg.substring(
+                MemorySetting.MAX_HEAPSIZE_PROPERTY_NAME.length() + 2);
+      }
+      else
+      {
+        arguments.add(arg);
+      }
     }
 
     // add memory setting if not specified
     boolean memSet = false;
     boolean dockIcon = false;
     boolean dockName = false;
-    ARG: for (int i = 0; i < command.size(); i++)
+    for (int i = 0; i < command.size(); i++)
     {
       String arg = command.get(i);
       if (arg.startsWith("-Xmx"))
       {
-        memSetting = arg;
-        memSet = true;
+        // only use -Xmx if jvmmemmax and jvmmempc have not been set
+        if (jvmmempc == null && jvmmemmax == null)
+        {
+          memSetting = arg;
+          memSet = true;
+        }
       }
       else if (arg.startsWith("-Xdock:icon"))
       {
@@ -60,11 +109,12 @@ public class Launcher
 
     if (!memSet)
     {
-      long maxMemLong = MemorySetting.getMemorySetting();
+      long maxMemLong = MemorySetting.getMemorySetting(jvmmemmax, jvmmempc);
       
       if (maxMemLong > 0)
       {
         memSetting = "-Xmx" + Long.toString(maxMemLong);
+        memSet = true;
         command.add(memSetting);
       }
     }
@@ -90,10 +140,9 @@ public class Launcher
 
     // System.out.println("COMMAND: " + String.join(" ", builder.command()));
     System.out.println("Running " + startClass + " with "
-            + (memSetting == null ? "no memSetting" : memSetting));
+            + (memSetting == null ? "no memory setting" : memSetting));
 
-    if (System.getProperty("launcherstop") != null
-            && System.getProperty("launcherstop").equals("true"))
+    if (Boolean.parseBoolean(System.getProperty("launcherstop")))
     {
       System.exit(0);
     }
@@ -118,7 +167,7 @@ public class Launcher
         }
         final ProcessBuilder builderNoMem = new ProcessBuilder(
                 commandNoMem);
-        System.out.println("NO MEM COMMAND: "
+        System.out.println("Command without memory setting: "
                 + String.join(" ", builderNoMem.command()));
         try
         {
index f6924b9..7849ba2 100644 (file)
@@ -1,20 +1,33 @@
 package jalview.bin;
 
+/**
+ * Methods to decide on appropriate memory setting for Jalview based on two
+ * optionally provided values: jvmmempc - the maximum percentage of total
+ * physical memory to allocate, and jvmmemmax - the maximum absolute amount of
+ * physical memory to allocate. These can be provided as arguments or system
+ * properties. Other considerations such as minimum application requirements and
+ * leaving space for OS are used too.
+ * 
+ * @author bsoares
+ *
+ */
 public class MemorySetting
 {
-  public static final long leaveFreeMinMemory = 536870912; // 0.5 GB
+  public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc";
 
-  public static final long applicationMinMemory = 536870912; // 0.5 GB
+  public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax";
 
-  private final static int maxHeapSizePerCentDefault = 90;
+  private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; // 90%
 
-  public final static String maxHeapSizePerCentProperty = "jvmmempc";
+  private static final long GIGABYTE = 1073741824; // 1GB
 
-  private final static long maxHeapSizeDefault = 34359738368L; // 32GB
+  public static final long LEAVE_FREE_MIN_MEMORY = GIGABYTE/2;
 
-  private final static long noMemMaxHeapSizeDefault = 8589934592L; // 8GB
+  public static final long APPLICATION_MIN_MEMORY = GIGABYTE/2;
 
-  public final static String maxHeapSizeProperty = "jvmmemmax";
+  private static final long MAX_HEAPSIZE_GB_DEFAULT = 32;
+
+  private static final long NOMEM_MAX_HEAPSIZE_GB_DEFAULT = 8;
 
   protected static boolean logToClassChecked = false;
 
@@ -23,19 +36,49 @@ public class MemorySetting
     return getMemorySetting(null, null);
   }
 
-  public static long getMemorySetting(String jvmmemmaxorig,
-          String jvmmempcorig)
+  /**
+   * Decide on appropriate memory setting for Jalview based on the two arguments
+   * values: jvmmempc - the maximum percentage of total physical memory to
+   * allocate, and jvmmemmax - the maximum absolute amount of physical memory to
+   * allocate. These can be provided as arguments. If not provided as arguments
+   * (or set as null) system properties will be used instead (if set). The memory
+   * setting returned will be the lower of the two values. If either of the values
+   * are not provided then defaults will be used (jvmmempc=90, jvmmemmax=32GB). If
+   * total physical memory can't be ascertained when jvmmempc was set or neither
+   * jvmmempc nor jvmmemmax were set, then jvmmemmax defaults to a much safer 8GB.
+   * In this case explicitly setting jvmmemmax and not setting jvmmempc can set a
+   * higher memory for Jalview. The calculation also tries to ensure 0.5GB memory
+   * for the OS, but also tries to ensure at least 0.5GB memory for Jalview (which
+   * takes priority over the OS) If there is less then 0.5GB of physical memory
+   * then the total physical memory is used for Jalview.
+   * 
+   * @param jvmmemmaxarg
+   *                       Maximum value of memory to set. This can be a numeric
+   *                       string optionally followed by "b", "k", "m", "g", "t"
+   *                       (case insensitive) to indicate bytes, kilobytes,
+   *                       megabytes, gigabytes, terabytes respectively. If null a
+   *                       default value of 32G will be used. If null and either
+   *                       physical memory can't be determined then the default is
+   *                       8GB.
+   * @param jvmmempcarg
+   *                       Max percentage of physical memory to use. Defaults to
+   *                       "90".
+   * 
+   * @return The amount of memory (in bytes) to allocate to Jalview
+   */
+  public static long getMemorySetting(String jvmmemmaxarg,
+          String jvmmempcarg)
   {
     // actual Xmx value-to-be
     long maxMemLong = -1;
 
-    // get (absolute) jvmmaxmem setting
-    long memmax = maxHeapSizeDefault;
-    if (jvmmemmaxorig == null)
+    // (absolute) jvmmaxmem setting, start with default
+    long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
+    if (jvmmemmaxarg == null)
     {
-      jvmmemmaxorig = System.getProperty(maxHeapSizeProperty);
+      jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME);
     }
-    String jvmmemmax = jvmmemmaxorig;
+    String jvmmemmax = jvmmemmaxarg;
     if (jvmmemmax != null && jvmmemmax.length() > 0)
     {
       long multiplier = 1;
@@ -71,22 +114,23 @@ public class MemorySetting
         memmax = Long.parseLong(jvmmemmax);
       } catch (NumberFormatException e)
       {
-        memmax = maxHeapSizeDefault;
+        memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
         System.out.println("MemorySetting Property '"
-                + maxHeapSizeProperty
+                + MAX_HEAPSIZE_PROPERTY_NAME
                 + "' ("
-                + jvmmemmaxorig + "') badly formatted, using default ("
-                + memmax + ").");
+                + jvmmemmaxarg + "') badly formatted, using default ("
+                + MAX_HEAPSIZE_GB_DEFAULT + "g).");
       }
 
       // apply multiplier if not too big (i.e. bigger than a long)
       if (Long.MAX_VALUE / memmax < multiplier)
       {
-        memmax = maxHeapSizeDefault;
+        memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
         System.out.println(
-                "MemorySetting Property '" + maxHeapSizeProperty + "' ("
-                        + jvmmemmaxorig
-                        + ") too big, using default (" + memmax + ").");
+                "MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' ("
+                        + jvmmemmaxarg
+                        + ") too big, using default ("
+                        + MAX_HEAPSIZE_GB_DEFAULT + "g).");
       }
       else
       {
@@ -94,14 +138,14 @@ public class MemorySetting
       }
 
       // check at least minimum value (this accounts for negatives too)
-      if (memmax < applicationMinMemory)
+      if (memmax < APPLICATION_MIN_MEMORY)
       {
-        memmax = applicationMinMemory;
+        memmax = APPLICATION_MIN_MEMORY;
         System.out.println(
-                "MemorySetting Property '" + maxHeapSizeProperty + "' ("
-                        + jvmmemmaxorig
+                "MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' ("
+                        + jvmmemmaxarg
                         + ") too small, using minimum ("
-                        + applicationMinMemory + ").");
+                        + APPLICATION_MIN_MEMORY + ").");
       }
 
     }
@@ -113,18 +157,14 @@ public class MemorySetting
       // set.");
     }
 
-    // get max percent of physical memory
-    float percent = maxHeapSizePerCentDefault;
-    if (jvmmempcorig == null)
-    {
-      jvmmempcorig = System.getProperty(maxHeapSizePerCentProperty);
-    }
-    String jvmmempc = jvmmempcorig;
-    if (jvmmempc == null)
+    // get max percent of physical memory, starting with default
+    float percent = MAX_HEAPSIZE_PERCENT_DEFAULT;
+    if (jvmmempcarg == null)
     {
-      jvmmempc = System.getProperty(maxHeapSizePerCentProperty);
+      jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME);
     }
-    long pcmem = -1;
+    String jvmmempc = jvmmempcarg;
+    long mempc = -1;
     try
     {
       if (jvmmempc != null)
@@ -137,83 +177,103 @@ public class MemorySetting
         else
         {
           System.out.println(
-                  "MemorySetting Property '" + maxHeapSizePerCentProperty
-                  + "' should be in range 1..100");
+                  "MemorySetting Property '"
+                          + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
+                          + "' should be in range 1..100. Using default "
+                          + percent + "%");
         }
       }
     } catch (NumberFormatException e)
     {
       System.out.println(
-              "MemorySetting property '" + maxHeapSizePerCentProperty
-                      + "' (" + jvmmempc + ") badly formatted");
+              "MemorySetting property '" + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
+                      + "' (" + jvmmempcarg + ") badly formatted");
     }
 
     // catch everything in case of no com.sun.management.OperatingSystemMXBean
     boolean memoryPercentError = false;
     try
     {
-      long physicalMem = MemoryPercent.getPhysicalMemory();
-      if (physicalMem > applicationMinMemory)
+      long physicalMem = GetMemory.getPhysicalMemory();
+      if (physicalMem > APPLICATION_MIN_MEMORY)
       {
         // try and set at least applicationMinMemory and thereafter ensure
         // leaveFreeMinMemory is left for the OS
 
-        pcmem = (long) ((physicalMem * percent) / 100F);
+        mempc = (long) ((physicalMem / 100F) * percent);
 
         // check for memory left for OS
-        if (physicalMem - pcmem < leaveFreeMinMemory)
+        boolean reducedmempc = false;
+        if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
         {
-          pcmem = physicalMem - leaveFreeMinMemory;
+          mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
+          reducedmempc = true;
           System.out.println("MemorySetting Property '"
-                  + maxHeapSizePerCentProperty + "' (" + jvmmempcorig
-                  + ") too large. Leaving free space for OS, using ("
-                  + pcmem + ").");
+                  + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
+                  + ") too large. Leaving free space for OS and reducing to ("
+                  + mempc + ").");
         }
 
         // check for minimum application memsize
-        if (pcmem < applicationMinMemory)
+        if (mempc < APPLICATION_MIN_MEMORY)
         {
-          pcmem = applicationMinMemory;
-          System.out.println("MemorySetting Property '"
-                  + maxHeapSizePerCentProperty + "' (" + jvmmempcorig
-                  + ") too small, using minimum (" + applicationMinMemory
-                  + ").");
+          if (reducedmempc)
+          {
+            System.out.println("Reduced MemorySetting (" + mempc
+                    + ") too small. Increasing to application minimum ("
+                    + APPLICATION_MIN_MEMORY + ").");
+          }
+          else
+          {
+            System.out.println("MemorySetting Property '"
+                    + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
+                    + ") too small. Using minimum (" + APPLICATION_MIN_MEMORY
+                    + ").");
+          }
+          mempc = APPLICATION_MIN_MEMORY;
         }
       }
       else
       {
         // not enough memory for application, just try and grab what we can!
-        pcmem = physicalMem;
-        System.out.println("MemorySetting Property '"
-                + maxHeapSizePerCentProperty + "' (" + jvmmempcorig
-                + "): Not enough memory, using max available (" + pcmem
-                + ").");
+        mempc = physicalMem;
+        System.out.println(
+                "Not enough physical memory for application. Ignoring MemorySetting Property '"
+                        + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
+                        + jvmmempcarg
+                        + "). Using maximum memory available ("
+                        + physicalMem + ").");
       }
 
     } catch (Throwable t)
     {
       memoryPercentError = true;
-      System.out.println("Problem calling MemoryPercent.memPercent("
-              + percent
-              + "). Likely to be problem with com.sun.management.OperatingSystemMXBean");
+      System.out.println(
+              "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean");
       t.printStackTrace();
     }
-    // In the case of an error reading the percentage of physical memory (when jvmmempc was set), let's cap maxMemLong to 8GB
-    if (memoryPercentError && jvmmempc != null && pcmem == -1
-            && memmax > noMemMaxHeapSizeDefault)
+
+    // In the case of an error reading the percentage of physical memory (when
+    // jvmmempc was set OR neither jvmmempc nor jvmmemmax were set), let's cap
+    // maxMemLong to 8GB
+    if (memoryPercentError && mempc == -1
+            && !(jvmmempcarg == null && jvmmemmaxarg != null) // the same as (jvmmempcarg != null || (jvmmempcarg == null && jvmmemmaxarg
+                                                              // == null))
+            && memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE)
     {
       System.out.println(
-              "Capping maximum memory to 8GB due to failure to read physical memory size.");
-      memmax = noMemMaxHeapSizeDefault;
+              "Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT
+                      + "g due to failure to read physical memory size.");
+      memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
     }
 
-    if (pcmem == -1) // not set
+    if (mempc == -1) // percentage memory not set
     {
       maxMemLong = memmax;
     }
     else
     {
-      maxMemLong = Math.min(pcmem, memmax);
+      maxMemLong = Math.min(mempc, memmax);
     }
 
     return maxMemLong;