JAL-3477 A bit of refactoring for the better
[jalview.git] / getdown / src / getdown / core / src / main / java / jalview / bin / MemorySetting.java
index 561fea5..f6924b9 100644 (file)
@@ -23,15 +23,14 @@ public class MemorySetting
     return getMemorySetting(null, null);
   }
 
-  public static long getMemorySetting(String jvmmemmaxString,
-          String jvmmempcString)
+  public static long getMemorySetting(String jvmmemmaxorig,
+          String jvmmempcorig)
   {
     // actual Xmx value-to-be
     long maxMemLong = -1;
 
     // get (absolute) jvmmaxmem setting
     long memmax = maxHeapSizeDefault;
-    String jvmmemmaxorig = jvmmemmaxString;
     if (jvmmemmaxorig == null)
     {
       jvmmemmaxorig = System.getProperty(maxHeapSizeProperty);
@@ -95,13 +94,14 @@ 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 + ").");
+                        + ") too small, using minimum ("
+                        + applicationMinMemory + ").");
       }
 
     }
@@ -115,7 +115,11 @@ public class MemorySetting
 
     // get max percent of physical memory
     float percent = maxHeapSizePerCentDefault;
-    String jvmmempc = jvmmempcString;
+    if (jvmmempcorig == null)
+    {
+      jvmmempcorig = System.getProperty(maxHeapSizePerCentProperty);
+    }
+    String jvmmempc = jvmmempcorig;
     if (jvmmempc == null)
     {
       jvmmempc = System.getProperty(maxHeapSizePerCentProperty);
@@ -148,7 +152,44 @@ public class MemorySetting
     boolean memoryPercentError = false;
     try
     {
-      pcmem = MemoryPercent.memPercentAmount(percent);
+      long physicalMem = MemoryPercent.getPhysicalMemory();
+      if (physicalMem > applicationMinMemory)
+      {
+        // try and set at least applicationMinMemory and thereafter ensure
+        // leaveFreeMinMemory is left for the OS
+
+        pcmem = (long) ((physicalMem * percent) / 100F);
+
+        // check for memory left for OS
+        if (physicalMem - pcmem < leaveFreeMinMemory)
+        {
+          pcmem = physicalMem - leaveFreeMinMemory;
+          System.out.println("MemorySetting Property '"
+                  + maxHeapSizePerCentProperty + "' (" + jvmmempcorig
+                  + ") too large. Leaving free space for OS, using ("
+                  + pcmem + ").");
+        }
+
+        // check for minimum application memsize
+        if (pcmem < applicationMinMemory)
+        {
+          pcmem = applicationMinMemory;
+          System.out.println("MemorySetting Property '"
+                  + maxHeapSizePerCentProperty + "' (" + jvmmempcorig
+                  + ") too small, using minimum (" + applicationMinMemory
+                  + ").");
+        }
+      }
+      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
+                + ").");
+      }
+
     } catch (Throwable t)
     {
       memoryPercentError = true;
@@ -178,4 +219,4 @@ public class MemorySetting
     return maxMemLong;
   }
 
-}
+}
\ No newline at end of file