JAL-3477 New jalview.bin.MemorySetting jalview.bin.GetMemory source files for getdown
[jalview.git] / getdown / src / getdown / core / src / main / java / jalview / bin / MemorySetting.java
index ac1ac28..117be25 100644 (file)
@@ -1,5 +1,16 @@
 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
@@ -8,33 +19,64 @@ public class MemorySetting
 
   private final static int maxHeapSizePerCentDefault = 90;
 
-  public final static String maxHeapSizePerCentProperty = "jvmmempc";
+  public final static String maxHeapSizePerCentPropertyName = "jvmmempc";
 
   private final static long maxHeapSizeDefault = 34359738368L; // 32GB
 
   private final static long noMemMaxHeapSizeDefault = 8589934592L; // 8GB
 
-  public final static String maxHeapSizeProperty = "jvmmemmax";
+  public final static String maxHeapSizePropertyName = "jvmmemmax";
+
+  protected static boolean logToClassChecked = false;
 
   public static long getMemorySetting()
   {
     return getMemorySetting(null, null);
   }
 
-  public static long getMemorySetting(String jvmmemmaxString,
-          String jvmmempcString)
+  /**
+   * 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
+    // (absolute) jvmmaxmem setting, start with default
     long memmax = maxHeapSizeDefault;
-    String jvmmemmaxorig = jvmmemmaxString;
-    if (jvmmemmaxorig == null)
+    if (jvmmemmaxarg == null)
     {
-      jvmmemmaxorig = System.getProperty(maxHeapSizeProperty);
+      jvmmemmaxarg = System.getProperty(maxHeapSizePropertyName);
     }
-    String jvmmemmax = jvmmemmaxorig;
+    String jvmmemmax = jvmmemmaxarg;
     if (jvmmemmax != null && jvmmemmax.length() > 0)
     {
       long multiplier = 1;
@@ -71,9 +113,10 @@ public class MemorySetting
       } catch (NumberFormatException e)
       {
         memmax = maxHeapSizeDefault;
-        System.out.println("MemorySetting Property '" + maxHeapSizeProperty
+        System.out.println("MemorySetting Property '"
+                + maxHeapSizePropertyName
                 + "' ("
-                + jvmmemmaxorig + "') badly formatted, using default ("
+                + jvmmemmaxarg + "') badly formatted, using default ("
                 + memmax + ").");
       }
 
@@ -82,8 +125,8 @@ public class MemorySetting
       {
         memmax = maxHeapSizeDefault;
         System.out.println(
-                "MemorySetting Property '" + maxHeapSizeProperty + "' ("
-                        + jvmmemmaxorig
+                "MemorySetting Property '" + maxHeapSizePropertyName + "' ("
+                        + jvmmemmaxarg
                         + ") too big, using default (" + memmax + ").");
       }
       else
@@ -92,31 +135,33 @@ public class MemorySetting
       }
 
       // check at least minimum value (this accounts for negatives too)
-      if (memmax < MemorySetting.applicationMinMemory)
+      if (memmax < applicationMinMemory)
       {
-        memmax = MemorySetting.applicationMinMemory;
+        memmax = applicationMinMemory;
         System.out.println(
-                "MemorySetting Property '" + maxHeapSizeProperty + "' ("
-                        + jvmmemmaxorig
-                        + ") too small, using minimum (" + memmax + ").");
+                "MemorySetting Property '" + maxHeapSizePropertyName + "' ("
+                        + jvmmemmaxarg
+                        + ") too small, using minimum ("
+                        + applicationMinMemory + ").");
       }
 
     }
     else
     {
       // no need to warn if no setting
-      // System.out.println("MemorySetting Property '" + maxHeapSizeProperty + "' not
+      // System.out.println("MemorySetting Property '" + maxHeapSizeProperty
+      // + "' not
       // set.");
     }
 
-    // get max percent of physical memory
+    // get max percent of physical memory, starting with default
     float percent = maxHeapSizePerCentDefault;
-    String jvmmempc = jvmmempcString;
-    if (jvmmempc == null)
+    if (jvmmempcarg == null)
     {
-      jvmmempc = System.getProperty(maxHeapSizePerCentProperty);
+      jvmmempcarg = System.getProperty(maxHeapSizePerCentPropertyName);
     }
-    long pcmem = -1;
+    String jvmmempc = jvmmempcarg;
+    long mempc = -1;
     try
     {
       if (jvmmempc != null)
@@ -129,49 +174,108 @@ public class MemorySetting
         else
         {
           System.out.println(
-                  "MemorySetting Property '" + maxHeapSizePerCentProperty
-                  + "' should be in range 1..100");
+                  "MemorySetting Property '"
+                          + maxHeapSizePerCentPropertyName
+                          + "' should be in range 1..100. Using default "
+                          + percent + "%");
         }
       }
     } catch (NumberFormatException e)
     {
       System.out.println(
-              "MemorySetting property '" + maxHeapSizePerCentProperty
-                      + "' (" + jvmmempc + ") badly formatted");
+              "MemorySetting property '" + maxHeapSizePerCentPropertyName
+                      + "' (" + jvmmempcarg + ") badly formatted");
     }
 
     // catch everything in case of no com.sun.management.OperatingSystemMXBean
     boolean memoryPercentError = false;
     try
     {
-      pcmem = MemoryPercent.memPercent(percent);
+      long physicalMem = GetMemory.getPhysicalMemory();
+      if (physicalMem > applicationMinMemory)
+      {
+        // try and set at least applicationMinMemory and thereafter ensure
+        // leaveFreeMinMemory is left for the OS
+
+        mempc = (long) ((physicalMem / 100F) * percent);
+
+        // check for memory left for OS
+        boolean reducedmempc = false;
+        if (physicalMem - mempc < leaveFreeMinMemory)
+        {
+          mempc = physicalMem - leaveFreeMinMemory;
+          reducedmempc = true;
+          System.out.println("MemorySetting Property '"
+                  + maxHeapSizePerCentPropertyName + "' (" + jvmmempcarg
+                  + ") too large. Leaving free space for OS and reducing to ("
+                  + mempc + ").");
+        }
+
+        // check for minimum application memsize
+        if (mempc < applicationMinMemory)
+        {
+          if (reducedmempc)
+          {
+            System.out.println("Reduced MemorySetting (" + mempc
+                    + ") too small. Increasing to application minimum ("
+                    + applicationMinMemory + ").");
+          }
+          else
+          {
+            System.out.println("MemorySetting Property '"
+                    + maxHeapSizePerCentPropertyName + "' (" + jvmmempcarg
+                    + ") too small. Using minimum (" + applicationMinMemory
+                    + ").");
+          }
+          mempc = applicationMinMemory;
+        }
+      }
+      else
+      {
+        // not enough memory for application, just try and grab what we can!
+        mempc = physicalMem;
+        System.out.println(
+                "Not enough physical memory for application. Ignoring MemorySetting Property '"
+                        + maxHeapSizePerCentPropertyName + "' ("
+                        + 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 if physical memory, let's cap maxMemLong to 8GB
-    if (memoryPercentError && jvmmempc != null && pcmem == -1
+
+    // 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 || (jvmmempcarg == null && jvmmemmaxarg == null))
+    // the above is easier to understand but simplified to
+            && !(jvmmempcarg == null && jvmmemmaxarg != null)
             && memmax > noMemMaxHeapSizeDefault)
     {
       System.out.println(
-              "Capping maximum memory to 8GB due to failure to read physical memory size.");
+              "Capping maximum memory to "
+                      + (noMemMaxHeapSizeDefault + 536870912) / 1073741824 // this is the nearest integer GB for noMemMaxHeapSizeDefault
+                      + "GB due to failure to read physical memory size.");
       memmax = noMemMaxHeapSizeDefault;
     }
 
-    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;
   }
 
-}
+}
\ No newline at end of file