From: Ben Soares Date: Thu, 9 Jan 2020 16:11:57 +0000 (+0000) Subject: JAL-3477 New jalview.bin.MemorySetting jalview.bin.GetMemory source files for getdown X-Git-Tag: Develop-2_11_2_0-d20201215~88^2~7 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=601332dce50381d7d91181243a14cdd92a81e60a;p=jalview.git JAL-3477 New jalview.bin.MemorySetting jalview.bin.GetMemory source files for getdown --- diff --git a/getdown/src/getdown/core/src/main/java/jalview/bin/MemoryPercent.java b/getdown/src/getdown/core/src/main/java/jalview/bin/GetMemory.java similarity index 51% rename from getdown/src/getdown/core/src/main/java/jalview/bin/MemoryPercent.java rename to getdown/src/getdown/core/src/main/java/jalview/bin/GetMemory.java index 3288e66..26c3d5a 100644 --- a/getdown/src/getdown/core/src/main/java/jalview/bin/MemoryPercent.java +++ b/getdown/src/getdown/core/src/main/java/jalview/bin/GetMemory.java @@ -3,9 +3,29 @@ package jalview.bin; import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; -public class MemoryPercent +/** + * Isolated class to ascertain physical memory of the system using + * com.sun.management.OperatingSystemMXBean class's getTotalPhysicalMemorySize + * method. This class is present in OpenJDK 8,9,10,11,12,13. It is present but + * marked as deprecated in the early-access(30) release of OpenJDK 14. In case + * of an alternative/unsupported JRE being used or the class/method not being + * implemented in an exotic architecture JRE this call has been isolated into + * this separate class. + * + * @author bsoares + * + */ +public class GetMemory { + /** + * Wrapper for + * com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize() + * + * @return Result of + * com.sun.management.OperatingSystemMXBean.getTotalPhysicalMemorySize() + * or -1 if this class is not present in the JRE. + */ protected static long getPhysicalMemory() { final OperatingSystemMXBean o = ManagementFactory 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 index f6924b9..117be25 100644 --- a/getdown/src/getdown/core/src/main/java/jalview/bin/MemorySetting.java +++ b/getdown/src/getdown/core/src/main/java/jalview/bin/MemorySetting.java @@ -1,5 +1,16 @@ package jalview.bin; +/** + * Methods to decide on appropriate memory setting for Jalview based on two + * optionally provided values: jvmmempc - the maximum percentage of total + * physical memory to allocate, and jvmmemmax - the maximum absolute amount of + * physical memory to allocate. These can be provided as arguments or system + * properties. Other considerations such as minimum application requirements and + * leaving space for OS are used too. + * + * @author bsoares + * + */ public class MemorySetting { public static final long leaveFreeMinMemory = 536870912; // 0.5 GB @@ -8,13 +19,13 @@ public class MemorySetting private final static int maxHeapSizePerCentDefault = 90; - public final static String maxHeapSizePerCentProperty = "jvmmempc"; + public final static String maxHeapSizePerCentPropertyName = "jvmmempc"; private final static long maxHeapSizeDefault = 34359738368L; // 32GB private final static long noMemMaxHeapSizeDefault = 8589934592L; // 8GB - public final static String maxHeapSizeProperty = "jvmmemmax"; + public final static String maxHeapSizePropertyName = "jvmmemmax"; protected static boolean logToClassChecked = false; @@ -23,19 +34,49 @@ public class MemorySetting return getMemorySetting(null, null); } - public static long getMemorySetting(String jvmmemmaxorig, - String jvmmempcorig) + /** + * Decide on appropriate memory setting for Jalview based on the two arguments + * values: jvmmempc - the maximum percentage of total physical memory to + * allocate, and jvmmemmax - the maximum absolute amount of physical memory to + * allocate. These can be provided as arguments. If not provided as arguments + * (or set as null) system properties will be used instead (if set). The memory + * setting returned will be the lower of the two values. If either of the values + * are not provided then defaults will be used (jvmmempc=90, jvmmemmax=32GB). If + * total physical memory can't be ascertained when jvmmempc was set or neither + * jvmmempc nor jvmmemmax were set, then jvmmemmax defaults to a much safer 8GB. + * In this case explicitly setting jvmmemmax and not setting jvmmempc can set a + * higher memory for Jalview. The calculation also tries to ensure 0.5GB memory + * for the OS, but also tries to ensure at least 0.5GB memory for Jalview (which + * takes priority over the OS) If there is less then 0.5GB of physical memory + * then the total physical memory is used for Jalview. + * + * @param jvmmemmaxarg + * Maximum value of memory to set. This can be a numeric + * string optionally followed by "b", "k", "m", "g", "t" + * (case insensitive) to indicate bytes, kilobytes, + * megabytes, gigabytes, terabytes respectively. If null a + * default value of 32G will be used. If null and either + * physical memory can't be determined then the default is + * 8GB. + * @param jvmmempcarg + * Max percentage of physical memory to use. Defaults to + * "90". + * + * @return The amount of memory (in bytes) to allocate to Jalview + */ + public static long getMemorySetting(String jvmmemmaxarg, + String jvmmempcarg) { // actual Xmx value-to-be long maxMemLong = -1; - // get (absolute) jvmmaxmem setting + // (absolute) jvmmaxmem setting, start with default long memmax = maxHeapSizeDefault; - if (jvmmemmaxorig == null) + if (jvmmemmaxarg == null) { - jvmmemmaxorig = System.getProperty(maxHeapSizeProperty); + jvmmemmaxarg = System.getProperty(maxHeapSizePropertyName); } - String jvmmemmax = jvmmemmaxorig; + String jvmmemmax = jvmmemmaxarg; if (jvmmemmax != null && jvmmemmax.length() > 0) { long multiplier = 1; @@ -73,9 +114,9 @@ public class MemorySetting { memmax = maxHeapSizeDefault; System.out.println("MemorySetting Property '" - + maxHeapSizeProperty + + maxHeapSizePropertyName + "' (" - + jvmmemmaxorig + "') badly formatted, using default (" + + jvmmemmaxarg + "') badly formatted, using default (" + memmax + ")."); } @@ -84,8 +125,8 @@ public class MemorySetting { memmax = maxHeapSizeDefault; System.out.println( - "MemorySetting Property '" + maxHeapSizeProperty + "' (" - + jvmmemmaxorig + "MemorySetting Property '" + maxHeapSizePropertyName + "' (" + + jvmmemmaxarg + ") too big, using default (" + memmax + ")."); } else @@ -98,8 +139,8 @@ public class MemorySetting { memmax = applicationMinMemory; System.out.println( - "MemorySetting Property '" + maxHeapSizeProperty + "' (" - + jvmmemmaxorig + "MemorySetting Property '" + maxHeapSizePropertyName + "' (" + + jvmmemmaxarg + ") too small, using minimum (" + applicationMinMemory + ")."); } @@ -113,18 +154,14 @@ public class MemorySetting // set."); } - // get max percent of physical memory + // get max percent of physical memory, starting with default float percent = maxHeapSizePerCentDefault; - if (jvmmempcorig == null) + if (jvmmempcarg == null) { - jvmmempcorig = System.getProperty(maxHeapSizePerCentProperty); + jvmmempcarg = System.getProperty(maxHeapSizePerCentPropertyName); } - String jvmmempc = jvmmempcorig; - if (jvmmempc == null) - { - jvmmempc = System.getProperty(maxHeapSizePerCentProperty); - } - long pcmem = -1; + String jvmmempc = jvmmempcarg; + long mempc = -1; try { if (jvmmempc != null) @@ -137,83 +174,105 @@ public class MemorySetting else { System.out.println( - "MemorySetting Property '" + maxHeapSizePerCentProperty - + "' should be in range 1..100"); + "MemorySetting Property '" + + maxHeapSizePerCentPropertyName + + "' should be in range 1..100. Using default " + + percent + "%"); } } } catch (NumberFormatException e) { System.out.println( - "MemorySetting property '" + maxHeapSizePerCentProperty - + "' (" + jvmmempc + ") badly formatted"); + "MemorySetting property '" + maxHeapSizePerCentPropertyName + + "' (" + jvmmempcarg + ") badly formatted"); } // catch everything in case of no com.sun.management.OperatingSystemMXBean boolean memoryPercentError = false; try { - long physicalMem = MemoryPercent.getPhysicalMemory(); + long physicalMem = GetMemory.getPhysicalMemory(); if (physicalMem > applicationMinMemory) { // try and set at least applicationMinMemory and thereafter ensure // leaveFreeMinMemory is left for the OS - pcmem = (long) ((physicalMem * percent) / 100F); + mempc = (long) ((physicalMem / 100F) * percent); // check for memory left for OS - if (physicalMem - pcmem < leaveFreeMinMemory) + boolean reducedmempc = false; + if (physicalMem - mempc < leaveFreeMinMemory) { - pcmem = physicalMem - leaveFreeMinMemory; + mempc = physicalMem - leaveFreeMinMemory; + reducedmempc = true; System.out.println("MemorySetting Property '" - + maxHeapSizePerCentProperty + "' (" + jvmmempcorig - + ") too large. Leaving free space for OS, using (" - + pcmem + ")."); + + maxHeapSizePerCentPropertyName + "' (" + jvmmempcarg + + ") too large. Leaving free space for OS and reducing to (" + + mempc + ")."); } // check for minimum application memsize - if (pcmem < applicationMinMemory) + if (mempc < applicationMinMemory) { - pcmem = applicationMinMemory; - System.out.println("MemorySetting Property '" - + maxHeapSizePerCentProperty + "' (" + jvmmempcorig - + ") too small, using minimum (" + applicationMinMemory - + ")."); + if (reducedmempc) + { + System.out.println("Reduced MemorySetting (" + mempc + + ") too small. Increasing to application minimum (" + + applicationMinMemory + ")."); + } + else + { + System.out.println("MemorySetting Property '" + + maxHeapSizePerCentPropertyName + "' (" + jvmmempcarg + + ") too small. Using minimum (" + applicationMinMemory + + ")."); + } + mempc = applicationMinMemory; } } else { // not enough memory for application, just try and grab what we can! - pcmem = physicalMem; - System.out.println("MemorySetting Property '" - + maxHeapSizePerCentProperty + "' (" + jvmmempcorig - + "): Not enough memory, using max available (" + pcmem - + ")."); + mempc = physicalMem; + System.out.println( + "Not enough physical memory for application. Ignoring MemorySetting Property '" + + maxHeapSizePerCentPropertyName + "' (" + + jvmmempcarg + + "). Using maximum memory available (" + + physicalMem + ")."); } } catch (Throwable t) { memoryPercentError = true; - System.out.println("Problem calling MemoryPercent.memPercent(" - + percent - + "). Likely to be problem with com.sun.management.OperatingSystemMXBean"); + System.out.println( + "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean"); t.printStackTrace(); } - // In the case of an error reading the percentage of physical memory (when jvmmempc was set), let's cap maxMemLong to 8GB - if (memoryPercentError && jvmmempc != null && pcmem == -1 + + // In the case of an error reading the percentage of physical memory (when + // jvmmempc was set OR neither jvmmempc nor jvmmemmax were set), let's cap + // maxMemLong to 8GB + if (memoryPercentError && mempc == -1 + // && (jvmmempcarg != null || (jvmmempcarg == null && jvmmemmaxarg == null)) + // the above is easier to understand but simplified to + && !(jvmmempcarg == null && jvmmemmaxarg != null) && memmax > noMemMaxHeapSizeDefault) { System.out.println( - "Capping maximum memory to 8GB due to failure to read physical memory size."); + "Capping maximum memory to " + + (noMemMaxHeapSizeDefault + 536870912) / 1073741824 // this is the nearest integer GB for noMemMaxHeapSizeDefault + + "GB due to failure to read physical memory size."); memmax = noMemMaxHeapSizeDefault; } - if (pcmem == -1) // not set + if (mempc == -1) // percentage memory not set { maxMemLong = memmax; } else { - maxMemLong = Math.min(pcmem, memmax); + maxMemLong = Math.min(mempc, memmax); } return maxMemLong;