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+
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)+"')");
}
--- /dev/null
+package jalview.bin;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.OperatingSystemMXBean;
+
+public class MemorySetting
+{
+ public static final long leaveFreeMinMemory = 536870912; // 0.5 GB
+
+ public static final long applicationMinMemory = 536870912; // 0.5 GB
+
+ protected static long getPhysicalMemory()
+ {
+ final OperatingSystemMXBean o = ManagementFactory
+ .getOperatingSystemMXBean();
+
+ try
+ {
+ if (o instanceof com.sun.management.OperatingSystemMXBean)
+ {
+ final com.sun.management.OperatingSystemMXBean osb = (com.sun.management.OperatingSystemMXBean) o;
+ return osb.getTotalPhysicalMemorySize();
+ }
+ } catch (NoClassDefFoundError e)
+ {
+ // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
+ System.out.println("No com.sun.management.OperatingSystemMXBean");
+ }
+
+ // We didn't get a com.sun.management.OperatingSystemMXBean.
+ return -1;
+ }
+
+ public static long memPercent(int percent)
+ {
+ long memPercent = -1;
+
+ long physicalMem = getPhysicalMemory();
+ if (physicalMem > applicationMinMemory)
+ {
+ // try and set at least applicationMinMemory and thereafter ensure
+ // leaveFreeMinMemory is left for the OS
+ memPercent = Math.max(applicationMinMemory,
+ physicalMem - Math.max(physicalMem * (100 - percent) / 100,
+ leaveFreeMinMemory));
+ }
+
+ return memPercent;
+ }
+
+}
getdown_channel_base = http://www.jalview.org/getdown/jalview
getdown_channel_name = TEST
getdown_txt_allow_offline = true
-getdown_txt_jvmmempc = 95
+getdown_txt_jalview.jvmmempc = 90
getdown_txt_multi_jvmarg = -Dgetdownappdir="%APPDIR%"
getdown_txt_strict_comments = true
getdown_txt_title = Jalview
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
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";
// 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);
{
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);
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;
}
}
--- /dev/null
+package jalview.bin;
+
+import java.lang.management.ManagementFactory;
+import java.lang.management.OperatingSystemMXBean;
+
+public class MemorySetting
+{
+ public static final long leaveFreeMinMemory = 536870912; // 0.5 GB
+
+ public static final long applicationMinMemory = 536870912; // 0.5 GB
+
+ protected static long getPhysicalMemory()
+ {
+ final OperatingSystemMXBean o = ManagementFactory
+ .getOperatingSystemMXBean();
+
+ try
+ {
+ if (o instanceof com.sun.management.OperatingSystemMXBean)
+ {
+ final com.sun.management.OperatingSystemMXBean osb = (com.sun.management.OperatingSystemMXBean) o;
+ return osb.getTotalPhysicalMemorySize();
+ }
+ } catch (NoClassDefFoundError e)
+ {
+ // com.sun.management.OperatingSystemMXBean doesn't exist in this JVM
+ System.out.println("No com.sun.management.OperatingSystemMXBean");
+ }
+
+ // We didn't get a com.sun.management.OperatingSystemMXBean.
+ return -1;
+ }
+
+ public static long memPercent(int percent)
+ {
+ long memPercent = -1;
+
+ long physicalMem = getPhysicalMemory();
+ if (physicalMem > applicationMinMemory)
+ {
+ // try and set at least applicationMinMemory and thereafter ensure
+ // leaveFreeMinMemory is left for the OS
+ memPercent = Math.max(applicationMinMemory,
+ physicalMem - Math.max(physicalMem * (100 - percent) / 100,
+ leaveFreeMinMemory));
+ }
+
+ return memPercent;
+ }
+
+}