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.
36 public class MemorySetting
38 public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc";
40 public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax";
42 private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; // 90%
44 private static final long GIGABYTE = 1073741824; // 1GB
46 public static final long LEAVE_FREE_MIN_MEMORY = GIGABYTE / 2;
48 public static final long APPLICATION_MIN_MEMORY = GIGABYTE / 2;
50 private static final long MAX_HEAPSIZE_GB_DEFAULT = 32;
52 private static final long NOMEM_MAX_HEAPSIZE_GB_DEFAULT = 8;
54 public static final String NS = "MEMORY";
56 public static final String CUSTOMISED_SETTINGS = NS
57 + "_CUSTOMISED_SETTINGS";
59 public static final String MEMORY_JVMMEMPC = NS + "_"
60 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME.toUpperCase();
62 public static final String MEMORY_JVMMEMMAX = NS + "_"
63 + MAX_HEAPSIZE_PROPERTY_NAME.toUpperCase();
65 protected static boolean logToClassChecked = false;
67 public static String memorySuffixes = "bkmgt"; // order of the suffixes is
70 public static long getMemorySetting()
72 return getMemorySetting(null, null);
75 public static long getMemorySetting(String jvmmemmaxarg,
78 return getMemorySetting(jvmmemmaxarg, jvmmempcarg, true, false);
82 * Decide on appropriate memory setting for Jalview based on the two arguments
83 * values: jvmmempc - the maximum percentage of total physical memory to
84 * allocate, and jvmmemmax - the maximum absolute amount of physical memory to
85 * allocate. These can be provided as arguments. If not provided as arguments
86 * (or set as null) system properties will be used instead (if set). The
87 * memory setting returned will be the lower of the two values. If either of
88 * the values are not provided then defaults will be used (jvmmempc=90,
89 * jvmmemmax=32GB). If total physical memory can't be ascertained when
90 * jvmmempc was set or neither jvmmempc nor jvmmemmax were set, then jvmmemmax
91 * defaults to a much safer 8GB. In this case explicitly setting jvmmemmax and
92 * not setting jvmmempc can set a higher memory for Jalview. The calculation
93 * also tries to ensure 0.5GB memory for the OS, but also tries to ensure at
94 * least 0.5GB memory for Jalview (which takes priority over the OS) If there
95 * is less then 0.5GB of physical memory then the total physical memory is
99 * Maximum value of memory to set. This can be a numeric string
100 * optionally followed by "b", "k", "m", "g", "t" (case insensitive)
101 * to indicate bytes, kilobytes, megabytes, gigabytes, terabytes
102 * respectively. If null a default value of 32G will be used. If null
103 * and either physical memory can't be determined then the default is
106 * Max percentage of physical memory to use. Defaults to "90".
109 * boolean to decide whether to look at System properties.
111 * @return The amount of memory (in bytes) to allocate to Jalview
113 public static long getMemorySetting(String jvmmemmaxarg,
114 String jvmmempcarg, boolean useProps, boolean quiet)
116 // actual Xmx value-to-be
117 long maxMemLong = -1;
118 clearAdjustmentMessage();
120 // (absolute) jvmmaxmem setting, start with default
121 long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
122 if (jvmmemmaxarg == null && useProps)
124 jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME);
126 String jvmmemmax = jvmmemmaxarg;
127 if (jvmmemmax != null && jvmmemmax.length() > 0)
132 memmax = memoryStringToLong(jvmmemmax);
135 throw (new NumberFormatException("Not allowing 0"));
137 } catch (NumberFormatException e)
139 memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
140 setAdjustmentMessage("MemorySetting Property '"
141 + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg
142 + "') badly formatted or 0, using default ("
143 + MAX_HEAPSIZE_GB_DEFAULT + "g).", quiet);
146 // check at least minimum value (this accounts for negatives too)
147 if (memmax < APPLICATION_MIN_MEMORY)
149 memmax = APPLICATION_MIN_MEMORY;
150 setAdjustmentMessage("MemorySetting Property '"
151 + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg
152 + ") too small, using minimum (" + APPLICATION_MIN_MEMORY
159 // no need to warn if no setting
160 // adjustmentMessage("MemorySetting Property '" + maxHeapSizeProperty
165 // get max percent of physical memory, starting with default
166 float percent = MAX_HEAPSIZE_PERCENT_DEFAULT;
167 if (jvmmempcarg == null && useProps)
169 jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME);
171 String jvmmempc = jvmmempcarg;
175 if (jvmmempc != null)
177 int trypercent = Integer.parseInt(jvmmempc);
178 if (0 <= trypercent && trypercent <= 100)
180 percent = trypercent;
184 setAdjustmentMessage("MemorySetting Property '"
185 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
186 + "' should be in range 0..100. Using default " + percent
190 } catch (NumberFormatException e)
192 setAdjustmentMessage("MemorySetting property '"
193 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
194 + ") badly formatted", quiet);
197 // catch everything in case of no com.sun.management.OperatingSystemMXBean
198 boolean memoryPercentError = false;
201 long physicalMem = GetMemory.getPhysicalMemory();
202 if (physicalMem > APPLICATION_MIN_MEMORY)
204 // try and set at least applicationMinMemory and thereafter ensure
205 // leaveFreeMinMemory is left for the OS
207 mempc = (long) ((physicalMem / 100F) * percent);
209 // check for memory left for OS
210 boolean reducedmempc = false;
211 if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
213 mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
215 setAdjustmentMessage("MemorySetting Property '"
216 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
217 + ") too large. Leaving free space for OS and reducing to ("
218 + mempc + ").", quiet);
221 // check for minimum application memsize
222 if (mempc < APPLICATION_MIN_MEMORY)
226 setAdjustmentMessage("Reduced MemorySetting (" + mempc
227 + ") too small. Increasing to application minimum ("
228 + APPLICATION_MIN_MEMORY + ").", quiet);
232 setAdjustmentMessage("MemorySetting Property '"
233 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
234 + jvmmempcarg + ") too small. Using minimum ("
235 + APPLICATION_MIN_MEMORY + ").", quiet);
237 mempc = APPLICATION_MIN_MEMORY;
242 // not enough memory for application, just try and grab what we can!
244 setAdjustmentMessage(
245 "Not enough physical memory for application. Ignoring MemorySetting Property '"
246 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
248 + "). Using maximum memory available ("
249 + physicalMem + ").",
253 } catch (Throwable t)
255 memoryPercentError = true;
256 setAdjustmentMessage(
257 "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean",
262 // In the case of an error reading the percentage of physical memory (when
263 // jvmmempc was set OR neither jvmmempc nor jvmmemmax were set), let's cap
265 if (memoryPercentError && mempc == -1
266 && !(jvmmempcarg == null && jvmmemmaxarg != null) // the same as
273 && memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE)
275 setAdjustmentMessage(
276 "Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT
277 + "g due to failure to read physical memory size.",
279 memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
282 if (mempc == -1) // percentage memory not set
288 maxMemLong = Math.min(mempc, memmax);
294 public static boolean isValidMemoryString(String text)
296 if (text.length() > 0)
298 char lastChar = text.charAt(text.length() - 1);
299 char[] otherChars = text.substring(0, text.length() - 1)
301 for (char c : otherChars)
303 if (c < '0' || c > '9')
308 if ((lastChar < '0' || lastChar > '9') && memorySuffixes
309 .indexOf(Character.toLowerCase(lastChar)) == -1)
317 public static long memoryStringToLong(String memString)
318 throws NumberFormatException
320 if (!isValidMemoryString(memString)) // not valid
322 throw (new NumberFormatException("Not a valid memory string"));
324 char suffix = Character
325 .toLowerCase(memString.charAt(memString.length() - 1));
326 if ('0' <= suffix && suffix <= '9') // no suffix
328 return Long.valueOf(memString);
330 if (memorySuffixes.indexOf(suffix) == -1) // suffix is unknown
335 long multiplier = (long) Math.pow(2,
336 memorySuffixes.indexOf(suffix) * 10); // note order of suffixes in
337 // memorySuffixes important
339 // parse the arg. NumberFormatExceptions passed on to calling method
341 .parseLong(memString.substring(0, memString.length() - 1));
347 // apply multiplier only if result is not too big (i.e. bigger than a long)
348 if (Long.MAX_VALUE / mem > multiplier)
350 return multiplier * mem;
354 // number too big for a Long. Limit to Long.MAX_VALUE
355 System.out.println("Memory parsing of '" + memString
356 + "' produces number too big. Limiting to Long.MAX_VALUE="
358 return Long.MAX_VALUE;
362 public static String memoryLongToString(long mem)
364 return memoryLongToString(mem, "%.3f");
367 public static String memoryLongToString(long mem, String format)
373 for (int i = 0; i < memorySuffixes.length(); i++)
375 char s = Character.toUpperCase(memorySuffixes.charAt(i));
376 if (mem < (long) Math.pow(2, exponent + 10)
377 || i == memorySuffixes.length() - 1) // last suffix
380 num = (float) (mem / Math.pow(2, exponent));
386 return String.format(format, num) + suffix;
389 private static String ADJUSTMENT_MESSAGE = null;
391 private static void setAdjustmentMessage(String reason, boolean quiet)
393 ADJUSTMENT_MESSAGE = reason;
396 System.out.println(reason);
400 public static void clearAdjustmentMessage()
402 ADJUSTMENT_MESSAGE = null;
405 public static String getAdjustmentMessage()
407 return ADJUSTMENT_MESSAGE;