Merge branch 'task/JAL-3247_JAL-3246_JAL-3254_JAL-3236_merge' into task/JAL-3141_JAL...
[jalview.git] / getdown / src / getdown / core / src / main / java / com / threerings / getdown / data / Application.java
index 25cd109..0de5c8a 100644 (file)
@@ -26,6 +26,7 @@ import java.util.zip.GZIPInputStream;
 import com.sun.management.OperatingSystemMXBean;
 import java.lang.management.ManagementFactory;
 
+import jalview.bin.MemorySetting;
 
 import com.threerings.getdown.util.*;
 // avoid ambiguity with java.util.Base64 which we can't use as it's 1.8+
@@ -753,28 +754,34 @@ public class Application
             jvmmempc = config.getInt(appPrefix + "jvmmempc", jvmmempc);
         }
         if (0 <= jvmmempc && jvmmempc <= 100) {
-            final Object o = ManagementFactory.getOperatingSystemMXBean();
-
-            try {
-                if (o instanceof OperatingSystemMXBean) {
-                    final OperatingSystemMXBean osb = (OperatingSystemMXBean) o;
-                    long physicalMem = osb.getTotalPhysicalMemorySize();
-                    long requestedMem = physicalMem*jvmmempc/100;
-                    String[] maxMemHeapArg = new String[]{"-Xmx"+Long.toString(requestedMem)};
-                    // remove other max heap size arg
-                    ARG: for (int i = 0; i < _jvmargs.size(); i++) {
-                           if (_jvmargs.get(i) instanceof java.lang.String && _jvmargs.get(i).startsWith("-Xmx")) {
-                                _jvmargs.remove(i);
-                           }
-                    }
-                    addAll(maxMemHeapArg, _jvmargs);
+          
+          long maxMemLong = -1;
+
+          try
+          {
+            maxMemLong = MemorySetting.memPercent(jvmmempc);
+          } catch (Exception e)
+          {
+            e.printStackTrace();
+          } catch (Throwable t)
+          {
+            t.printStackTrace();
+          }
 
-                }
-            }
-            catch (NoClassDefFoundError e) {
-                // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
-                System.out.println("No com.sun.management.OperatingSystemMXBean. Cannot use 'jvmmempc'.");
+          if (maxMemLong > 0)
+          {
+            
+            String[] maxMemHeapArg = new String[]{"-Xmx"+Long.toString(maxMemLong)};
+            // remove other max heap size arg
+            ARG: for (int i = 0; i < _jvmargs.size(); i++) {
+              if (_jvmargs.get(i) instanceof java.lang.String && _jvmargs.get(i).startsWith("-Xmx")) {
+                _jvmargs.remove(i);
+              }
             }
+            addAll(maxMemHeapArg, _jvmargs);
+            
+          }
+
         } else if (jvmmempc != -1) {
           System.out.println("'jvmmempc' value must be in range 0 to 100 (read as '"+Integer.toString(jvmmempc)+"')");
         }