X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fbin%2FMemorySetting.java;h=7849ba27e33cd1ba5e52fc011b6456caf85e5335;hb=f9cb557b667c61cab5a939b3c87506cdc7d8d26b;hp=f6924b9b9cdfb6369288aaaee05f48efe8acd918;hpb=38120f6e0031a59b21cd4ae9c94c9e83f1ebc6cd;p=jalview.git diff --git a/src/jalview/bin/MemorySetting.java b/src/jalview/bin/MemorySetting.java index f6924b9..7849ba2 100644 --- a/src/jalview/bin/MemorySetting.java +++ b/src/jalview/bin/MemorySetting.java @@ -1,20 +1,33 @@ 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 + public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc"; - public static final long applicationMinMemory = 536870912; // 0.5 GB + public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax"; - private final static int maxHeapSizePerCentDefault = 90; + private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; // 90% - public final static String maxHeapSizePerCentProperty = "jvmmempc"; + private static final long GIGABYTE = 1073741824; // 1GB - private final static long maxHeapSizeDefault = 34359738368L; // 32GB + public static final long LEAVE_FREE_MIN_MEMORY = GIGABYTE/2; - private final static long noMemMaxHeapSizeDefault = 8589934592L; // 8GB + public static final long APPLICATION_MIN_MEMORY = GIGABYTE/2; - public final static String maxHeapSizeProperty = "jvmmemmax"; + private static final long MAX_HEAPSIZE_GB_DEFAULT = 32; + + private static final long NOMEM_MAX_HEAPSIZE_GB_DEFAULT = 8; protected static boolean logToClassChecked = false; @@ -23,19 +36,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 - long memmax = maxHeapSizeDefault; - if (jvmmemmaxorig == null) + // (absolute) jvmmaxmem setting, start with default + long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE; + if (jvmmemmaxarg == null) { - jvmmemmaxorig = System.getProperty(maxHeapSizeProperty); + jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME); } - String jvmmemmax = jvmmemmaxorig; + String jvmmemmax = jvmmemmaxarg; if (jvmmemmax != null && jvmmemmax.length() > 0) { long multiplier = 1; @@ -71,22 +114,23 @@ public class MemorySetting memmax = Long.parseLong(jvmmemmax); } catch (NumberFormatException e) { - memmax = maxHeapSizeDefault; + memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE; System.out.println("MemorySetting Property '" - + maxHeapSizeProperty + + MAX_HEAPSIZE_PROPERTY_NAME + "' (" - + jvmmemmaxorig + "') badly formatted, using default (" - + memmax + ")."); + + jvmmemmaxarg + "') badly formatted, using default (" + + MAX_HEAPSIZE_GB_DEFAULT + "g)."); } // apply multiplier if not too big (i.e. bigger than a long) if (Long.MAX_VALUE / memmax < multiplier) { - memmax = maxHeapSizeDefault; + memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE; System.out.println( - "MemorySetting Property '" + maxHeapSizeProperty + "' (" - + jvmmemmaxorig - + ") too big, using default (" + memmax + ")."); + "MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + + jvmmemmaxarg + + ") too big, using default (" + + MAX_HEAPSIZE_GB_DEFAULT + "g)."); } else { @@ -94,14 +138,14 @@ public class MemorySetting } // check at least minimum value (this accounts for negatives too) - if (memmax < applicationMinMemory) + if (memmax < APPLICATION_MIN_MEMORY) { - memmax = applicationMinMemory; + memmax = APPLICATION_MIN_MEMORY; System.out.println( - "MemorySetting Property '" + maxHeapSizeProperty + "' (" - + jvmmemmaxorig + "MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + + jvmmemmaxarg + ") too small, using minimum (" - + applicationMinMemory + ")."); + + APPLICATION_MIN_MEMORY + ")."); } } @@ -113,18 +157,14 @@ public class MemorySetting // set."); } - // get max percent of physical memory - float percent = maxHeapSizePerCentDefault; - if (jvmmempcorig == null) - { - jvmmempcorig = System.getProperty(maxHeapSizePerCentProperty); - } - String jvmmempc = jvmmempcorig; - if (jvmmempc == null) + // get max percent of physical memory, starting with default + float percent = MAX_HEAPSIZE_PERCENT_DEFAULT; + if (jvmmempcarg == null) { - jvmmempc = System.getProperty(maxHeapSizePerCentProperty); + jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME); } - long pcmem = -1; + String jvmmempc = jvmmempcarg; + long mempc = -1; try { if (jvmmempc != null) @@ -137,83 +177,103 @@ public class MemorySetting else { System.out.println( - "MemorySetting Property '" + maxHeapSizePerCentProperty - + "' should be in range 1..100"); + "MemorySetting Property '" + + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + + "' should be in range 1..100. Using default " + + percent + "%"); } } } catch (NumberFormatException e) { System.out.println( - "MemorySetting property '" + maxHeapSizePerCentProperty - + "' (" + jvmmempc + ") badly formatted"); + "MemorySetting property '" + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + + "' (" + jvmmempcarg + ") badly formatted"); } // catch everything in case of no com.sun.management.OperatingSystemMXBean boolean memoryPercentError = false; try { - long physicalMem = MemoryPercent.getPhysicalMemory(); - if (physicalMem > applicationMinMemory) + long physicalMem = GetMemory.getPhysicalMemory(); + if (physicalMem > APPLICATION_MIN_MEMORY) { // 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 < LEAVE_FREE_MIN_MEMORY) { - pcmem = physicalMem - leaveFreeMinMemory; + mempc = physicalMem - LEAVE_FREE_MIN_MEMORY; + reducedmempc = true; System.out.println("MemorySetting Property '" - + maxHeapSizePerCentProperty + "' (" + jvmmempcorig - + ") too large. Leaving free space for OS, using (" - + pcmem + ")."); + + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg + + ") too large. Leaving free space for OS and reducing to (" + + mempc + ")."); } // check for minimum application memsize - if (pcmem < applicationMinMemory) + if (mempc < APPLICATION_MIN_MEMORY) { - 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 (" + + APPLICATION_MIN_MEMORY + ")."); + } + else + { + System.out.println("MemorySetting Property '" + + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg + + ") too small. Using minimum (" + APPLICATION_MIN_MEMORY + + ")."); + } + mempc = APPLICATION_MIN_MEMORY; } } 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 '" + + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + + 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 - && memmax > noMemMaxHeapSizeDefault) + + // 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 && jvmmemmaxarg != null) // the same as (jvmmempcarg != null || (jvmmempcarg == null && jvmmemmaxarg + // == null)) + && memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE) { System.out.println( - "Capping maximum memory to 8GB due to failure to read physical memory size."); - memmax = noMemMaxHeapSizeDefault; + "Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT + + "g due to failure to read physical memory size."); + memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE; } - 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;