JAL-3278 A system.out.println turned into a cache.log.error
[jalview.git] / src / jalview / bin / Launcher.java
index b595b4f..412f119 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,9 @@ 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 maxHeapSizePerCentProperty = "jvmmempc";
 
   private final static String dockIconPath = "JalviewLogo_Huge.png";
 
@@ -22,6 +24,8 @@ public class Launcher
     ArrayList<String> command = new ArrayList<>();
     command.add(javaBin);
 
+    String memSetting = null;
+
     boolean isAMac = System.getProperty("os.name").indexOf("Mac") > -1;
 
     for (String jvmArg : ManagementFactory.getRuntimeMXBean()
@@ -40,39 +44,81 @@ 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);
       if (arg.startsWith("-Xmx"))
       {
+        memSetting = arg;
         memSet = true;
       }
       else if (arg.startsWith("-Xdock:icon"))
       {
         dockIcon = true;
       }
+      else if (arg.startsWith("-Xdock:name"))
+      {
+        dockName = true;
+      }
     }
 
     if (!memSet)
     {
       long maxMemLong = -1;
-      long physicalMem = getPhysicalMemory();
-      if (physicalMem > 0)
+      int percent = maxHeapSizePerCent;
+      String jvmmempc = System.getProperty(maxHeapSizePerCentProperty);
+      try
+      {
+        if (jvmmempc != null)
+        {
+          int trypercent = Integer.parseInt(jvmmempc);
+          if (0 < trypercent && trypercent <= 100)
+          {
+            percent = trypercent;
+          }
+          else
+          {
+            System.out.println("Property '" + maxHeapSizePerCentProperty
+                    + "' should be in range 1..100");
+          }
+        }
+      } catch (Exception e)
+      {
+        System.out.println("Error parsing " + maxHeapSizePerCentProperty
+                + " '" + jvmmempc + "'");
+      }
+
+      try
       {
-        maxMemLong = physicalMem * maxHeapSizePerCent / 100;
+        maxMemLong = MemorySetting.memPercent(percent);
+      } catch (Exception e)
+      {
+        e.printStackTrace();
+      } catch (Throwable t)
+      {
+        t.printStackTrace();
       }
+
       if (maxMemLong > 0)
       {
-        command.add("-Xmx" + Long.toString(maxMemLong));
+        memSetting = "-Xmx" + Long.toString(maxMemLong);
+        command.add(memSetting);
       }
     }
 
-    if (!dockIcon)
+    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);
@@ -80,40 +126,53 @@ public class Launcher
 
     final ProcessBuilder builder = new ProcessBuilder(command);
 
-    System.out.println("COMMAND: " + String.join(" ", builder.command()));
+    // System.out.println("COMMAND: " + String.join(" ", builder.command()));
+    System.out.println("Running " + startClass + " with "
+            + (memSetting == null ? "no memSetting" : memSetting));
 
     try
     {
       builder.inheritIO();
-      builder.start();
-    } catch (Exception e)
+      Process process = builder.start();
+      process.waitFor();
+    } catch (IOException e)
     {
-      e.printStackTrace();
-    }
-    // System.exit(0);
-
-  }
-
-  public static long getPhysicalMemory()
-  {
-    final OperatingSystemMXBean o = ManagementFactory
-            .getOperatingSystemMXBean();
-
-    try
-    {
-      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;
   }
 
 }