JAL-3477 Newer jalview.bin.MemorySetting jalview.bin.GetMemory source files for getdown
authorBen Soares <bsoares@dundee.ac.uk>
Thu, 9 Jan 2020 16:55:10 +0000 (16:55 +0000)
committerBen Soares <bsoares@dundee.ac.uk>
Thu, 9 Jan 2020 16:55:10 +0000 (16:55 +0000)
getdown/src/getdown/core/src/main/java/jalview/bin/GetMemory.java
getdown/src/getdown/core/src/main/java/jalview/bin/MemorySetting.java

index 26c3d5a..e89bffb 100644 (file)
@@ -15,7 +15,7 @@ import java.lang.management.OperatingSystemMXBean;
  * @author bsoares
  *
  */
-public class GetMemory
+class GetMemory
 {
 
   /**
index 117be25..f7c40cf 100644 (file)
@@ -13,19 +13,21 @@ package jalview.bin;
  */
 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 maxHeapSizePerCentPropertyName = "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 maxHeapSizePropertyName = "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;
 
@@ -71,10 +73,10 @@ public class MemorySetting
     long maxMemLong = -1;
 
     // (absolute) jvmmaxmem setting, start with default
-    long memmax = maxHeapSizeDefault;
+    long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
     if (jvmmemmaxarg == null)
     {
-      jvmmemmaxarg = System.getProperty(maxHeapSizePropertyName);
+      jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME);
     }
     String jvmmemmax = jvmmemmaxarg;
     if (jvmmemmax != null && jvmmemmax.length() > 0)
@@ -112,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 '"
-                + maxHeapSizePropertyName
+                + MAX_HEAPSIZE_PROPERTY_NAME
                 + "' ("
                 + jvmmemmaxarg + "') badly formatted, using default ("
-                + memmax + ").");
+                + 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 '" + maxHeapSizePropertyName + "' ("
+                "MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' ("
                         + jvmmemmaxarg
-                        + ") too big, using default (" + memmax + ").");
+                        + ") too big, using default ("
+                        + MAX_HEAPSIZE_GB_DEFAULT + "g).");
       }
       else
       {
@@ -135,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 '" + maxHeapSizePropertyName + "' ("
+                "MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' ("
                         + jvmmemmaxarg
                         + ") too small, using minimum ("
-                        + applicationMinMemory + ").");
+                        + APPLICATION_MIN_MEMORY + ").");
       }
 
     }
@@ -155,10 +158,10 @@ public class MemorySetting
     }
 
     // get max percent of physical memory, starting with default
-    float percent = maxHeapSizePerCentDefault;
+    float percent = MAX_HEAPSIZE_PERCENT_DEFAULT;
     if (jvmmempcarg == null)
     {
-      jvmmempcarg = System.getProperty(maxHeapSizePerCentPropertyName);
+      jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME);
     }
     String jvmmempc = jvmmempcarg;
     long mempc = -1;
@@ -175,7 +178,7 @@ public class MemorySetting
         {
           System.out.println(
                   "MemorySetting Property '"
-                          + maxHeapSizePerCentPropertyName
+                          + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
                           + "' should be in range 1..100. Using default "
                           + percent + "%");
         }
@@ -183,7 +186,7 @@ public class MemorySetting
     } catch (NumberFormatException e)
     {
       System.out.println(
-              "MemorySetting property '" + maxHeapSizePerCentPropertyName
+              "MemorySetting property '" + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
                       + "' (" + jvmmempcarg + ") badly formatted");
     }
 
@@ -192,7 +195,7 @@ public class MemorySetting
     try
     {
       long physicalMem = GetMemory.getPhysicalMemory();
-      if (physicalMem > applicationMinMemory)
+      if (physicalMem > APPLICATION_MIN_MEMORY)
       {
         // try and set at least applicationMinMemory and thereafter ensure
         // leaveFreeMinMemory is left for the OS
@@ -201,33 +204,33 @@ public class MemorySetting
 
         // check for memory left for OS
         boolean reducedmempc = false;
-        if (physicalMem - mempc < leaveFreeMinMemory)
+        if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
         {
-          mempc = physicalMem - leaveFreeMinMemory;
+          mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
           reducedmempc = true;
           System.out.println("MemorySetting Property '"
-                  + maxHeapSizePerCentPropertyName + "' (" + jvmmempcarg
+                  + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
                   + ") too large. Leaving free space for OS and reducing to ("
                   + mempc + ").");
         }
 
         // check for minimum application memsize
-        if (mempc < applicationMinMemory)
+        if (mempc < APPLICATION_MIN_MEMORY)
         {
           if (reducedmempc)
           {
             System.out.println("Reduced MemorySetting (" + mempc
                     + ") too small. Increasing to application minimum ("
-                    + applicationMinMemory + ").");
+                    + APPLICATION_MIN_MEMORY + ").");
           }
           else
           {
             System.out.println("MemorySetting Property '"
-                    + maxHeapSizePerCentPropertyName + "' (" + jvmmempcarg
-                    + ") too small. Using minimum (" + applicationMinMemory
+                    + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
+                    + ") too small. Using minimum (" + APPLICATION_MIN_MEMORY
                     + ").");
           }
-          mempc = applicationMinMemory;
+          mempc = APPLICATION_MIN_MEMORY;
         }
       }
       else
@@ -236,7 +239,7 @@ public class MemorySetting
         mempc = physicalMem;
         System.out.println(
                 "Not enough physical memory for application. Ignoring MemorySetting Property '"
-                        + maxHeapSizePerCentPropertyName + "' ("
+                        + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
                         + jvmmempcarg
                         + "). Using maximum memory available ("
                         + physicalMem + ").");
@@ -257,13 +260,12 @@ public class MemorySetting
     // && (jvmmempcarg != null || (jvmmempcarg == null && jvmmemmaxarg == null))
     // the above is easier to understand but simplified to
             && !(jvmmempcarg == null && jvmmemmaxarg != null)
-            && memmax > noMemMaxHeapSizeDefault)
+            && memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE)
     {
       System.out.println(
-              "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;
+              "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 (mempc == -1) // percentage memory not set