4 * Methods to decide on appropriate memory setting for Jalview based on two
5 * optionally provided values: jvmmempc - the maximum percentage of total
6 * physical memory to allocate, and jvmmemmax - the maximum absolute amount of
7 * physical memory to allocate. These can be provided as arguments or system
8 * properties. Other considerations such as minimum application requirements and
9 * leaving space for OS are used too.
14 public class MemorySetting
16 public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc";
18 public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax";
20 private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; // 90%
22 private static final long GIGABYTE = 1073741824; // 1GB
24 public static final long LEAVE_FREE_MIN_MEMORY = GIGABYTE/2;
26 public static final long APPLICATION_MIN_MEMORY = GIGABYTE/2;
28 private static final long MAX_HEAPSIZE_GB_DEFAULT = 32;
30 private static final long NOMEM_MAX_HEAPSIZE_GB_DEFAULT = 8;
32 protected static boolean logToClassChecked = false;
34 public static long getMemorySetting()
36 return getMemorySetting(null, null);
40 * Decide on appropriate memory setting for Jalview based on the two arguments
41 * values: jvmmempc - the maximum percentage of total physical memory to
42 * allocate, and jvmmemmax - the maximum absolute amount of physical memory to
43 * allocate. These can be provided as arguments. If not provided as arguments
44 * (or set as null) system properties will be used instead (if set). The memory
45 * setting returned will be the lower of the two values. If either of the values
46 * are not provided then defaults will be used (jvmmempc=90, jvmmemmax=32GB). If
47 * total physical memory can't be ascertained when jvmmempc was set or neither
48 * jvmmempc nor jvmmemmax were set, then jvmmemmax defaults to a much safer 8GB.
49 * In this case explicitly setting jvmmemmax and not setting jvmmempc can set a
50 * higher memory for Jalview. The calculation also tries to ensure 0.5GB memory
51 * for the OS, but also tries to ensure at least 0.5GB memory for Jalview (which
52 * takes priority over the OS) If there is less then 0.5GB of physical memory
53 * then the total physical memory is used for Jalview.
56 * Maximum value of memory to set. This can be a numeric
57 * string optionally followed by "b", "k", "m", "g", "t"
58 * (case insensitive) to indicate bytes, kilobytes,
59 * megabytes, gigabytes, terabytes respectively. If null a
60 * default value of 32G will be used. If null and either
61 * physical memory can't be determined then the default is
64 * Max percentage of physical memory to use. Defaults to
67 * @return The amount of memory (in bytes) to allocate to Jalview
69 public static long getMemorySetting(String jvmmemmaxarg,
72 // actual Xmx value-to-be
75 // (absolute) jvmmaxmem setting, start with default
76 long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
77 if (jvmmemmaxarg == null)
79 jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME);
81 String jvmmemmax = jvmmemmaxarg;
82 if (jvmmemmax != null && jvmmemmax.length() > 0)
85 switch (jvmmemmax.toLowerCase().substring(jvmmemmax.length() - 1))
88 multiplier = 1099511627776L; // 2^40
89 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
92 multiplier = 1073741824; // 2^30
93 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
96 multiplier = 1048576; // 2^20
97 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
100 multiplier = 1024; // 2^10
101 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
104 multiplier = 1; // 2^0
105 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
114 memmax = Long.parseLong(jvmmemmax);
115 } catch (NumberFormatException e)
117 memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
118 System.out.println("MemorySetting Property '"
119 + MAX_HEAPSIZE_PROPERTY_NAME
121 + jvmmemmaxarg + "') badly formatted, using default ("
122 + MAX_HEAPSIZE_GB_DEFAULT + "g).");
125 // apply multiplier if not too big (i.e. bigger than a long)
126 if (Long.MAX_VALUE / memmax < multiplier)
128 memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
130 "MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' ("
132 + ") too big, using default ("
133 + MAX_HEAPSIZE_GB_DEFAULT + "g).");
137 memmax = multiplier * memmax;
140 // check at least minimum value (this accounts for negatives too)
141 if (memmax < APPLICATION_MIN_MEMORY)
143 memmax = APPLICATION_MIN_MEMORY;
145 "MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' ("
147 + ") too small, using minimum ("
148 + APPLICATION_MIN_MEMORY + ").");
154 // no need to warn if no setting
155 // System.out.println("MemorySetting Property '" + maxHeapSizeProperty
160 // get max percent of physical memory, starting with default
161 float percent = MAX_HEAPSIZE_PERCENT_DEFAULT;
162 if (jvmmempcarg == null)
164 jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME);
166 String jvmmempc = jvmmempcarg;
170 if (jvmmempc != null)
172 float trypercent = Float.parseFloat(jvmmempc);
173 if (0 < trypercent && trypercent <= 100f)
175 percent = trypercent;
180 "MemorySetting Property '"
181 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
182 + "' should be in range 1..100. Using default "
186 } catch (NumberFormatException e)
189 "MemorySetting property '" + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
190 + "' (" + jvmmempcarg + ") badly formatted");
193 // catch everything in case of no com.sun.management.OperatingSystemMXBean
194 boolean memoryPercentError = false;
197 long physicalMem = GetMemory.getPhysicalMemory();
198 if (physicalMem > APPLICATION_MIN_MEMORY)
200 // try and set at least applicationMinMemory and thereafter ensure
201 // leaveFreeMinMemory is left for the OS
203 mempc = (long) ((physicalMem / 100F) * percent);
205 // check for memory left for OS
206 boolean reducedmempc = false;
207 if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
209 mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
211 System.out.println("MemorySetting Property '"
212 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
213 + ") too large. Leaving free space for OS and reducing to ("
217 // check for minimum application memsize
218 if (mempc < APPLICATION_MIN_MEMORY)
222 System.out.println("Reduced MemorySetting (" + mempc
223 + ") too small. Increasing to application minimum ("
224 + APPLICATION_MIN_MEMORY + ").");
228 System.out.println("MemorySetting Property '"
229 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
230 + ") too small. Using minimum (" + APPLICATION_MIN_MEMORY
233 mempc = APPLICATION_MIN_MEMORY;
238 // not enough memory for application, just try and grab what we can!
241 "Not enough physical memory for application. Ignoring MemorySetting Property '"
242 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
244 + "). Using maximum memory available ("
245 + physicalMem + ").");
248 } catch (Throwable t)
250 memoryPercentError = true;
252 "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean");
256 // In the case of an error reading the percentage of physical memory (when
257 // jvmmempc was set OR neither jvmmempc nor jvmmemmax were set), let's cap
259 if (memoryPercentError && mempc == -1
260 && !(jvmmempcarg == null && jvmmemmaxarg != null) // the same as (jvmmempcarg != null || (jvmmempcarg == null && jvmmemmaxarg
262 && memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE)
265 "Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT
266 + "g due to failure to read physical memory size.");
267 memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
270 if (mempc == -1) // percentage memory not set
276 maxMemLong = Math.min(mempc, memmax);