JAL-3254 Improved memory setting (allows mem for OS). Moved OperatingSystemMXBean...
[jalview.git] / src / jalview / bin / Launcher.java
index aec3acd..9957a40 100644 (file)
@@ -1,8 +1,8 @@
 package jalview.bin;
 
 import java.io.File;
+import java.io.IOException;
 import java.lang.management.ManagementFactory;
-import java.lang.management.OperatingSystemMXBean;
 import java.util.ArrayList;
 
 public class Launcher
@@ -10,7 +10,7 @@ public class Launcher
 
   private final static String startClass = "jalview.bin.Jalview";
 
-  private final static int maxHeapSizePerCent = 95;
+  private final static int maxHeapSizePerCent = 90;
 
   private final static String dockIconPath = "JalviewLogo_Huge.png";
 
@@ -40,6 +40,7 @@ public class Launcher
     // add memory setting if not specified
     boolean memSet = false;
     boolean dockIcon = false;
+    boolean dockName = false;
     ARG: for (int i = 0; i < command.size(); i++)
     {
       String arg = command.get(i);
@@ -51,28 +52,45 @@ public class Launcher
       {
         dockIcon = true;
       }
+      else if (arg.startsWith("-Xdock:name"))
+      {
+        dockName = true;
+      }
     }
 
     if (!memSet)
     {
       long maxMemLong = -1;
-      long physicalMem = getPhysicalMemory();
-      if (physicalMem > 0)
+
+      try
+      {
+        maxMemLong = MemorySetting.memPercent(maxHeapSizePerCent);
+      } catch (Exception e)
       {
-        maxMemLong = physicalMem * maxHeapSizePerCent / 100;
+        e.printStackTrace();
+      } catch (Throwable t)
+      {
+        t.printStackTrace();
       }
+
       if (maxMemLong > 0)
       {
         command.add("-Xmx" + Long.toString(maxMemLong));
       }
     }
 
-    if (!dockIcon && isAMac)
+    if (isAMac)
     {
-      command.add("-Xdock:icon=" + dockIconPath);
-      // -Xdock:name=... doesn't actually work :(
-      // Leaving it in in case it gets fixed
-      command.add("-Xdock:name=" + "Jalview");
+      if (!dockIcon)
+      {
+        command.add("-Xdock:icon=" + dockIconPath);
+      }
+      if (!dockName)
+      {
+        // -Xdock:name=... doesn't actually work :(
+        // Leaving it in in case it gets fixed
+        command.add("-Xdock:name=" + "Jalview");
+      }
     }
 
     command.add(startClass);
@@ -87,34 +105,44 @@ public class Launcher
       builder.inheritIO();
       Process process = builder.start();
       process.waitFor();
-    } catch (Exception e)
-    {
-      e.printStackTrace();
-    }
-    // System.exit(0);
-
-  }
-
-  public static long getPhysicalMemory()
-  {
-    final OperatingSystemMXBean o = ManagementFactory
-            .getOperatingSystemMXBean();
-
-    try
+    } catch (IOException e)
     {
-      if (o instanceof com.sun.management.OperatingSystemMXBean)
+      if (e.getMessage().toLowerCase().contains("memory"))
+      {
+        System.out.println("Caught a memory exception: " + e.getMessage());
+        // Probably the "Cannot allocate memory" error, try without the memory setting
+        ArrayList<String> commandNoMem = new ArrayList<>();
+        for (int i = 0; i < command.size(); i++)
+        {
+          if (!command.get(i).startsWith("-Xmx"))
+          {
+            commandNoMem.add(command.get(i));
+          }
+        }
+        final ProcessBuilder builderNoMem = new ProcessBuilder(
+                commandNoMem);
+        System.out.println("NO MEM COMMAND: "
+                + String.join(" ", builderNoMem.command()));
+        try
+        {
+          builderNoMem.inheritIO();
+          Process processNoMem = builderNoMem.start();
+          processNoMem.waitFor();
+        } catch (Exception ex)
+        {
+          ex.printStackTrace();
+        }
+      }
+      else
       {
-        final com.sun.management.OperatingSystemMXBean osb = (com.sun.management.OperatingSystemMXBean) o;
-        return osb.getTotalPhysicalMemorySize();
+        e.printStackTrace();
       }
-    } catch (NoClassDefFoundError e)
+    } catch (Exception e)
     {
-      // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
-      System.out.println("No com.sun.management.OperatingSystemMXBean");
+      e.printStackTrace();
     }
+    // System.exit(0);
 
-    // We didn't get a com.sun.management.OperatingSystemMXBean.
-    return -1;
   }
 
 }