From: Ben Soares Date: Mon, 20 May 2019 14:15:19 +0000 (+0100) Subject: JAL-3254 Improved memory setting (allows mem for OS). Moved OperatingSystemMXBean... X-Git-Tag: Release_2_11_1_0~53^2~11^2~3^2 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=8988e5070bf882959c09e325789c84f5ef4e1832;p=jalview.git JAL-3254 Improved memory setting (allows mem for OS). Moved OperatingSystemMXBean stuff to separate class. Class duplicated in getdown for same Memory Per Centage calculation. --- diff --git a/getdown/lib/getdown-core-1.8.3-SNAPSHOT.jar b/getdown/lib/getdown-core-1.8.3-SNAPSHOT.jar index 7230883..c09d3e4 100644 Binary files a/getdown/lib/getdown-core-1.8.3-SNAPSHOT.jar and b/getdown/lib/getdown-core-1.8.3-SNAPSHOT.jar differ diff --git a/getdown/lib/getdown-launcher.jar b/getdown/lib/getdown-launcher.jar index cb5f670..321b5ce 100644 Binary files a/getdown/lib/getdown-launcher.jar and b/getdown/lib/getdown-launcher.jar differ diff --git a/getdown/src/getdown/core/src/main/java/com/threerings/getdown/data/Application.java b/getdown/src/getdown/core/src/main/java/com/threerings/getdown/data/Application.java index 25cd109..0de5c8a 100644 --- a/getdown/src/getdown/core/src/main/java/com/threerings/getdown/data/Application.java +++ b/getdown/src/getdown/core/src/main/java/com/threerings/getdown/data/Application.java @@ -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)+"')"); } diff --git a/getdown/src/getdown/core/src/main/java/jalview/bin/MemorySetting.java b/getdown/src/getdown/core/src/main/java/jalview/bin/MemorySetting.java new file mode 100644 index 0000000..b3bae2d --- /dev/null +++ b/getdown/src/getdown/core/src/main/java/jalview/bin/MemorySetting.java @@ -0,0 +1,51 @@ +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; + } + +} diff --git a/gradle.properties b/gradle.properties index e49c3ab..7bfd72c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -59,7 +59,7 @@ getdown_txt_title = Jalview 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 diff --git a/j11lib/getdown-core.jar b/j11lib/getdown-core.jar index 7230883..c09d3e4 100644 Binary files a/j11lib/getdown-core.jar and b/j11lib/getdown-core.jar differ diff --git a/j8lib/getdown-core.jar b/j8lib/getdown-core.jar index 7230883..c09d3e4 100644 Binary files a/j8lib/getdown-core.jar and b/j8lib/getdown-core.jar differ diff --git a/src/jalview/bin/Launcher.java b/src/jalview/bin/Launcher.java index aec3acd..9957a40 100644 --- a/src/jalview/bin/Launcher.java +++ b/src/jalview/bin/Launcher.java @@ -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 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; } } diff --git a/src/jalview/bin/MemorySetting.java b/src/jalview/bin/MemorySetting.java new file mode 100644 index 0000000..b3bae2d --- /dev/null +++ b/src/jalview/bin/MemorySetting.java @@ -0,0 +1,51 @@ +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; + } + +}