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
65 * memory setting returned will be the lower of the two values. If either of
66 * the values are not provided then defaults will be used (jvmmempc=90,
67 * jvmmemmax=32GB). If total physical memory can't be ascertained when
68 * jvmmempc was set or neither jvmmempc nor jvmmemmax were set, then jvmmemmax
69 * defaults to a much safer 8GB. In this case explicitly setting jvmmemmax and
70 * not setting jvmmempc can set a higher memory for Jalview. The calculation
71 * also tries to ensure 0.5GB memory for the OS, but also tries to ensure at
72 * least 0.5GB memory for Jalview (which takes priority over the OS) If there
73 * is less then 0.5GB of physical memory then the total physical memory is
77 * Maximum value of memory to set. This can be a numeric string
78 * optionally followed by "b", "k", "m", "g", "t" (case insensitive)
79 * to indicate bytes, kilobytes, megabytes, gigabytes, terabytes
80 * respectively. If null a default value of 32G will be used. If null
81 * and either physical memory can't be determined then the default is
84 * Max percentage of physical memory to use. Defaults to "90".
86 * @return The amount of memory (in bytes) to allocate to Jalview
88 public static long getMemorySetting(String jvmmemmaxarg,
91 // actual Xmx value-to-be
94 // (absolute) jvmmaxmem setting, start with default
95 long memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
96 if (jvmmemmaxarg == null)
98 jvmmemmaxarg = System.getProperty(MAX_HEAPSIZE_PROPERTY_NAME);
100 String jvmmemmax = jvmmemmaxarg;
101 if (jvmmemmax != null && jvmmemmax.length() > 0)
104 switch (jvmmemmax.toLowerCase().substring(jvmmemmax.length() - 1))
107 multiplier = 1099511627776L; // 2^40
108 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
111 multiplier = 1073741824; // 2^30
112 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
115 multiplier = 1048576; // 2^20
116 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
119 multiplier = 1024; // 2^10
120 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
123 multiplier = 1; // 2^0
124 jvmmemmax = jvmmemmax.substring(0, jvmmemmax.length() - 1);
133 memmax = Long.parseLong(jvmmemmax);
134 } catch (NumberFormatException e)
136 memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
137 System.out.println("MemorySetting Property '"
138 + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg
139 + "') badly formatted, using default ("
140 + MAX_HEAPSIZE_GB_DEFAULT + "g).");
143 // apply multiplier if not too big (i.e. bigger than a long)
144 if (Long.MAX_VALUE / memmax < multiplier)
146 memmax = MAX_HEAPSIZE_GB_DEFAULT * GIGABYTE;
147 System.out.println("MemorySetting Property '"
148 + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg
149 + ") too big, using default (" + MAX_HEAPSIZE_GB_DEFAULT
154 memmax = multiplier * memmax;
157 // check at least minimum value (this accounts for negatives too)
158 if (memmax < APPLICATION_MIN_MEMORY)
160 memmax = APPLICATION_MIN_MEMORY;
161 System.out.println("MemorySetting Property '"
162 + MAX_HEAPSIZE_PROPERTY_NAME + "' (" + jvmmemmaxarg
163 + ") too small, using minimum (" + APPLICATION_MIN_MEMORY
170 // no need to warn if no setting
171 // System.out.println("MemorySetting Property '" + maxHeapSizeProperty
176 // get max percent of physical memory, starting with default
177 float percent = MAX_HEAPSIZE_PERCENT_DEFAULT;
178 if (jvmmempcarg == null)
180 jvmmempcarg = System.getProperty(MAX_HEAPSIZE_PERCENT_PROPERTY_NAME);
182 String jvmmempc = jvmmempcarg;
186 if (jvmmempc != null)
188 float trypercent = Float.parseFloat(jvmmempc);
189 if (0 < trypercent && trypercent <= 100f)
191 percent = trypercent;
195 System.out.println("MemorySetting Property '"
196 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME
197 + "' should be in range 1..100. Using default " + percent
201 } catch (NumberFormatException e)
203 System.out.println("MemorySetting property '"
204 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
205 + ") badly formatted");
208 // catch everything in case of no com.sun.management.OperatingSystemMXBean
209 boolean memoryPercentError = false;
212 long physicalMem = GetMemory.getPhysicalMemory();
213 if (physicalMem > APPLICATION_MIN_MEMORY)
215 // try and set at least applicationMinMemory and thereafter ensure
216 // leaveFreeMinMemory is left for the OS
218 mempc = (long) ((physicalMem / 100F) * percent);
220 // check for memory left for OS
221 boolean reducedmempc = false;
222 if (physicalMem - mempc < LEAVE_FREE_MIN_MEMORY)
224 mempc = physicalMem - LEAVE_FREE_MIN_MEMORY;
226 System.out.println("MemorySetting Property '"
227 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' (" + jvmmempcarg
228 + ") too large. Leaving free space for OS and reducing to ("
232 // check for minimum application memsize
233 if (mempc < APPLICATION_MIN_MEMORY)
237 System.out.println("Reduced MemorySetting (" + mempc
238 + ") too small. Increasing to application minimum ("
239 + APPLICATION_MIN_MEMORY + ").");
243 System.out.println("MemorySetting Property '"
244 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
245 + jvmmempcarg + ") too small. Using minimum ("
246 + APPLICATION_MIN_MEMORY + ").");
248 mempc = APPLICATION_MIN_MEMORY;
253 // not enough memory for application, just try and grab what we can!
256 "Not enough physical memory for application. Ignoring MemorySetting Property '"
257 + MAX_HEAPSIZE_PERCENT_PROPERTY_NAME + "' ("
259 + "). Using maximum memory available ("
260 + physicalMem + ").");
263 } catch (Throwable t)
265 memoryPercentError = true;
267 "Problem calling GetMemory.getPhysicalMemory(). Likely to be problem with com.sun.management.OperatingSystemMXBean");
271 // In the case of an error reading the percentage of physical memory (when
272 // jvmmempc was set OR neither jvmmempc nor jvmmemmax were set), let's cap
274 if (memoryPercentError && mempc == -1
275 && !(jvmmempcarg == null && jvmmemmaxarg != null) // the same as
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);