2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
24 * Methods to decide on appropriate memory setting for Jalview based on two
25 * optionally provided values: jvmmempc - the maximum percentage of total
26 * physical memory to allocate, and jvmmemmax - the maximum absolute amount of
27 * physical memory to allocate. These can be provided as arguments or system
28 * properties. Other considerations such as minimum application requirements and
29 * leaving space for OS are used too.
34 public class MemorySetting
36 public static final String MAX_HEAPSIZE_PERCENT_PROPERTY_NAME = "jvmmempc";
38 public static final String MAX_HEAPSIZE_PROPERTY_NAME = "jvmmemmax";
40 private static final int MAX_HEAPSIZE_PERCENT_DEFAULT = 90; // 90%
42 private static final long GIGABYTE = 1073741824; // 1GB
44 public static final long LEAVE_FREE_MIN_MEMORY = GIGABYTE/2;
46 public static final long APPLICATION_MIN_MEMORY = GIGABYTE/2;
48 private static final long MAX_HEAPSIZE_GB_DEFAULT = 32;
50 private static final long NOMEM_MAX_HEAPSIZE_GB_DEFAULT = 8;
52 protected static boolean logToClassChecked = false;
54 public static long getMemorySetting()
56 return getMemorySetting(null, null);
60 * Decide on appropriate memory setting for Jalview based on the two arguments
61 * values: jvmmempc - the maximum percentage of total physical memory to
62 * allocate, and jvmmemmax - the maximum absolute amount of physical memory to
63 * allocate. These can be provided as arguments. If not provided as arguments
64 * (or set as null) system properties will be used instead (if set). The memory
65 * setting returned will be the lower of the two values. If either of the values
66 * are not provided then defaults will be used (jvmmempc=90, jvmmemmax=32GB). If
67 * total physical memory can't be ascertained when jvmmempc was set or neither
68 * jvmmempc nor jvmmemmax were set, then jvmmemmax defaults to a much safer 8GB.
69 * In this case explicitly setting jvmmemmax and not setting jvmmempc can set a
70 * higher memory for Jalview. The calculation also tries to ensure 0.5GB memory
71 * for the OS, but also tries to ensure at least 0.5GB memory for Jalview (which
72 * takes priority over the OS) If there is less then 0.5GB of physical memory
73 * then the total physical memory is used for Jalview.
76 * Maximum value of memory to set. This can be a numeric
77 * string optionally followed by "b", "k", "m", "g", "t"
78 * (case insensitive) to indicate bytes, kilobytes,
79 * megabytes, gigabytes, terabytes respectively. If null a
80 * default value of 32G will be used. If null and either
81 * physical memory can't be determined then the default is
84 * Max percentage of physical memory to use. Defaults to
87 * @return The amount of memory (in bytes) to allocate to Jalview
89 public static long getMemorySetting(String jvmmemmaxarg,
92 // actual Xmx value-to-be
95 // (absolute) jvmmaxmem setting, start with default
96 long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
97 if (jvmmemmaxarg == null)
99 jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME);
101 String jvmmemmax = jvmmemmaxarg;
102 if (jvmmemmax != null && jvmmemmax.length() > 0)
105 switch (jvmmemmax.toLowerCase().substring(jvmmemmax.length() - 1))
108 multiplier = 1099511627776L; // 2^40
109 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
112 multiplier = 1073741824; // 2^30
113 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
116 multiplier = 1048576; // 2^20
117 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
120 multiplier = 1024; // 2^10
121 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
124 multiplier = 1; // 2^0
125 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
134 memmax = Long.parseLong(jvmmemmax);
135 } catch (NumberFormatException e)
137 memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
138 System.out.println("MemorySetting Property '"
139 + MAX_HEAPSIZE_PROPERTY_NAME
141 + jvmmemmaxarg + "') badly formatted, using default ("
142 + MAX_HEAPSIZE_GB_DEFAULT + "g).");
145 // apply multiplier if not too big (i.e. bigger than a long)
146 if (Long.MAX_VALUE / memmax < multiplier)
148 memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
150 "MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' ("
152 + ") too big, using default ("
153 + MAX_HEAPSIZE_GB_DEFAULT + "g).");
157 memmax = multiplier * memmax;
160 // check at least minimum value (this accounts for negatives too)
161 if (memmax < APPLICATION_MIN_MEMORY)
163 memmax = APPLICATION_MIN_MEMORY;
165 "MemorySetting Property '" + MAX_HEAPSIZE_PROPERTY_NAME + "' ("
167 + ") too small, using minimum ("
168 + APPLICATION_MIN_MEMORY + ").");
174 // no need to warn if no setting
175 // System.out.println("MemorySetting Property '" + maxHeapSizeProperty
180 // get max percent of physical memory, starting with default
181 float percent = MAX_HEAPSIZE_PERCENT_DEFAULT;
182 if (jvmmempcarg == null)
184 jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME);
186 String jvmmempc = jvmmempcarg;
190 if (jvmmempc != null)
192 float trypercent = Float.parseFloat(jvmmempc);
193 if (0 < trypercent && trypercent <= 100f)
195 percent = trypercent;
200 "MemorySetting Property '"
201 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
202 + "' should be in range 1..100. Using default "
206 } catch (NumberFormatException e)
209 "MemorySetting property '" + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
210 + "' (" + jvmmempcarg + ") badly formatted");
213 // catch everything in case of no com.sun.management.OperatingSystemMXBean
214 boolean memoryPercentError = false;
217 long physicalMem = GetMemory.getPhysicalMemory();
218 if (physicalMem > APPLICATION_MIN_MEMORY)
220 // try and set at least applicationMinMemory and thereafter ensure
221 // leaveFreeMinMemory is left for the OS
223 mempc = (long) ((physicalMem / 100F) * percent);
225 // check for memory left for OS
226 boolean reducedmempc = false;
227 if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
229 mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
231 System.out.println("MemorySetting Property '"
232 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
233 + ") too large. Leaving free space for OS and reducing to ("
237 // check for minimum application memsize
238 if (mempc < APPLICATION_MIN_MEMORY)
242 System.out.println("Reduced MemorySetting (" + mempc
243 + ") too small. Increasing to application minimum ("
244 + APPLICATION_MIN_MEMORY + ").");
248 System.out.println("MemorySetting Property '"
249 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
250 + ") too small. Using minimum (" + APPLICATION_MIN_MEMORY
253 mempc = APPLICATION_MIN_MEMORY;
258 // not enough memory for application, just try and grab what we can!
261 "Not enough physical memory for application. Ignoring MemorySetting Property '"
262 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
264 + "). Using maximum memory available ("
265 + physicalMem + ").");
268 } catch (Throwable t)
270 memoryPercentError = true;
272 "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean");
276 // In the case of an error reading the percentage of physical memory (when
277 // jvmmempc was set OR neither jvmmempc nor jvmmemmax were set), let's cap
279 if (memoryPercentError && mempc == -1
280 && !(jvmmempcarg == null && jvmmemmaxarg != null) // the same as (jvmmempcarg != null || (jvmmempcarg == null && jvmmemmaxarg
282 && memmax > NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE)
285 "Capping maximum memory to " + NOMEM_MAX_HEAPSIZE_GB_DEFAULT
286 + "g due to failure to read physical memory size.");
287 memmax = NOMEM_MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
290 if (mempc == -1) // percentage memory not set
296 maxMemLong = Math.min(mempc, memmax);