3 private static String ADJUSTMENT_MESSAGE = null;
4 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
5 * Copyright (C) $$Year-Rel$$ The Jalview Authors
7 * This file is part of Jalview.
9 * Jalview is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation, either version 3
12 * of the License, or (at your option) any later version.
14 * Jalview is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty
16 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17 * PURPOSE. See the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
21 * The Jalview Authors are detailed in the 'AUTHORS' file.
26 * Methods to decide on appropriate memory setting for Jalview based on two
27 * optionally provided values: jvmmempc - the maximum percentage of total
28 * physical memory to allocate, and jvmmemmax - the maximum absolute amount of
29 * physical memory to allocate. These can be provided as arguments or system
30 * properties. Other considerations such as minimum application requirements and
31 * leaving space for OS are used too.
37 public class MemorySetting
39 public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc";
41 public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax";
43 private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; // 90%
45 private static final long GIGABYTE = 1073741824; // 1GB
47 public static final long LEAVE_FREE_MIN_MEMORY = GIGABYTE / 2;
49 public static final long APPLICATION_MIN_MEMORY = GIGABYTE / 2;
51 private static final long MAX_HEAPSIZE_GB_DEFAULT = 32;
53 private static final long NOMEM_MAX_HEAPSIZE_GB_DEFAULT = 8;
55 public static final String NS = "MEMORY";
57 public static final String CUSTOMISED_SETTINGS = NS
58 + "_CUSTOMISED_SETTINGS";
60 public static final String MEMORY_JVMMEMPC = NS + "_"
61 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.toUpperCase();
63 public static final String MEMORY_JVMMEMMAX = NS + "_"
64 + MAX_HEAPSIZE_PROPERTY_NAME.toUpperCase();
66 protected static boolean logToClassChecked = false;
68 public static String memorySuffixes = "bkmgt"; // order of the suffixes is
71 public static long getMemorySetting()
73 return getMemorySetting(null, null);
76 public static long getMemorySetting(String jvmmemmaxarg,
79 return getMemorySetting(jvmmemmaxarg, jvmmempcarg, true, false);
83 * Decide on appropriate memory setting for Jalview based on the two arguments
84 * values: jvmmempc - the maximum percentage of total physical memory to
85 * allocate, and jvmmemmax - the maximum absolute amount of physical memory to
86 * allocate. These can be provided as arguments. If not provided as arguments
87 * (or set as null) system properties will be used instead (if set). The
88 * memory setting returned will be the lower of the two values. If either of
89 * the values are not provided then defaults will be used (jvmmempc=90,
90 * jvmmemmax=32GB). If total physical memory can't be ascertained when
91 * jvmmempc was set or neither jvmmempc nor jvmmemmax were set, then jvmmemmax
92 * defaults to a much safer 8GB. In this case explicitly setting jvmmemmax and
93 * not setting jvmmempc can set a higher memory for Jalview. The calculation
94 * also tries to ensure 0.5GB memory for the OS, but also tries to ensure at
95 * least 0.5GB memory for Jalview (which takes priority over the OS) If there
96 * is less then 0.5GB of physical memory then the total physical memory is
100 * Maximum value of memory to set. This can be a numeric string
101 * optionally followed by "b", "k", "m", "g", "t" (case insensitive)
102 * to indicate bytes, kilobytes, megabytes, gigabytes, terabytes
103 * respectively. If null a default value of 32G will be used. If null
104 * and either physical memory can't be determined then the default is
107 * Max percentage of physical memory to use. Defaults to "90".
110 * boolean to decide whether to look at System properties.
112 * @return The amount of memory (in bytes) to allocate to Jalview
114 public static long getMemorySetting(String jvmmemmaxarg,
115 String jvmmempcarg, boolean useProps, boolean quiet)
117 // actual Xmx value-to-be
118 long maxMemLong = -1;
119 clearAdjustmentMessage();
121 // (absolute) jvmmaxmem setting, start with default
122 long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
123 if (jvmmemmaxarg == null && useProps)
125 jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME);
127 String jvmmemmax = jvmmemmaxarg;
128 if (jvmmemmax != null && jvmmemmax.length() > 0)
133 memmax = memoryStringToLong(jvmmemmax);
136 throw (new NumberFormatException("Not allowing 0"));
138 } catch (NumberFormatException e)
140 memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
141 setAdjustmentMessage("MemorySetting Property '"
142 + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg
143 + "') badly formatted or 0, using default ("
144 + MAX_HEAPSIZE_GB_DEFAULT + "g).", quiet);
147 // check at least minimum value (this accounts for negatives too)
148 if (memmax < APPLICATION_MIN_MEMORY)
150 memmax = APPLICATION_MIN_MEMORY;
151 setAdjustmentMessage("MemorySetting Property '"
152 + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg
153 + ") too small, using minimum (" + APPLICATION_MIN_MEMORY
160 // no need to warn if no setting
161 // adjustmentMessage("MemorySetting Property '" + maxHeapSizeProperty
166 // get max percent of physical memory, starting with default
167 float percent = MAX_HEAPSIZE_PERCENT_DEFAULT;
168 if (jvmmempcarg == null && useProps)
170 jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME);
172 String jvmmempc = jvmmempcarg;
176 if (jvmmempc != null)
178 int trypercent = Integer.parseInt(jvmmempc);
179 if (0 <= trypercent && trypercent <= 100)
181 percent = trypercent;
185 setAdjustmentMessage("MemorySetting Property '"
186 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
187 + "' should be in range 0..100. Using default " + percent
191 } catch (NumberFormatException e)
193 setAdjustmentMessage("MemorySetting property '"
194 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
195 + ") badly formatted", quiet);
198 // catch everything in case of no com.sun.management.OperatingSystemMXBean
199 boolean memoryPercentError = false;
202 long physicalMem = GetMemory.getPhysicalMemory();
203 if (physicalMem > APPLICATION_MIN_MEMORY)
205 // try and set at least applicationMinMemory and thereafter ensure
206 // leaveFreeMinMemory is left for the OS
208 mempc = (long) ((physicalMem / 100F) * percent);
210 // check for memory left for OS
211 boolean reducedmempc = false;
212 if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
214 mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
216 setAdjustmentMessage("MemorySetting Property '"
217 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
218 + ") too large. Leaving free space for OS and reducing to ("
219 + mempc + ").", quiet);
222 // check for minimum application memsize
223 if (mempc < APPLICATION_MIN_MEMORY)
227 setAdjustmentMessage("Reduced MemorySetting (" + mempc
228 + ") too small. Increasing to application minimum ("
229 + APPLICATION_MIN_MEMORY + ").", quiet);
233 setAdjustmentMessage("MemorySetting Property '"
234 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
235 + jvmmempcarg + ") too small. Using minimum ("
236 + APPLICATION_MIN_MEMORY + ").", quiet);
238 mempc = APPLICATION_MIN_MEMORY;
243 // not enough memory for application, just try and grab what we can!
245 setAdjustmentMessage(
246 "Not enough physical memory for application. Ignoring MemorySetting Property '"
247 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
249 + "). Using maximum memory available ("
250 + physicalMem + ").",
254 } catch (Throwable t)
256 memoryPercentError = true;
257 setAdjustmentMessage(
258 "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean",
263 // In the case of an error reading the percentage of physical memory (when
264 // jvmmempc was set OR neither jvmmempc nor jvmmemmax were set), let's cap
266 if (memoryPercentError && mempc == -1
267 && !(jvmmempcarg == null && jvmmemmaxarg != null) // the same as
274 && memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE)
276 setAdjustmentMessage(
277 "Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT
278 + "g due to failure to read physical memory size.",
280 memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
283 if (mempc == -1) // percentage memory not set
289 maxMemLong = Math.min(mempc, memmax);
295 public static boolean isValidMemoryString(String text)
297 if (text.length() > 0)
299 char lastChar = text.charAt(text.length() - 1);
300 char[] otherChars = text.substring(0, text.length() - 1)
302 for (char c : otherChars)
304 if (c < '0' || c > '9')
309 if ((lastChar < '0' || lastChar > '9') && memorySuffixes
310 .indexOf(Character.toLowerCase(lastChar)) == -1)
318 public static long memoryStringToLong(String memString)
319 throws NumberFormatException
321 if (!isValidMemoryString(memString)) // not valid
323 throw (new NumberFormatException("Not a valid memory string"));
325 char suffix = Character
326 .toLowerCase(memString.charAt(memString.length() - 1));
327 if ('0' <= suffix && suffix <= '9') // no suffix
329 return Long.valueOf(memString);
331 if (memorySuffixes.indexOf(suffix) == -1) // suffix is unknown
336 long multiplier = (long) Math.pow(2,
337 memorySuffixes.indexOf(suffix) * 10); // note order of suffixes in
338 // memorySuffixes important
340 // parse the arg. NumberFormatExceptions passed on to calling method
342 .parseLong(memString.substring(0, memString.length() - 1));
348 // apply multiplier only if result is not too big (i.e. bigger than a long)
349 if (Long.MAX_VALUE / mem > multiplier)
351 return multiplier * mem;
355 // number too big for a Long. Limit to Long.MAX_VALUE
356 System.out.println("Memory parsing of '" + memString
357 + "' produces number too big. Limiting to Long.MAX_VALUE="
359 return Long.MAX_VALUE;
363 public static String memoryLongToString(long mem)
365 return memoryLongToString(mem, "%.3f");
368 public static String memoryLongToString(long mem, String format)
374 for (int i = 0; i < memorySuffixes.length(); i++)
376 char s = Character.toUpperCase(memorySuffixes.charAt(i));
377 if (mem < (long) Math.pow(2, exponent + 10)
378 || i == memorySuffixes.length() - 1) // last suffix
381 num = (float) (mem / Math.pow(2, exponent));
387 return String.format(format, num) + suffix;
390 private static String ADJUSTMENT_MESSAGE = null;
392 private static void setAdjustmentMessage(String reason, boolean quiet)
394 ADJUSTMENT_MESSAGE = reason;
397 System.out.println(reason);
401 public static void clearAdjustmentMessage()
403 ADJUSTMENT_MESSAGE = null;
406 public static String getAdjustmentMessage()
408 return ADJUSTMENT_MESSAGE;