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.
25 import java.util.Locale;
28 * Methods to decide on appropriate memory setting for Jalview based on two
29 * optionally provided values: jvmmempc - the maximum percentage of total
30 * physical memory to allocate, and jvmmemmax - the maximum absolute amount of
31 * physical memory to allocate. These can be provided as arguments or system
32 * properties. Other considerations such as minimum application requirements and
33 * leaving space for OS are used too.
38 public class MemorySetting
40 public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc";
42 public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax";
44 private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; // 90%
46 private static final long GIGABYTE = 1073741824; // 1GB
48 public static final long LEAVE_FREE_MIN_MEMORY = GIGABYTE / 2;
50 public static final long APPLICATION_MIN_MEMORY = GIGABYTE / 2;
52 private static final long MAX_HEAPSIZE_GB_DEFAULT = 32;
54 private static final long NOMEM_MAX_HEAPSIZE_GB_DEFAULT = 8;
56 public static final String NS = "MEMORY";
58 public static final String CUSTOMISED_SETTINGS = NS
59 + "_CUSTOMISED_SETTINGS";
61 public static final String MEMORY_JVMMEMPC = NS + "_"
62 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.toUpperCase(Locale.ROOT);
64 public static final String MEMORY_JVMMEMMAX = NS + "_"
65 + MAX_HEAPSIZE_PROPERTY_NAME.toUpperCase(Locale.ROOT);
67 protected static boolean logToClassChecked = false;
69 public static String memorySuffixes = "bkmgt"; // order of the suffixes is
72 public static long getMemorySetting()
74 return getMemorySetting(null, null);
77 public static long getMemorySetting(String jvmmemmaxarg,
80 return getMemorySetting(jvmmemmaxarg, jvmmempcarg, true, false);
84 * Decide on appropriate memory setting for Jalview based on the two arguments
85 * values: jvmmempc - the maximum percentage of total physical memory to
86 * allocate, and jvmmemmax - the maximum absolute amount of physical memory to
87 * allocate. These can be provided as arguments. If not provided as arguments
88 * (or set as null) system properties will be used instead (if set). The
89 * memory setting returned will be the lower of the two values. If either of
90 * the values are not provided then defaults will be used (jvmmempc=90,
91 * jvmmemmax=32GB). If total physical memory can't be ascertained when
92 * jvmmempc was set or neither jvmmempc nor jvmmemmax were set, then jvmmemmax
93 * defaults to a much safer 8GB. In this case explicitly setting jvmmemmax and
94 * not setting jvmmempc can set a higher memory for Jalview. The calculation
95 * also tries to ensure 0.5GB memory for the OS, but also tries to ensure at
96 * least 0.5GB memory for Jalview (which takes priority over the OS) If there
97 * is less then 0.5GB of physical memory then the total physical memory is
100 * @param jvmmemmaxarg
101 * Maximum value of memory to set. This can be a numeric string
102 * optionally followed by "b", "k", "m", "g", "t" (case insensitive)
103 * to indicate bytes, kilobytes, megabytes, gigabytes, terabytes
104 * respectively. If null a default value of 32G will be used. If null
105 * and either physical memory can't be determined then the default is
108 * Max percentage of physical memory to use. Defaults to "90".
111 * boolean to decide whether to look at System properties.
113 * @return The amount of memory (in bytes) to allocate to Jalview
115 public static long getMemorySetting(String jvmmemmaxarg,
116 String jvmmempcarg, boolean useProps, boolean quiet)
118 // actual Xmx value-to-be
119 long maxMemLong = -1;
120 clearAdjustmentMessage();
122 // (absolute) jvmmaxmem setting, start with default
123 long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
124 if (jvmmemmaxarg == null && useProps)
126 jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME);
128 String jvmmemmax = jvmmemmaxarg;
129 if (jvmmemmax != null && jvmmemmax.length() > 0)
134 memmax = memoryStringToLong(jvmmemmax);
137 throw (new NumberFormatException("Not allowing 0"));
139 } catch (NumberFormatException e)
141 memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
142 setAdjustmentMessage("MemorySetting Property '"
143 + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg
144 + "') badly formatted or 0, using default ("
145 + MAX_HEAPSIZE_GB_DEFAULT + "g).", quiet);
148 // check at least minimum value (this accounts for negatives too)
149 if (memmax < APPLICATION_MIN_MEMORY)
151 memmax = APPLICATION_MIN_MEMORY;
152 setAdjustmentMessage("MemorySetting Property '"
153 + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg
154 + ") too small, using minimum (" + APPLICATION_MIN_MEMORY
161 // no need to warn if no setting
162 // adjustmentMessage("MemorySetting Property '" + maxHeapSizeProperty
167 // get max percent of physical memory, starting with default
168 float percent = MAX_HEAPSIZE_PERCENT_DEFAULT;
169 if (jvmmempcarg == null && useProps)
171 jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME);
173 String jvmmempc = jvmmempcarg;
177 if (jvmmempc != null)
179 int trypercent = Integer.parseInt(jvmmempc);
180 if (0 <= trypercent && trypercent <= 100)
182 percent = trypercent;
186 setAdjustmentMessage("MemorySetting Property '"
187 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
188 + "' should be in range 0..100. Using default " + percent
192 } catch (NumberFormatException e)
194 setAdjustmentMessage("MemorySetting property '"
195 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
196 + ") badly formatted", quiet);
199 // catch everything in case of no com.sun.management.OperatingSystemMXBean
200 boolean memoryPercentError = false;
203 long physicalMem = GetMemory.getPhysicalMemory();
204 if (physicalMem > APPLICATION_MIN_MEMORY)
206 // try and set at least applicationMinMemory and thereafter ensure
207 // leaveFreeMinMemory is left for the OS
209 mempc = (long) ((physicalMem / 100F) * percent);
211 // check for memory left for OS
212 boolean reducedmempc = false;
213 if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
215 mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
217 setAdjustmentMessage("MemorySetting Property '"
218 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
219 + ") too large. Leaving free space for OS and reducing to ("
220 + mempc + ").", quiet);
223 // check for minimum application memsize
224 if (mempc < APPLICATION_MIN_MEMORY)
228 setAdjustmentMessage("Reduced MemorySetting (" + mempc
229 + ") too small. Increasing to application minimum ("
230 + APPLICATION_MIN_MEMORY + ").", quiet);
234 setAdjustmentMessage("MemorySetting Property '"
235 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
236 + jvmmempcarg + ") too small. Using minimum ("
237 + APPLICATION_MIN_MEMORY + ").", quiet);
239 mempc = APPLICATION_MIN_MEMORY;
244 // not enough memory for application, just try and grab what we can!
246 setAdjustmentMessage(
247 "Not enough physical memory for application. Ignoring MemorySetting Property '"
248 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
250 + "). Using maximum memory available ("
251 + physicalMem + ").",
255 } catch (Throwable t)
257 memoryPercentError = true;
258 setAdjustmentMessage(
259 "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean",
264 // In the case of an error reading the percentage of physical memory (when
265 // jvmmempc was set OR neither jvmmempc nor jvmmemmax were set), let's cap
267 if (memoryPercentError && mempc == -1
268 && !(jvmmempcarg == null && jvmmemmaxarg != null) // the same as
275 && memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE)
277 setAdjustmentMessage(
278 "Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT
279 + "g due to failure to read physical memory size.",
281 memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
284 if (mempc == -1) // percentage memory not set
290 maxMemLong = Math.min(mempc, memmax);
296 public static boolean isValidMemoryString(String text)
298 if (text.length() > 0)
300 char lastChar = text.charAt(text.length() - 1);
301 char[] otherChars = text.substring(0, text.length() - 1)
303 for (char c : otherChars)
305 if (c < '0' || c > '9')
310 if ((lastChar < '0' || lastChar > '9') && memorySuffixes
311 .indexOf(Character.toLowerCase(lastChar)) == -1)
319 public static long memoryStringToLong(String memString)
320 throws NumberFormatException
322 if (!isValidMemoryString(memString)) // not valid
324 throw (new NumberFormatException("Not a valid memory string"));
326 char suffix = Character
327 .toLowerCase(memString.charAt(memString.length() - 1));
328 if ('0' <= suffix && suffix <= '9') // no suffix
330 return Long.valueOf(memString);
332 if (memorySuffixes.indexOf(suffix) == -1) // suffix is unknown
337 long multiplier = (long) Math.pow(2,
338 memorySuffixes.indexOf(suffix) * 10); // note order of suffixes in
339 // memorySuffixes important
341 // parse the arg. NumberFormatExceptions passed on to calling method
343 .parseLong(memString.substring(0, memString.length() - 1));
349 // apply multiplier only if result is not too big (i.e. bigger than a long)
350 if (Long.MAX_VALUE / mem > multiplier)
352 return multiplier * mem;
356 // number too big for a Long. Limit to Long.MAX_VALUE
357 System.out.println("Memory parsing of '" + memString
358 + "' produces number too big. Limiting to Long.MAX_VALUE="
360 return Long.MAX_VALUE;
364 public static String memoryLongToString(long mem)
366 return memoryLongToString(mem, "%.3f");
369 public static String memoryLongToString(long mem, String format)
375 for (int i = 0; i < memorySuffixes.length(); i++)
377 char s = Character.toUpperCase(memorySuffixes.charAt(i));
378 if (mem < (long) Math.pow(2, exponent + 10)
379 || i == memorySuffixes.length() - 1) // last suffix
382 num = (float) (mem / Math.pow(2, exponent));
388 return String.format(format, num) + suffix;
391 private static String ADJUSTMENT_MESSAGE = null;
393 private static void setAdjustmentMessage(String reason, boolean quiet)
395 ADJUSTMENT_MESSAGE = reason;
398 System.out.println(reason);
402 public static void clearAdjustmentMessage()
404 ADJUSTMENT_MESSAGE = null;
407 public static String getAdjustmentMessage()
409 return ADJUSTMENT_MESSAGE;