JAL-3210 removing overly complicated logging. Settling for System.out.println and...
[jalview.git] / getdown / src / getdown / core / src / main / java / jalview / bin / MemoryPercent.java
index 2de2157..90257d1 100644 (file)
@@ -21,33 +21,47 @@ public class MemoryPercent
     } catch (NoClassDefFoundError e)
     {
       // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
-      System.out.println("No com.sun.management.OperatingSystemMXBean");
+      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;
   }
 
-  public static long memPercent(int percent)
+  public static long memPercentAmount(int percent)
   {
-    return memPercent(percent);
+    return memPercentAmount((float)percent);
   }
-  public static long memPercent(float percent)
+  public static long memPercentAmount(float percent)
   {
-    long memPercent = -1;
+    long memPercentAmount = -1;
 
     long physicalMem = getPhysicalMemory();
     if (physicalMem > MemorySetting.applicationMinMemory)
     {
       // try and set at least applicationMinMemory and thereafter ensure
       // leaveFreeMinMemory is left for the OS
-      memPercent = Math.max(MemorySetting.applicationMinMemory,
-              (long) (physicalMem
-                      - Math.max(physicalMem * (100 - percent) / 100,
-                              MemorySetting.leaveFreeMinMemory)));
+
+      memPercentAmount = (long) ((physicalMem * percent) / 100F);
+
+      // check for memory left for OS
+      if (physicalMem - memPercentAmount < MemorySetting.leaveFreeMinMemory)
+      {
+        memPercentAmount = physicalMem - MemorySetting.leaveFreeMinMemory;
+      }
+
+      // check for minimum application memsize
+      if (memPercentAmount < MemorySetting.applicationMinMemory)
+      {
+        memPercentAmount = MemorySetting.applicationMinMemory;
+      }
+    } else {
+      // not enough memory for application, just try and grab what we can!
+      memPercentAmount = physicalMem;
     }
 
-    return memPercent;
+    return memPercentAmount;
   }
 
 }